lua-users home
lua-l archive

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


On 2010-03-12, Chuck Coffing <clc@alum.mit.edu> wrote:
> Hi list,
>
>  At work we use Lua in an embedded environment.  It's a large project that
>  does a lot of network IO.  After some profiling, I discovered that
>  luaL_addlstring was consuming a large amount of the processor; much of the
>  usage was initiated by luasockets.
>
>  It turns out that luaL_addlstring calls luaL_addchar for every byte, which
>  means that multiple dereferences, a test, and a jump occur for every byte
>  received over the network.
>
>  The buffer, however, already knows how much space it has available, so the
>  repeated tests are unnecessary.  I changed luaL_addlstring to memcpy the
>  largest chunk that is known to fit, and then expand the buffer as needed.
>
>  For a trivial luasocket client that receives data, the change cuts the number
>  of instructions executed by more than 50%.  (I use valgrind to count
>  instructions.)  I recall (although it's been a while) that it cut the
>  instruction count of our app by about 13% overall.
>
>  The change only minimally increases the code size (16 bytes larger on x86):
>
>  chuck@magma:~/lua-perf$ nm -S lua.orig | grep addlstring
>  08059ec0 00000068 T luaL_addlstring
>  chuck@magma:~/lua-perf$ nm -S lua.perf | grep addlstring
>  08059ec0 00000078 T luaL_addlstring
>
>  I made the change on 5.1.4, but it looks like 5.2 has the same performance
>  issue.
>
>  Patch is below; the trivial test script and output from Valgrind is also below.
>

It would be great if this patch (or its equivalent) makes its way into
official Lua-5.1.4 patch list at http://www.lua.org/bugs.html

--Leo--