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
footprint,
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 }

settagmethod(tag(nil),'getglobal',
        function (obj,t)
          if readonly[obj] then
            return readonly[obj]
          else
            error ("global not found: "..obj)
          end
        end )

settagmethod(tag(nil),'setglobal',
        function (obj,v)
          if readonly[obj] then
            error ("global is readonly: "..obj)
          else
            -- set the global: globals()[obj] = v ???
          end
        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
for
keeping the pointer: userdata saves you the table allocation here.

True.

> 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
also
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.

Regards,
Nick