lua-users home
lua-l archive

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




On 2017-07-03 04:16 PM, J Doe wrote:
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


Lua already combines multiple concatenations into one. It all becomes a single OP_CONCAT.

On the other hand, Lua does not combine multiple array initializations into one.

--
Disclaimer: these emails may be made public at any given time, with or without reason. If you don't agree with this, DO NOT REPLY.