webmcp
changeset 200:035b58aa430a
Improve performance of pairs and ipairs in JSON library
author | jbe |
---|---|
date | Thu Aug 14 01:11:24 2014 +0200 (2014-08-14) |
parents | 9fd7e1bf9fe3 |
children | 1a10fef86d97 |
files | libraries/json/json.c |
line diff
1.1 --- a/libraries/json/json.c Tue Aug 12 01:01:54 2014 +0200 1.2 +++ b/libraries/json/json.c Thu Aug 14 01:11:24 2014 +0200 1.3 @@ -865,12 +865,7 @@ 1.4 static int json_pairs_iterfunc(lua_State *L) { 1.5 // stack shall contain two function arguments: 1.6 lua_settop(L, 2); 1.7 - // replace first argument with its shadow table 1.8 - // or throw error if no shadow table is found: 1.9 - json_getshadow(L, 1); 1.10 - if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found"); 1.11 - lua_replace(L, 1); 1.12 - // get next key value pair from shadow table (using previous key from argument 2) 1.13 + // get next key value pair from shadow table (argument 1) using previous key (argument 2) 1.14 // and return nothing if there is no next pair: 1.15 if (!lua_next(L, 1)) return 0; 1.16 // replace null-marker with nil: 1.17 @@ -887,9 +882,10 @@ 1.18 static int json_pairs(lua_State *L) { 1.19 // require one argument to function 1.20 luaL_checkany(L, 1); 1.21 - // return triple of function json_pairs_iterfunc, first argument, and nil: 1.22 + // return triple of function json_pairs_iterfunc, shadow table of first argument, and nil: 1.23 lua_pushcfunction(L, json_pairs_iterfunc); 1.24 - lua_pushvalue(L, 1); 1.25 + json_getshadow(L, 1); 1.26 + if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found"); 1.27 lua_pushnil(L); 1.28 return 3; 1.29 } 1.30 @@ -901,20 +897,16 @@ 1.31 lua_settop(L, 2); 1.32 // calculate new index by incrementing second argument: 1.33 idx = lua_tointeger(L, 2) + 1; 1.34 - // push shadow table onto stack position 3 1.35 - // or throw error if no shadow table is found: 1.36 - json_getshadow(L, 1); 1.37 - if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found"); 1.38 - // do integer lookup in shadow table and store result on stack position 4: 1.39 - lua_rawgeti(L, 3, idx); 1.40 + // do integer lookup in shadow table and store result on stack position 3: 1.41 + lua_rawgeti(L, 1, idx); 1.42 // return nothing if there was no value: 1.43 - if (lua_isnil(L, 4)) return 0; 1.44 + if (lua_isnil(L, 3)) return 0; 1.45 // return new index and 1.46 // either the looked up value if it is not equal to the null-marker 1.47 // or nil instead of null-marker: 1.48 lua_pushinteger(L, idx); 1.49 - if (json_isnullmark(L, 4)) lua_pushnil(L); 1.50 - else lua_pushvalue(L, 4); 1.51 + if (json_isnullmark(L, 3)) lua_pushnil(L); 1.52 + else lua_pushvalue(L, 3); 1.53 return 2; 1.54 } 1.55 1.56 @@ -923,9 +915,10 @@ 1.57 static int json_ipairs(lua_State *L) { 1.58 // require one argument to function 1.59 luaL_checkany(L, 1); 1.60 - // return triple of function json_ipairs_iterfunc, first argument, and zero: 1.61 + // return triple of function json_ipairs_iterfunc, shadow table of first argument, and zero: 1.62 lua_pushcfunction(L, json_ipairs_iterfunc); 1.63 - lua_pushvalue(L, 1); 1.64 + json_getshadow(L, 1); 1.65 + if (lua_isnil(L, -1)) return luaL_error(L, "Shadow table not found"); 1.66 lua_pushinteger(L, 0); 1.67 return 3; 1.68 }