From 73f4b8f172a1eb0d68bf64af149fabdf82212de4 Mon Sep 17 00:00:00 2001 From: Damjan Adamic Date: Wed, 26 Apr 2017 21:45:46 +0200 Subject: [PATCH] Projectkk2glider/issue 4827 (#4855) * Reverting a part of f94b4c743d6720641f985790d71d99d976b831c9 that deals with the size of table hash. Fixes 4827, but uses more memory and CPU. * New internal Lua traces --- radio/src/debug.h | 10 ++++++++++ radio/src/thirdparty/Lua/src/lobject.h | 4 ++-- radio/src/thirdparty/Lua/src/lparser.c | 2 ++ radio/src/thirdparty/Lua/src/ltable.c | 17 ++++++++--------- 4 files changed, 22 insertions(+), 11 deletions(-) diff --git a/radio/src/debug.h b/radio/src/debug.h index 41cdfda30..90aca3d88 100644 --- a/radio/src/debug.h +++ b/radio/src/debug.h @@ -70,8 +70,18 @@ extern "C" { #if defined(TRACE_LUA_INTERNALS_ENABLED) #define TRACE_LUA_INTERNALS(f_, ...) debugPrintf(("[LUA INT] " f_ "\r\n"), ##__VA_ARGS__) + + #define TRACE_LUA_INTERNALS_WITH_LINEINFO(L, f_, ...) do { \ + lua_Debug ar; \ + if (lua_getstack(L, 1, &ar)) { \ + lua_getinfo(L, ">Sl", &ar); \ + debugPrintf("%s:%d: ", ar.short_src, ar.currentline); \ + } \ + debugPrintf(("[LUA INT] " f_ "\r\n"), ##__VA_ARGS__); \ + } while(0) #else #define TRACE_LUA_INTERNALS(...) + #define TRACE_LUA_INTERNALS_WITH_LINEINFO(L, f_, ...) #endif #if defined(DEBUG) && !defined(SIMU) diff --git a/radio/src/thirdparty/Lua/src/lobject.h b/radio/src/thirdparty/Lua/src/lobject.h index 7e5c03ab4..919b2c304 100644 --- a/radio/src/thirdparty/Lua/src/lobject.h +++ b/radio/src/thirdparty/Lua/src/lobject.h @@ -569,7 +569,7 @@ typedef struct Node { typedef struct Table { CommonHeader; lu_byte flags; /* 1<

lsizenode) +#define sizenode(t) (twoto((t)->lsizenode)) /* diff --git a/radio/src/thirdparty/Lua/src/lparser.c b/radio/src/thirdparty/Lua/src/lparser.c index d8f5b4ffc..43a8967ff 100644 --- a/radio/src/thirdparty/Lua/src/lparser.c +++ b/radio/src/thirdparty/Lua/src/lparser.c @@ -181,6 +181,7 @@ static void new_localvar (LexState *ls, TString *name) { int reg = registerlocalvar(ls, name); checklimit(fs, dyd->actvar.n + 1 - fs->firstlocal, MAXVARS, "local variables"); + TRACE_LUA_INTERNALS("new_localvar %s %d", getstr(name), dyd->actvar.n + 1 - fs->firstlocal); luaM_growvector(ls->L, dyd->actvar.arr, dyd->actvar.n + 1, dyd->actvar.size, Vardesc, MAX_INT, "local variables"); dyd->actvar.arr[dyd->actvar.n++].idx = cast(short, reg); @@ -1529,6 +1530,7 @@ static void retstat (LexState *ls) { static void statement (LexState *ls) { int line = ls->linenumber; /* may be needed for error messages */ enterlevel(ls); + TRACE_LUA_INTERNALS("statement %02x at line %d", ls->t.token, line); switch (ls->t.token) { case ';': { /* stat -> ';' (empty statement) */ luaX_next(ls); /* skip ';' */ diff --git a/radio/src/thirdparty/Lua/src/ltable.c b/radio/src/thirdparty/Lua/src/ltable.c index 6cea6e5e2..902e09fae 100644 --- a/radio/src/thirdparty/Lua/src/ltable.c +++ b/radio/src/thirdparty/Lua/src/ltable.c @@ -282,16 +282,14 @@ static void setnodevector (lua_State *L, Table *t, int size) { int lsize; if (size == 0) { /* no elements to hash part? */ t->node = cast(Node *, dummynode); /* use common `dummynode' */ - lsize = 1; + lsize = 0; } else { int i; - // lsize = luaO_ceillog2(size); - // if (lsize > MAXBITS) - // luaG_runerror(L, "table overflow"); - // size = twoto(lsize); - // size += size/2; - lsize = size; + lsize = luaO_ceillog2(size); + if (lsize > MAXBITS) + luaG_runerror(L, "table overflow"); + size = twoto(lsize); t->node = luaM_newvector(L, size, Node); for (i=0; isizearray; int oldhsize = t->lsizenode; + TRACE_LUA_INTERNALS_WITH_LINEINFO(L, "luaH_resize table %p %d(%d) %d(%d)", t, nasize, oldasize, nhsize, twoto(oldhsize)); Node *nold = t->node; /* save old hash ... */ if (nasize > oldasize) /* array part must grow? */ setarrayvector(L, t, nasize); @@ -324,7 +323,7 @@ void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) { luaM_reallocvector(L, t->array, oldasize, nasize, TValue); } /* re-insert elements from hash part */ - for (i = oldhsize - 1; i >= 0; i--) { + for (i = twoto(oldhsize) - 1; i >= 0; i--) { Node *old = nold+i; if (!ttisnil(gval(old))) { /* doesn't need barrier/invalidate cache, as entry was @@ -333,7 +332,7 @@ void luaH_resize (lua_State *L, Table *t, int nasize, int nhsize) { } } if (!isdummy(nold)) - luaM_freearray(L, nold, cast(size_t, oldhsize)); /* free old array */ + luaM_freearray(L, nold, cast(size_t, twoto(oldhsize))); /* free old array */ }