lua-users home
lua-l archive

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


Hi,

In chapter 2 of "Lua Gems", Roberto mentions the fact that building a string in a loop by concatenating more string data to it on each iteration is wasteful as a new string must be allocated, the existing elements copied, the old string GC'ed (at some point) and the addition of the new portion of the string.

The recommendation for optimization is to emulate "string builder" functionality by storing each piece as an element in a table and then calling table.concat(t).

I am wondering if this is also a good pattern for building a regular string that has multiple concatenations . . . but say on the order of 5 to 10 concatenation operations:

t = {"Banned UA:", bad_ua, "reason: ", reason_ua, "stats", clck_elapsed}
s = table.concat(t, " ")

Is the possible performance benefit negligible for "small" string concatenation operations like this such that it's preferable to use the more common .. syntax for concatenation ?  I am looking to squeeze every last bit of speed out of the code as it is running in a Web Application Firewall (ModSecurity 2.9.x).

Thanks

- J