diff options
| author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2023-05-15 17:56:25 -0300 |
|---|---|---|
| committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2023-05-15 17:56:25 -0300 |
| commit | 351ccd733298e08c41937c1baf22a68e62bfeca9 (patch) | |
| tree | be290db6b41e949c74d6699c7a01963c7cec9b21 /ltable.c | |
| parent | 6443185167c77adcc8552a3fee7edab7895db1a9 (diff) | |
| download | lua-351ccd733298e08c41937c1baf22a68e62bfeca9.tar.gz lua-351ccd733298e08c41937c1baf22a68e62bfeca9.tar.bz2 lua-351ccd733298e08c41937c1baf22a68e62bfeca9.zip | |
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.)
Diffstat (limited to 'ltable.c')
| -rw-r--r-- | ltable.c | 30 |
1 files changed, 30 insertions, 0 deletions
| @@ -752,6 +752,21 @@ const TValue *luaH_getint (Table *t, lua_Integer key) { | |||
| 752 | } | 752 | } |
| 753 | 753 | ||
| 754 | 754 | ||
| 755 | static int finishnodeget (const TValue *val, TValue *res) { | ||
| 756 | if (!ttisnil(val)) { | ||
| 757 | setobj(((lua_State*)NULL), res, val); | ||
| 758 | return HOK; /* success */ | ||
| 759 | } | ||
| 760 | else | ||
| 761 | return HNOTFOUND; /* could not get value */ | ||
| 762 | } | ||
| 763 | |||
| 764 | |||
| 765 | int luaH_getint1 (Table *t, lua_Integer key, TValue *res) { | ||
| 766 | return finishnodeget(luaH_getint(t, key), res); | ||
| 767 | } | ||
| 768 | |||
| 769 | |||
| 755 | /* | 770 | /* |
| 756 | ** search function for short strings | 771 | ** search function for short strings |
| 757 | */ | 772 | */ |
| @@ -771,6 +786,11 @@ const TValue *luaH_getshortstr (Table *t, TString *key) { | |||
| 771 | } | 786 | } |
| 772 | 787 | ||
| 773 | 788 | ||
| 789 | int luaH_getshortstr1 (Table *t, TString *key, TValue *res) { | ||
| 790 | return finishnodeget(luaH_getshortstr(t, key), res); | ||
| 791 | } | ||
| 792 | |||
| 793 | |||
| 774 | const TValue *luaH_getstr (Table *t, TString *key) { | 794 | const TValue *luaH_getstr (Table *t, TString *key) { |
| 775 | if (key->tt == LUA_VSHRSTR) | 795 | if (key->tt == LUA_VSHRSTR) |
| 776 | return luaH_getshortstr(t, key); | 796 | return luaH_getshortstr(t, key); |
| @@ -782,6 +802,11 @@ const TValue *luaH_getstr (Table *t, TString *key) { | |||
| 782 | } | 802 | } |
| 783 | 803 | ||
| 784 | 804 | ||
| 805 | int luaH_getstr1 (Table *t, TString *key, TValue *res) { | ||
| 806 | return finishnodeget(luaH_getstr(t, key), res); | ||
| 807 | } | ||
| 808 | |||
| 809 | |||
| 785 | /* | 810 | /* |
| 786 | ** main search function | 811 | ** main search function |
| 787 | */ | 812 | */ |
| @@ -802,6 +827,11 @@ const TValue *luaH_get (Table *t, const TValue *key) { | |||
| 802 | } | 827 | } |
| 803 | 828 | ||
| 804 | 829 | ||
| 830 | int luaH_get1 (Table *t, const TValue *key, TValue *res) { | ||
| 831 | return finishnodeget(luaH_get(t, key), res); | ||
| 832 | } | ||
| 833 | |||
| 834 | |||
| 805 | /* | 835 | /* |
| 806 | ** Finish a raw "set table" operation, where 'slot' is where the value | 836 | ** Finish a raw "set table" operation, where 'slot' is where the value |
| 807 | ** should have been (the result of a previous "get table"). | 837 | ** should have been (the result of a previous "get table"). |
