Skip to content

Commit

Permalink
Remove Var weak table
Browse files Browse the repository at this point in the history
It's expensive in time and space for what should be an infrequently
used function.
  • Loading branch information
jkl1337 committed Feb 28, 2024
1 parent 59cb4b3 commit 534db83
Showing 1 changed file with 4 additions and 41 deletions.
45 changes: 4 additions & 41 deletions luakiwi/luakiwi.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -227,15 +227,6 @@ VariableData* var_register(lua_State* L, VariableData* var) {
lua_rawgeti(L, lua_upvalueindex(1), MEM_ERR_MSG);
lua_error(L);
}
#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
// a true compatibility shim has performance implications here
lua_pushlightuserdata(L, var);
lua_pushvalue(L, -2);
lua_rawset(L, lua_upvalueindex(2));
#else
lua_pushvalue(L, -1);
lua_rawsetp(L, lua_upvalueindex(2), var);
#endif
return var;
}

Expand Down Expand Up @@ -667,16 +658,8 @@ int lkiwi_term_m_index(lua_State* L) {
size_t len;
const char* k = lua_tolstring(L, 2, &len);
if (len == 3 && memcmp("var", k, len) == 0) {
#if defined(LUA_VERSION_NUM) && LUA_VERSION_NUM == 501
lua_pushlightuserdata(L, term->var);
lua_rawget(L, lua_upvalueindex(2));
#else
lua_rawgetp(L, lua_upvalueindex(2), term->var);
#endif
if (lua_isnil(L, -1)) {
auto* varp = var_new(L);
var_register(L, *varp = retain_unmanaged(term->var));
}
auto* varp = var_new(L);
var_register(L, *varp = retain_unmanaged(term->var));
return 1;
} else if (len == 11 && memcmp("coefficient", k, len) == 0) {
lua_pushnumber(L, term->coefficient);
Expand All @@ -699,7 +682,7 @@ constexpr const struct luaL_Reg kiwi_term_m[] = {
{"__unm", lkiwi_term_m_unm},
{"__tostring", lkiwi_term_m_tostring},
{"__gc", lkiwi_term_m_gc},
{"__index", 0},
{"__index", lkiwi_term_m_index},
{"toexpr", lkiwi_term_toexpr},
{"value", lkiwi_term_value},
{"eq", lkiwi_eq},
Expand Down Expand Up @@ -1544,7 +1527,7 @@ int lkiwi_is_error(lua_State* L) {
}

constexpr const struct luaL_Reg lkiwi[] = {
{"Var", 0},
{"Var", lkiwi_var_new},
{"is_var", lkiwi_is_var},
{"Term", lkiwi_term_new},
{"is_term", lkiwi_is_term},
Expand Down Expand Up @@ -1653,26 +1636,6 @@ extern "C" LJKIWI_EXPORT int luaopen_ljkiwi(lua_State* L) {
lua_pushvalue(L, ctx_i);
setfuncs(L, lkiwi, 1);

/* var weak table */
/* set as upvalue for selected functions */
lua_createtable(L, 0, 0);
lua_createtable(L, 0, 1);
lua_pushstring(L, "v");
lua_setfield(L, -2, "__mode");
lua_setmetatable(L, -2);

lua_pushvalue(L, ctx_i);
lua_pushvalue(L, -2);
lua_pushcclosure(L, lkiwi_var_new, 2);
lua_setfield(L, -3, "Var");

lua_rawgeti(L, ctx_i, TERM);
lua_pushvalue(L, ctx_i);
lua_pushvalue(L, -3);
lua_pushcclosure(L, lkiwi_term_m_index, 2);
lua_setfield(L, -2, "__index");
lua_pop(L, 2); // TERM mt and var weak table

/* ErrKind table */
/* TODO: implement __call metamethod for these */
lua_createtable(L, array_count(lkiwi_error_kinds) + 1, array_count(lkiwi_error_kinds));
Expand Down

0 comments on commit 534db83

Please sign in to comment.