lua-users home
lua-l archive

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


Jonathan Castello schrieb:
> You can mitigate the cost a little by creating the functions
> themselves outside the constructor function, and creating functions
> inside that simply call those functions. You save the cost of
> recreating each function's bytecode by saving the functions in one
> place; comparably, the "thunk" functions are a relatively minor
> overhead as they simply do the work of passing the specific object to
> the functions:

I was worried about duplicated bytecode myself, but it looks like Lua
doesn't copy the bytecode when creating a new closure, it just links to
the upvalues and has a pointer to a single "function prototype"
containing the bytecode and the constants.

Source (Lua 5.1.4):
lvm.c, Lines 719 - 738
lobject.h, Lines 228 - 312
lfunc.c, Lines 33 - 41

...assuming I understood the code correctly.

So you wouldn't need to use small wrapper closures. (But I think I did
this myself in some Lua and JavaScript projects as well).

- David