Re: A reference to
Re: A reference to
- Subject: Re: A reference to
- From: Charles Arthur <email@hidden>
- Date: Mon, 4 Jun 2001 12:26:53 +0100
Bryan Harris <email@hidden> wrote:
Michelle wrote:
>
> I understand the difference between "set a to b" and "copy b to a" but
>
> don't understand what "set a to a reference to b" does.
>
>
>
I know that when you make a reference to a list, you can access that
>
reference much more quickly than accessing the list itself.
The difference is to do with stack pointers , I'm pretty sure. My (Z-80)
tuition is some decades back now, but a brief peruse of Google and some of
the papers turned up there suggest this is to do with machine architectures
generally; that is, that a similar construct should work on CISC processors
too.
Have a peruse, for example, of
http://www.google.com/search?q=cache:0gFBcZsvykc:www.ee.umanitoba.ca/programs/un
dergrad/c24422/links/ebooks/bookarch/chapter04.pdf+"a+reference+to"+"stack
+pointer"+list+faster+applescript&hl=en
.. which gets interesting (well, depending how you define interesting of
course) around the stage about summing arrays, and especially sec 4.4.1,
and "memory index registers" (which on an x86 are "used ot point to the
beginning or end of an array in memory").
Chris et al can feel free to leap in with a cluestick or cluebaseballbat
but my understanding would be...
When you define a list or variable, some memory space in the stack (which
is where the machine basically has a Stickies list of each piece of data,
plus where that data links to) is allocated to it.
To change that list/var directly (set a to b) you have to copy
that var into the registers, do something to it (add/dec), write it back
out to the stack. You have an address pointer to the top of the list/var,
and some info about how long it is. This can take a lot of time
(comparatively) when the array gets big.
A reference to the list/var, however, is just a pointer to where
that list/var is kept. If you're smart, then you can add something to the
end of a list by this method (using one of those memory index registers).
To add something to the end of the list
1) create the new element in memory
2) put that element into the stack, with a pointer to the end of your
original list
3) update your memory address register (which points to the end of the
stack) to point to that single new element you just created.
Shall I do it with people and houses? Might as well. Imagine a street of
people living in houses. Each has a piece of paper saying where their
left-hand neighbour lives. The last person on the right doesn't have a
piece of paper.
To update the list using "set a to b" means getting all those people out of
their houses and into the town hall, checking through to see who hasn't got
a piece of paper, and giving them a piece with the address of their new
"official" neighbour, sending them back to their houses. Quelle dommage.
By contrast, using "a reference to" means driving down the street and
posting the address of the new "neighbour" through the letterbox. (It even
works if there's nobody at home - ie the empty list.)
OK?
BTW I got a speed difference of about 5,300 to 17 on using "set" vs
"a reference to" on the script that someone (sorry, can't find who just
now) posted here.
So I guess I'm off to change my list uses.
Charles
----------------------------
http://www.ukclimbing.com : 1000+ British crags, 350+ British climbing
walls - searchable by distance and anything else you care to think of -
with weather forecasts for every one, plus maps, articles, news and
features. Plus Britain's busiest climbing discussion boards. And there's
even a cool shop attached.