Avoiding copying strings in Lua
Let's say I have a C program that wants to call a very simple Lua function with two strings (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 great, however, from the doc it looks like lua_pushstring, but creates a copy of the string for Lua to work with.
This means that going to a Lua function would require two string copies, which I could have avoided by rewriting the Lua function in C. Is there a way to organize things so that existing C strings can be reused in Lua to achieve performance (or if would the price be negligible)?
From my research so far (my first couple of hours looking seriously at Lua), lite userdata seems like what I want, but as a string.
a source to share
No. You cannot prevent Lua from copying a string when you call lua_pushstring()
.
Reason: if the internal collector fails to free unused memory (like your 2 lines of input).
Even if you are using the lightweight user data functionality (which would be overkill in this case), you will have to use it lua_pushstring()
later when the Lua program asks for a string.
a source to share
Hmm .. You could of course write some C functions to get the job done on the C side, but as another answer points out, you can get stuck hitting a line or parts of it anyway.
Note: Lua stores strings only once, when they are inserted. ie: if I have 1 line containing "A quick brown fox jumping over a lazy dog" and I push it into Lua and there are no other string objects that contain that line, it will create a new copy. If, on the other hand, I have already inserted it, you just get a pointer to that first row so that equality checks are pretty cheap. Importing these lines can be a little expensive, I would guess if done at a high frequency, however comparisons are again cheap.
I would try profiling what you are implementing and see if the performance meets your expectations or not.
As Lua Performance Tips (which I recommend reading if you are thinking about maximizing performance with Lua), two related programming for optimization are:
Rule # 1: Don't do this.
Rule # 2: Don't do it yet. (for experts only)
a source to share