Avoiding string copying in Lua

Posted by Matt Sheppard on Stack Overflow See other posts from Stack Overflow or by Matt Sheppard
Published on 2010-05-18T08:09:02Z Indexed on 2010/05/18 8:10 UTC
Read the original article Hit count: 262

Filed under:
|
|
|

Say I have a C program which wants to call a very simple Lua function with two strings (let's say two comma separated lists, returning true if the lists intersect at all, false if not).

The obvious way to do this is to push them onto the stack with lua_pushstring, which works fine, however, from the doc it looks like lua_pushstring but makes a copy of the string for Lua to work with.

That means that to cross over to the Lua function is going to require two string copies which I could avoid by rewriting the Lua function in C. Is there any way to arrange things so that the existing C strings could be reused on the Lua side for the sake of performance (or would the strcpy cost pale into insignificance anyway)?

From my investigation so far (my first couple of hours looking seriously at Lua), lite userdata seems like the sort of thing I want, but in the form of a string.

© Stack Overflow or respective owner

Related posts about beginner

Related posts about lua