lua-users home
lua-l archive

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



在 2019年6月17日,22:37,Roberto Ierusalimschy <roberto@inf.puc-rio.br> 写道:

>> Lua has unique string type before 5.2.1 , all the strings is interning
>> for fast comparison .
>> 
>> [...]
>> attack , but short string is still interning. I guess the reason is
>> the performance of string comparison is very important to lua , string
>> interning can reduce the string comparison from O(n) to O(1).
>> 
>> I think if we can find another way to compare strings with O(1) , we
>> can use only one string type without interning.
>> 
>> We can add an unique 64bit id into TString struct , every time we push
>> a string into lua VM , allocate a new id to it . When we need compare
>> two strings, compare the id first. If id are the same, they are
>> equality, otherwise we compare the hash. If hash is also the same, use
>> memcmp.  And if the strings are equality, we can change the lower id
>> to the higher one.
> 
> It is a nice idea. One drawback to keep in mind is the addition of eight
> bytes to the memory overhead of all strings. Another is that Lua does
> not demand platforms to have a 64-bit integer type.
> 
> -- Roberto
> 

If we don’t need string interning, the next pointer in TString is useless, so I think maybe no more overhead.