degesch: optimize Lua weak refs

This commit is contained in:
Přemysl Eric Janouch 2016-10-28 13:07:50 +02:00
parent 95c7ababc3
commit c8a826f016
Signed by: p
GPG Key ID: B715679E3A361BE6
1 changed files with 11 additions and 30 deletions

View File

@ -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 * static void *
lua_plugin_alloc (void *ud, void *ptr, size_t o_size, size_t n_size) 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; 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 static void
lua_plugin_push_ref (struct lua_plugin *self, void *object, lua_plugin_push_ref (struct lua_plugin *self, void *object,
struct ispect_field *field) struct ispect_field *field)
{ {
// TODO: we can definitely make a resolution table right in Lua, // We create a mapping on object type registration
// lua_plugin_reg_weak() can fill it automatically (lightud->lightud) hard_assert (lua_rawgetp (self->L, LUA_REGISTRYINDEX, field->fields));
struct lua_weak_info *info = NULL; struct lua_weak_info *info = lua_touserdata (self->L, -1);
for (size_t i = 0; i < N_ELEMENTS (lua_types); i++) lua_pop (self->L, 1);
if (lua_types[i].ispect == field->fields)
{
info = lua_types[i].info;
break;
}
hard_assert (info != NULL);
if (!field->is_list) if (!field->is_list)
{ {
@ -9924,6 +9898,9 @@ lua_plugin_push_ref (struct lua_plugin *self, void *object,
return; 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; int i = 1;
lua_newtable (self->L); lua_newtable (self->L);
LIST_FOR_EACH (struct list_header, iter, object) 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 static void
lua_plugin_reg_weak (lua_State *L, struct lua_weak_info *info, luaL_Reg *fns) 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_newmetatable (L, info->name);
luaL_setfuncs (L, fns, 0); luaL_setfuncs (L, fns, 0);
lua_plugin_reg_finish (L, info); lua_plugin_reg_finish (L, info);