lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]

On 18-Dec-06, at 12:20 PM, Gavin Kistner wrote:

   "There are eight basic types in Lua: nil, boolean, number, string,
    function, userdata, thread, and table.
    Tables, functions, threads, and (full) userdata values are objects:
    variables do not actually contain these values, only references to

I think that quote from the manual is a bit misleading, personally; but I suppose it depends on how you interpret the word "object" (or, for that matter, "variable").

My layman interpretation of the second sentence above was that variables
thus *do* 'contain' values that are not object types. (I realize that
this is technically a logical fallacy, but a reasonable interpretation
of the intent.) Thus, I had thought that the code...
   local a = "foo"
   local b = "foo"
   local c = b
...meant that 3 strings were allocated in memory internally, one to be

Are 3 strings allocated and maintained internally? (Each assignment of a
non-object value copies it 'into' the variable.)

Or is it 2? (One for each string literal, with the latter shared by 'b'
and 'c'.)

Or is it 1? (Are Lua's internals tricky enough that they 'hash' a string
and only allocate memory for each unique sequence?)

Only one. Strings are indeed hashed and only unique sequences are stored in memory.

As you say, it's an implementation detail, but one of the consequences is that string equality comparisons are simply a pointer comparison, and hence as fast as any other primitive equality comparison.