lua-users home
lua-l archive

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

> These usedata are small chunks of memory (a long) allocated at program start
instead of requesting a table from the Lua memory. In terms of memory
I am not sure that allocating as many tables as constants is deeply different
from allocating as many userdatas.

I guess I just try and keep all allocation to a minimum, but then Lua is
allocating new objects left right and centre as you skip through the code. I
dont suppose this is any different. The only other way I can think of doing it
is, ie you just have one table:

readonly = { my_pi = 3.1415927 }

        function (obj,t)
          if readonly[obj] then
            return readonly[obj]
            error ("global not found: "..obj)
        end )

        function (obj,v)
          if readonly[obj] then
            error ("global is readonly: "..obj)
            -- set the global: globals()[obj] = v ???
        end )

print (my_pi)
-- print (cant_find)
--my_pi = 1 -- error
my_pi2 = 1
print (my_pi2)

> For writing back to the C variable, you also need to keep a pointer to the C
variable and you end up with allocating memory for the table (from Lua) and
keeping the pointer: userdata saves you the table allocation here.


> If I understand correctly, you are concerned with memory fragmentation,
but -in
this case- those allocation are made early in the initialisation process and
never freed, it does consume memory, not fragment it. On the other hand, it
consumes less memory than tables.

Ah, true. To allocate a table for each constant is quite expensive. I suppose
you could put all of the constants in a table and use this as a namespace, but
then you dont have same binding.

I was curious to the output of LuaSWIG since I have been using toLua for some
time and havent got round to trying LuaSWIG out. Have you any performance
comparisons? Which is the faster binding to use? I dont suppose there is much
in it.