lua-users home
lua-l archive

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


On Sun, Oct 12, 2008 at 11:20 PM, Canute Bigler <biglerc@cox.net> wrote:
> I have need to provide a mapping of "names" to integer values on a
> reasonably large scale (hundreds of thousands).  The name to number mapping
> is known at the compile time of my host program (C++). My basic naive
> approach is to generate a lua source file with the mapping in a table and
> load that script file into the host environment at run-time.  This approach
> has a substantial time cost to it and seems like it would be more or less
> unnecessary since the data is static, at least for that particular version
> of the program.
>
>
> Example:
> field = {
>   Field1 = 12345,
>   Field2 = 45678,
>   -- thousands of additional fields and subtables.
> }
>
> -- my intended usage.  setData is an exposed API function that takes an
> integer field value and some data to set it to
> setData(field.Field1, "abc")
>
>
> I'm looking for some advice about how I could have this table of mappings
> available in my lua environment without having to pay the cost of loading
> the script every time my program runs.

Perhaps metatables would help here? Something along the lines of

  field = setmetatable({}, { __index = c_function_to_get_int_by_string; })

with "c_function_to_get_int_by_string" to fetch data from C-side constant map.

Also when generating large constant Lua tables, keep in mind limit on
the number of constants in chunk:

http://lua-users.org/lists/lua-l/2008-02/msg00257.html

HTH,
Alexander.