I have the following function that simply returns its string argument:
function N_(s)
return s
end
You may recognize this function name from gettext. It's a marking for the benefit of the .pot extractor only.
Would it be more efficient to implement N_() in C or in Lua?
Can you give me a rule of thumb?
Another question:
Let's say I have a function a bit more complicated:
function Z_(s)
return dict[s] or s
end
Would it be more efficient to implement Z_() in C or in Lua?
(I'll be calling both N_() and Z_() from Lua code, not from C code.)
Functions implemented in Lua that aren't very processor-intensive are very likely to be more efficient than those in C, if you're spending most of your time in Lua. This is especially true if you end up using LuaJIT at some time in the future. The compiler will always be able to infer more information from your function if it's in the language than if it's some obscure C function which it doesn't know anything about, and it doesn't have to do as much hopping around different contexts.
The problem with implementing
Z_
in C is that you would also have to implementdict
in C, where Lua already has such functionality in place. So, it depends if you need some specially optimized hash map, maybe C would be better.If your concern is about function call efficiency, why not just cache the results?
Of course, your
Z_
function doesn't look like it takes substitutions yet, but I assume it might later. However, if you never need substitutions, you may as well make it a table like this:This table has an
__index
metamethod which returns the key used, if the table does not have such an entry.