lua-users home
lua-l archive

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


I usually use a mix of table.concat and string.format. table.concat is good for loops but not so much for one-off string building, because you need to make a table and then put all values in the table, where string.format just takes raw arguments. I've not really benchmarked it though (which, I really should, to be honest).

On Mon, Jul 3, 2017, 14:17 J Doe <general@nativemethods.com> 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

--
--
Ryan <vandor2012@gmail.com>
Software Developer / System Administrator
https://hashbang.sh