From c8a826f016dcb91b108420600795358c74c05148 Mon Sep 17 00:00:00 2001 From: Přemysl Janouch Date: Fri, 28 Oct 2016 13:07:50 +0200 Subject: degesch: optimize Lua weak refs --- degesch.c | 41 +++++++++++------------------------------ 1 file changed, 11 insertions(+), 30 deletions(-) diff --git a/degesch.c b/degesch.c index c604e83..132ebab 100644 --- a/degesch.c +++ b/degesch.c @@ -9860,19 +9860,6 @@ static luaL_Reg lua_plugin_library[] = // - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -static struct lua_ispect_mapping -{ - struct ispect_field *ispect; ///< Type info - struct lua_weak_info *info; ///< Lua-specific additions -} -lua_types[] = -{ - { g_user_ispect, &lua_user_info }, - { g_channel_ispect, &lua_channel_info }, - { g_buffer_ispect, &lua_buffer_info }, - { g_server_ispect, &lua_server_info }, -}; - static void * lua_plugin_alloc (void *ud, void *ptr, size_t o_size, size_t n_size) { @@ -9896,27 +9883,14 @@ lua_plugin_panic (lua_State *L) return 0; } -// As a rule in this codebase, these fields are right at the top of structs -struct list_header -{ - void *next; ///< Next item - void *prev; ///< Previous item -}; - static void lua_plugin_push_ref (struct lua_plugin *self, void *object, struct ispect_field *field) { - // TODO: we can definitely make a resolution table right in Lua, - // lua_plugin_reg_weak() can fill it automatically (lightud->lightud) - struct lua_weak_info *info = NULL; - for (size_t i = 0; i < N_ELEMENTS (lua_types); i++) - if (lua_types[i].ispect == field->fields) - { - info = lua_types[i].info; - break; - } - hard_assert (info != NULL); + // We create a mapping on object type registration + hard_assert (lua_rawgetp (self->L, LUA_REGISTRYINDEX, field->fields)); + struct lua_weak_info *info = lua_touserdata (self->L, -1); + lua_pop (self->L, 1); if (!field->is_list) { @@ -9924,6 +9898,9 @@ lua_plugin_push_ref (struct lua_plugin *self, void *object, return; } + // As a rule in this codebase, these fields are right at the top of structs + struct list_header { LIST_HEADER (void) }; + int i = 1; lua_newtable (self->L); LIST_FOR_EACH (struct list_header, iter, object) @@ -10097,6 +10074,10 @@ lua_plugin_reg_meta (lua_State *L, const char *name, luaL_Reg *fns) static void lua_plugin_reg_weak (lua_State *L, struct lua_weak_info *info, luaL_Reg *fns) { + // Create a mapping from the object type back to our metadata + lua_pushlightuserdata (L, info); + lua_rawsetp (L, LUA_REGISTRYINDEX, info->ispect); + luaL_newmetatable (L, info->name); luaL_setfuncs (L, fns, 0); lua_plugin_reg_finish (L, info); -- cgit v1.2.3-70-g09d2