lua-users home
lua-l archive

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


On Tue, Nov 13, 2012 at 6:35 AM, Rena <hyperhacker@gmail.com> wrote:
> Recently I was thinking about how you might add a 64-bit integer type to Lua.
>
> As I understand it, the existing primitive types such as number and
> light userdata have a bit of a speed advantage over others, because
> they're small enough to be stored directly on the stack or in a
> register; that means one less level of indirection, plus no time
> needed to allocate memory and collect them.
>
> It'd be nice if we could have this advantage for our in64 types as
> well, since they're only 8 bytes, and using full userdata for them
> incurs some penalties in performance (memory has to be allocated and
> freed, memory fragmentation can be an issue) and memory use (each
> instance needs at least a pointer to its memory block and a pointer to
> its metatable, so already the size triples, plus whatever overhead for
> the allocator to keep track of those blocks).
>
> A tempting idea is to just cast your int64 to void* and use light
> userdata, and there's a module that does this:
> https://github.com/cloudwu/lua-int64
> but this has a few obvious issues:
> -Requires that sizeof(void*) == sizeof(int64)
> -int64 values share the same metatable as all other light userdata, so
> accidentally invoking a method intended for some other object that
> expects a pointer will lead to a segfault
> -no real way to tell if a value is intended to be a pointer or not
>
> It seems like these issues could be avoided, if there were a way to
> create more "primitive" types, that function like light userdata. Each
> type having its own metatable and data size (up to 8 bytes?). You
> might have int64, uint64, pointers to different things?
>
> --
> Sent from my Game Boy.
>

If this is what you want, you should be using LuaJIT. It can do
exactly this, and it's not limited to just simple numeric primitives.
You can use almost any C type more or less directly. And as for 64-bit
integers, LuaJIT even has syntax extensions for defining signed and
unsigned 64-bit integer literals.

I'm not certain that adding something like this to vanilla Lua itself
is necessarily a good idea.

/s/ Adam