lua-users home
lua-l archive

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


2014/1/2 Thijs Schreijer <thijs@thijsschreijer.nl>:
>
>> Assignment does not duplicate strings. Moreover, because strings are
>> interned, there is a single copy of strings with equal contents; if you
>> produce another identical string by different means (e.g. by concatenation),
>> after the interning operation a single copy of the string will exist (please
>> correct me if I am wrong here).
>
> I don't know how it works in practice, but what you're suggesting means that after each string operation the resulting string would have to be compared to every other string in the current Lua state... I doubt whether that would be possible performance wise.
>
> Considering the example again:
>  local first  = [[ some 100mb stuff here ]]
>  local second = first
>  local third  = first .. ""   -- changed here, result is the same
>
> My guess is it would still result in 2 strings, one being the value for `first` and `second`, the second one being the value for `third`, which would not be compared to the first because it is the result of an expression.
>
> But maybe someone can enlighten us :)

It works just like table keys. A hash code is computed from the
string. Only if that is the same as a string already in the table (in
which case the strings are likely to be equal) are the actual strings
compared. O(#str) average speed.