[Date Prev][Date Next][Thread Prev][Thread Next]
[Date Index]
[Thread Index]
- Subject: Re: Lua binding: How to implement C enum
- From: Javier Guerra Giraldez <javier@...>
- Date: Thu, 15 Dec 2011 17:25:00 -0500
i agree that the most Lua way of representing enum constants is to use
constant strings. but, for the C side of my brain, that seems big,
heavy and slow. of course, in fact they're not, since
already-existing Lua strings are just pointers, which move and compare
_very_ quickly.
the real problem, then, is how to return those constants from C to
Lua. in the vast majority of cases, just creating the string value is
quick enough.
still, when i wrote my libpcap binding, i wanted to create
nicely-keyed tables for each network package captured and interpreted.
I found that pushing the key strings took a significant time, so i
benchmarked a few alternatives:
- standard: creating/pushing strings
- refvalues: i stored the strings in refvals and copied from there.
- upvalues: pre-stored the strings in upvalues of the C function
- numeric: forget about strings, use (integer) numbers as keys
i don't have the results at hand, but they came in that order:
standard > refvalues > upvalues > numeric (in time spent, so standard
is the slowest, numeric is the fastest). the biggest difference was
from standard to refvalues, the rest was barely noticeable in the
finished routine.
still, note that this only make any difference when reusing tables.
allocating a new table per packet was a much slower operation and made
all string-pushing methods perform similarly.
--
Javier