lua-users home
lua-l archive

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



>> 在 2019年6月17日,21:40,Matthew Wild <mwild1@gmail.com> 写道:
>> 
>> On Mon, 17 Jun 2019 at 12:57, 云风 Cloud Wu <cloudwu@gmail.com> wrote:
>> 
>> Lua has unique string type before 5.2.1 , all the strings is interning
>> for fast comparison .
>> 
>> Lua 5.2.1 add a new internal long string type because of hash DoS
>> 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).
> 
> There is also a benefit in reduced RAM usage (in some applications).
> But importantly a string's hash is also used for table lookups, which
> is quite a key part of Lua.
> 
> Regards,
> Matthew

The hash is remained, but can be lazy calculated like long string now.

To reduce memory usage , we can do string interning in parser stage (it’s the main source of the string object) to remove the same strings. And we can also use a cache like lua_pushstring now to avoid push the same string or combine the same string during gc .