From 351ccd733298e08c41937c1baf22a68e62bfeca9 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Mon, 15 May 2023 17:56:25 -0300 Subject: Towards a new implementation of arrays The array part of a table wastes too much space, due to padding. To avoid that, we need to store values in the array as something different from a TValue. Therefore, the API for table access should not assume that any value in a table lives in a *TValue. This commit is the first step to remove that assumption: functions luaH_get*, instead of returning a *TValue where the value lives, receive a *TValue where to put the value being accessed. (We still have to change the luaH_set* functions.) --- ltable.c | 30 ++++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) (limited to 'ltable.c') diff --git a/ltable.c b/ltable.c index 3c690c5f..8fd83fda 100644 --- a/ltable.c +++ b/ltable.c @@ -752,6 +752,21 @@ const TValue *luaH_getint (Table *t, lua_Integer key) { } +static int finishnodeget (const TValue *val, TValue *res) { + if (!ttisnil(val)) { + setobj(((lua_State*)NULL), res, val); + return HOK; /* success */ + } + else + return HNOTFOUND; /* could not get value */ +} + + +int luaH_getint1 (Table *t, lua_Integer key, TValue *res) { + return finishnodeget(luaH_getint(t, key), res); +} + + /* ** search function for short strings */ @@ -771,6 +786,11 @@ const TValue *luaH_getshortstr (Table *t, TString *key) { } +int luaH_getshortstr1 (Table *t, TString *key, TValue *res) { + return finishnodeget(luaH_getshortstr(t, key), res); +} + + const TValue *luaH_getstr (Table *t, TString *key) { if (key->tt == LUA_VSHRSTR) return luaH_getshortstr(t, key); @@ -782,6 +802,11 @@ const TValue *luaH_getstr (Table *t, TString *key) { } +int luaH_getstr1 (Table *t, TString *key, TValue *res) { + return finishnodeget(luaH_getstr(t, key), res); +} + + /* ** main search function */ @@ -802,6 +827,11 @@ const TValue *luaH_get (Table *t, const TValue *key) { } +int luaH_get1 (Table *t, const TValue *key, TValue *res) { + return finishnodeget(luaH_get(t, key), res); +} + + /* ** Finish a raw "set table" operation, where 'slot' is where the value ** should have been (the result of a previous "get table"). -- cgit v1.2.3-55-g6feb