MenTaLguY
12/9/2005 7:25:00 AM
On Fri, 2005-12-09 at 14:52 +0900, petermichaux@yahoo.com wrote:
> Thanks for all the responses.
>
> Could someone describe how symbols are stored in memory?
Integers.
They're indices into an array of unique string values, which is
accompanied by a hash table of string values to indices.
When a string is converted to a symbol, the string is looked up in the
hash table. If no entry is found, the string value is appended to the
array, and its index is recorded to the hash table. If an entry already
exists, that index (the symbol) is simply returned.
When a symbol is converted to a string, its index is looked up in the
array and that string value returned.
The values of two symbols can also be compared directly to determine
whether their associated strings are equal. This is much faster than
comparing two strings.
If you are storing the same string value multiple places, it may also
save memory to represent it with a symbol. Be careful, however. That
array never shrinks, so every unique string value you turn into a symbol
stays around until your program exits.
It's best to reserve symbols for situations where you expect a bounded
number of unique string values.
-mental