From dcff9b85a3cef778af60ad4426f91262ba815931 Mon Sep 17 00:00:00 2001 From: William Wilgus Date: Tue, 11 May 2021 21:35:41 -0400 Subject: [PATCH] lua latebound function update return the nextfunction and nil instead of pairs it allows a faster return to lua rather than calling the lua function pcall(fnpairs) from c and returning the result back into lua to kick off the search yeah, no clue why I didn't realize that before.. testing in x86 and ARM.. its more RAM efficient to do the initial creation of the stack in lua code for the __pairs functon its not faster but being that its a one time hit per iter creation the reduced churn alone should be worth it along with a reduced peak RAM usage fix bug where a failed module can not be reloaded optimize filetol fix potential bug in splash scroller when no break character is found Change-Id: I42c922e07039a19138b97c0d0e80cf3cf2426471 --- apps/plugins/lua/lauxlib.c | 37 ++++++++++++++++++++++--------------- apps/plugins/lua/loadlib.c | 4 +++- apps/plugins/lua/lparser.c | 5 +++++ apps/plugins/lua/rockaux.c | 34 +++++++++++++++++++++------------- 4 files changed, 51 insertions(+), 29 deletions(-) diff --git a/apps/plugins/lua/lauxlib.c b/apps/plugins/lua/lauxlib.c index acd7e0e636..b8332427f0 100644 --- a/apps/plugins/lua/lauxlib.c +++ b/apps/plugins/lua/lauxlib.c @@ -268,7 +268,8 @@ static int latebind_func_index(lua_State *L) const char *name = lua_tostring(L, -1); lua_pushstring (L, "__latebind");/* basetable;name;__latebind;*/ - lua_rawget (L, -3);/* basetable;name;__latebind(t);*/ + if(lua_istable(L, -3)) + lua_rawget (L, -3);/* basetable;name;__latebind(t);*/ luaL_argcheck(L, lua_istable(L, -3) && lua_istable(L, -1), 1, "__latebind table expected"); @@ -324,25 +325,31 @@ static int latebind_func_pairs(lua_State *L) { /* basetable @ top of stack 1(basetable)-1 */ luaL_argcheck(L, lua_istable(L, 1), 1, "table expected"); - lua_getglobal(L, "pairs"); /* function to be called / returned (btable;pairs) */ - lua_createtable(L, 0, 15); /* btable;pairs;newtable; */ - /* clone base table */ +#if 0 + lua_getglobal(L, "next"); /* function to be called / returned (btable;next) */ + lua_createtable(L, 0, 15); /* btable;next;newtable; */ + lua_pushnil(L); /* nil name retrieves all unbound latebound functions */ lua_pushnil(L); /* first key */ - while(lua_next(L, 1) != 0) { - /* (btable;pairs;ntable;k;v) */ +#else + /* this way is more RAM efficient in testing */ + if(luaL_dostring(L, "return next, {}, nil, nil")!= 0) + lua_error(L); +#endif + /* (btable;next;ntable;nil;nil) */ + /* clone base table */ + while(lua_next(L, 1) > 0) { + /* (btable;next;ntable;nil;k;v) */ lua_pushvalue(L, -2); /* dupe key Stk = (..;k;v -> ..k;v;k)*/ lua_insert(L, -2); /* Stk = (..k;k;v) */ - lua_rawset(L, 3); /* btable;pairs;ntable;k */ + lua_rawset(L, -5); /* btable;next;ntable;nil;k */ } - - lua_pushnil(L); /*nil name retrieves all unbound late bound functions */ - latebind_func_index(L);/* (btable;pairs;ntable;nil) -> (btable;pairs;ntable) */ - - /* (btable;pairs;ntable) */ - lua_call(L, 1, 3); /* pairs(ntable) -> (btable;iter;state;value) */ - - return 3; + /* fill the new table with all the latebound functions */ + /* nil name retrieves all unbound latebound functions */ + latebind_func_index(L);/* (btable;next;ntable;nil) -> (btable;next;ntable) */ + lua_pushnil(L); /*nil initial key for next*/ + /* stack = (btable;next;ntable;nil) */ + return 3; /*(next,ntable,nil)*/ } diff --git a/apps/plugins/lua/loadlib.c b/apps/plugins/lua/loadlib.c index 1e310beed1..732ad707b5 100644 --- a/apps/plugins/lua/loadlib.c +++ b/apps/plugins/lua/loadlib.c @@ -130,9 +130,11 @@ static int ll_require (lua_State *L) { lua_pushliteral(L, ""); /* error message accumulator */ for (i=1; ; i++) { lua_rawgeti(L, -2, i); /* get a loader */ - if (lua_isnil(L, -1)) + if (lua_isnil(L, -1)) { + lua_setfield(L, 2, name); /* _LOADED[name] = nil */ luaL_error(L, "module " LUA_QS " not found:%s", name, lua_tostring(L, -2)); + } lua_pushstring(L, name); lua_call(L, 1, 1); /* call it */ if (lua_isfunction(L, -1)) /* did it find module? */ diff --git a/apps/plugins/lua/lparser.c b/apps/plugins/lua/lparser.c index 8b93237918..23d3972036 100644 --- a/apps/plugins/lua/lparser.c +++ b/apps/plugins/lua/lparser.c @@ -603,7 +603,12 @@ static void parlist (LexState *ls) { } while (!f->is_vararg && testnext(ls, ',')); } adjustlocalvars(ls, nparams); + //f->numparams = cast_byte(fs->nactvar - (f->is_vararg & VARARG_HASARG)); +#if defined(LUA_COMPAT_VARARG) f->numparams = cast_byte(fs->nactvar - (f->is_vararg & VARARG_HASARG)); +#else + f->numparams = cast_byte(fs->nactvar); +#endif luaK_reserveregs(fs, fs->nactvar); /* reserve register for parameters */ } diff --git a/apps/plugins/lua/rockaux.c b/apps/plugins/lua/rockaux.c index 058ad313fa..929dea798b 100644 --- a/apps/plugins/lua/rockaux.c +++ b/apps/plugins/lua/rockaux.c @@ -106,7 +106,8 @@ int splash_scroller(int timeout, const char* str) { brk = strpbrk_n(ch+1, max_ch, break_chars); chars_next_break = (brk - ch); - if (chars_next_break < 2 || w + (ch_w * chars_next_break) > max_w) + if (brk && + (chars_next_break < 2 || w + (ch_w * chars_next_break) > max_w)) { if (!isprint(line[linepos])) { @@ -226,7 +227,7 @@ int get_current_path(lua_State *L, int level) } } - lua_pushnil(L); + lua_pushnil(L); return 1; } @@ -250,25 +251,32 @@ int filetol(int fd, long *num) while (rb->read(fd, &chbuf, 1) == 1) { - if(!isspace(chbuf) || retn == 1) + if(retn || !isspace(chbuf)) { - if(chbuf == '0') /* strip preceeding zeros */ + switch(chbuf) { - *num = 0; - retn = 1; - } - else if(chbuf == '-' && retn != 1) - neg = true; - else - { - rb->lseek(fd, -1, SEEK_CUR); - break; + case '-': + { + if (retn) /* 0 preceeds, this negative sign must be in error */ + goto get_digits; + neg = true; + continue; + } + case '0': /* strip preceeding zeros */ + { + *num = 0; + retn = 1; + continue; + } + default: + goto get_digits; } } } while (rb->read(fd, &chbuf, 1) == 1) { +get_digits: if(!isdigit(chbuf)) { rb->lseek(fd, -1, SEEK_CUR);