diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-12-29 14:23:32 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2005-12-29 14:23:32 -0200 |
commit | bfdcbbcd76c7187022fe2d35675de0b1c92eeadf (patch) | |
tree | e312b2b1d54e4854693c2e410fb6f0f9bd37c84d | |
parent | 30eebb2d1cc2cdd14426b9e80a9e1c576b955f6b (diff) | |
download | lua-bfdcbbcd76c7187022fe2d35675de0b1c92eeadf.tar.gz lua-bfdcbbcd76c7187022fe2d35675de0b1c92eeadf.tar.bz2 lua-bfdcbbcd76c7187022fe2d35675de0b1c92eeadf.zip |
small optimizations (lua_newtable -> lua_createtable)
-rw-r--r-- | lauxlib.c | 18 | ||||
-rw-r--r-- | lauxlib.h | 4 | ||||
-rw-r--r-- | lbaselib.c | 4 | ||||
-rw-r--r-- | ldblib.c | 6 | ||||
-rw-r--r-- | linit.c | 4 | ||||
-rw-r--r-- | liolib.c | 8 | ||||
-rw-r--r-- | loadlib.c | 10 | ||||
-rw-r--r-- | lstrlib.c | 4 | ||||
-rw-r--r-- | lua.c | 4 |
9 files changed, 35 insertions, 27 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.c,v 1.155 2005/10/20 11:35:25 roberto Exp roberto $ | 2 | ** $Id: lauxlib.c,v 1.156 2005/10/21 13:47:42 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -226,16 +226,24 @@ LUALIB_API void (luaL_register) (lua_State *L, const char *libname, | |||
226 | } | 226 | } |
227 | 227 | ||
228 | 228 | ||
229 | static int libsize (const luaL_Reg *l) { | ||
230 | int size = 0; | ||
231 | for (; l->name; l++) size++; | ||
232 | return size; | ||
233 | } | ||
234 | |||
235 | |||
229 | LUALIB_API void luaI_openlib (lua_State *L, const char *libname, | 236 | LUALIB_API void luaI_openlib (lua_State *L, const char *libname, |
230 | const luaL_Reg *l, int nup) { | 237 | const luaL_Reg *l, int nup) { |
231 | if (libname) { | 238 | if (libname) { |
239 | int size = libsize(l); | ||
232 | /* check whether lib already exists */ | 240 | /* check whether lib already exists */ |
233 | luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED"); | 241 | luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", size); |
234 | lua_getfield(L, -1, libname); /* get _LOADED[libname] */ | 242 | lua_getfield(L, -1, libname); /* get _LOADED[libname] */ |
235 | if (!lua_istable(L, -1)) { /* not found? */ | 243 | if (!lua_istable(L, -1)) { /* not found? */ |
236 | lua_pop(L, 1); /* remove previous result */ | 244 | lua_pop(L, 1); /* remove previous result */ |
237 | /* try global variable (and create one if it does not exist) */ | 245 | /* try global variable (and create one if it does not exist) */ |
238 | if (luaL_findtable(L, LUA_GLOBALSINDEX, libname) != NULL) | 246 | if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL) |
239 | luaL_error(L, "name conflict for module " LUA_QS, libname); | 247 | luaL_error(L, "name conflict for module " LUA_QS, libname); |
240 | lua_pushvalue(L, -1); | 248 | lua_pushvalue(L, -1); |
241 | lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ | 249 | lua_setfield(L, -3, libname); /* _LOADED[libname] = new table */ |
@@ -331,7 +339,7 @@ LUALIB_API const char *luaL_gsub (lua_State *L, const char *s, const char *p, | |||
331 | 339 | ||
332 | 340 | ||
333 | LUALIB_API const char *luaL_findtable (lua_State *L, int idx, | 341 | LUALIB_API const char *luaL_findtable (lua_State *L, int idx, |
334 | const char *fname) { | 342 | const char *fname, int szhint) { |
335 | const char *e; | 343 | const char *e; |
336 | lua_pushvalue(L, idx); | 344 | lua_pushvalue(L, idx); |
337 | do { | 345 | do { |
@@ -341,7 +349,7 @@ LUALIB_API const char *luaL_findtable (lua_State *L, int idx, | |||
341 | lua_rawget(L, -2); | 349 | lua_rawget(L, -2); |
342 | if (lua_isnil(L, -1)) { /* no such field? */ | 350 | if (lua_isnil(L, -1)) { /* no such field? */ |
343 | lua_pop(L, 1); /* remove this nil */ | 351 | lua_pop(L, 1); /* remove this nil */ |
344 | lua_newtable(L); /* create a new table for field */ | 352 | lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */ |
345 | lua_pushlstring(L, fname, e - fname); | 353 | lua_pushlstring(L, fname, e - fname); |
346 | lua_pushvalue(L, -2); | 354 | lua_pushvalue(L, -2); |
347 | lua_settable(L, -4); /* set new table into field */ | 355 | lua_settable(L, -4); /* set new table into field */ |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lauxlib.h,v 1.85 2005/09/06 17:19:51 roberto Exp roberto $ | 2 | ** $Id: lauxlib.h,v 1.86 2005/10/21 13:47:42 roberto Exp roberto $ |
3 | ** Auxiliary functions for building Lua libraries | 3 | ** Auxiliary functions for building Lua libraries |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -86,7 +86,7 @@ LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p, | |||
86 | const char *r); | 86 | const char *r); |
87 | 87 | ||
88 | LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx, | 88 | LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx, |
89 | const char *fname); | 89 | const char *fname, int szhint); |
90 | 90 | ||
91 | 91 | ||
92 | 92 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lbaselib.c,v 1.186 2005/10/21 13:47:42 roberto Exp roberto $ | 2 | ** $Id: lbaselib.c,v 1.187 2005/12/27 17:10:11 roberto Exp roberto $ |
3 | ** Basic library | 3 | ** Basic library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -625,7 +625,7 @@ static void base_open (lua_State *L) { | |||
625 | auxopen(L, "ipairs", luaB_ipairs, ipairsaux); | 625 | auxopen(L, "ipairs", luaB_ipairs, ipairsaux); |
626 | auxopen(L, "pairs", luaB_pairs, luaB_next); | 626 | auxopen(L, "pairs", luaB_pairs, luaB_next); |
627 | /* `newproxy' needs a weaktable as upvalue */ | 627 | /* `newproxy' needs a weaktable as upvalue */ |
628 | lua_newtable(L); /* new table `w' */ | 628 | lua_createtable(L, 0, 1); /* new table `w' */ |
629 | lua_pushvalue(L, -1); /* `w' will be its own metatable */ | 629 | lua_pushvalue(L, -1); /* `w' will be its own metatable */ |
630 | lua_setmetatable(L, -2); | 630 | lua_setmetatable(L, -2); |
631 | lua_pushliteral(L, "kv"); | 631 | lua_pushliteral(L, "kv"); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: ldblib.c,v 1.102 2005/10/19 13:05:11 roberto Exp roberto $ | 2 | ** $Id: ldblib.c,v 1.103 2005/11/01 16:08:32 roberto Exp roberto $ |
3 | ** Interface from Lua to its debug API | 3 | ** Interface from Lua to its debug API |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -116,7 +116,7 @@ static int db_getinfo (lua_State *L) { | |||
116 | return luaL_argerror(L, arg+1, "function or level expected"); | 116 | return luaL_argerror(L, arg+1, "function or level expected"); |
117 | if (!lua_getinfo(L1, options, &ar)) | 117 | if (!lua_getinfo(L1, options, &ar)) |
118 | return luaL_argerror(L, arg+2, "invalid option"); | 118 | return luaL_argerror(L, arg+2, "invalid option"); |
119 | lua_newtable(L); | 119 | lua_createtable(L, 0, 2); |
120 | if (strchr(options, 'S')) { | 120 | if (strchr(options, 'S')) { |
121 | settabss(L, "source", ar.source); | 121 | settabss(L, "source", ar.source); |
122 | settabss(L, "short_src", ar.short_src); | 122 | settabss(L, "short_src", ar.short_src); |
@@ -246,7 +246,7 @@ static void gethooktable (lua_State *L) { | |||
246 | lua_rawget(L, LUA_REGISTRYINDEX); | 246 | lua_rawget(L, LUA_REGISTRYINDEX); |
247 | if (!lua_istable(L, -1)) { | 247 | if (!lua_istable(L, -1)) { |
248 | lua_pop(L, 1); | 248 | lua_pop(L, 1); |
249 | lua_newtable(L); | 249 | lua_createtable(L, 0, 1); |
250 | lua_pushlightuserdata(L, (void *)&KEY_HOOK); | 250 | lua_pushlightuserdata(L, (void *)&KEY_HOOK); |
251 | lua_pushvalue(L, -2); | 251 | lua_pushvalue(L, -2); |
252 | lua_rawset(L, LUA_REGISTRYINDEX); | 252 | lua_rawset(L, LUA_REGISTRYINDEX); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: linit.c,v 1.12 2005/08/10 18:06:58 roberto Exp roberto $ | 2 | ** $Id: linit.c,v 1.13 2005/08/26 17:36:32 roberto Exp roberto $ |
3 | ** Initialization of libraries for lua.c | 3 | ** Initialization of libraries for lua.c |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -16,13 +16,13 @@ | |||
16 | 16 | ||
17 | static const luaL_Reg lualibs[] = { | 17 | static const luaL_Reg lualibs[] = { |
18 | {"", luaopen_base}, | 18 | {"", luaopen_base}, |
19 | {LUA_LOADLIBNAME, luaopen_package}, | ||
19 | {LUA_TABLIBNAME, luaopen_table}, | 20 | {LUA_TABLIBNAME, luaopen_table}, |
20 | {LUA_IOLIBNAME, luaopen_io}, | 21 | {LUA_IOLIBNAME, luaopen_io}, |
21 | {LUA_OSLIBNAME, luaopen_os}, | 22 | {LUA_OSLIBNAME, luaopen_os}, |
22 | {LUA_STRLIBNAME, luaopen_string}, | 23 | {LUA_STRLIBNAME, luaopen_string}, |
23 | {LUA_MATHLIBNAME, luaopen_math}, | 24 | {LUA_MATHLIBNAME, luaopen_math}, |
24 | {LUA_DBLIBNAME, luaopen_debug}, | 25 | {LUA_DBLIBNAME, luaopen_debug}, |
25 | {LUA_LOADLIBNAME, luaopen_package}, | ||
26 | {NULL, NULL} | 26 | {NULL, NULL} |
27 | }; | 27 | }; |
28 | 28 | ||
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: liolib.c,v 2.68 2005/10/14 16:24:11 roberto Exp roberto $ | 2 | ** $Id: liolib.c,v 2.69 2005/10/19 13:05:11 roberto Exp roberto $ |
3 | ** Standard I/O (and system) library | 3 | ** Standard I/O (and system) library |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -507,8 +507,8 @@ static void createstdfile (lua_State *L, FILE *f, int k, const char *fname) { | |||
507 | 507 | ||
508 | LUALIB_API int luaopen_io (lua_State *L) { | 508 | LUALIB_API int luaopen_io (lua_State *L) { |
509 | createmeta(L); | 509 | createmeta(L); |
510 | /* create new (private) environment */ | 510 | /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */ |
511 | lua_newtable(L); | 511 | lua_createtable(L, 2, 1); |
512 | lua_replace(L, LUA_ENVIRONINDEX); | 512 | lua_replace(L, LUA_ENVIRONINDEX); |
513 | /* open library */ | 513 | /* open library */ |
514 | luaL_register(L, LUA_IOLIBNAME, iolib); | 514 | luaL_register(L, LUA_IOLIBNAME, iolib); |
@@ -518,7 +518,7 @@ LUALIB_API int luaopen_io (lua_State *L) { | |||
518 | createstdfile(L, stderr, 0, "stderr"); | 518 | createstdfile(L, stderr, 0, "stderr"); |
519 | /* create environment for 'popen' */ | 519 | /* create environment for 'popen' */ |
520 | lua_getfield(L, -1, "popen"); | 520 | lua_getfield(L, -1, "popen"); |
521 | lua_newtable(L); | 521 | lua_createtable(L, 0, 1); |
522 | lua_pushcfunction(L, io_pclose); | 522 | lua_pushcfunction(L, io_pclose); |
523 | lua_setfield(L, -2, "__close"); | 523 | lua_setfield(L, -2, "__close"); |
524 | lua_setfenv(L, -2); | 524 | lua_setfenv(L, -2); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: loadlib.c,v 1.49 2005/12/07 15:42:32 roberto Exp roberto $ | 2 | ** $Id: loadlib.c,v 1.50 2005/12/19 20:56:39 roberto Exp roberto $ |
3 | ** Dynamic library loader for Lua | 3 | ** Dynamic library loader for Lua |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | ** | 5 | ** |
@@ -550,7 +550,7 @@ static int ll_module (lua_State *L) { | |||
550 | if (!lua_istable(L, -1)) { /* not found? */ | 550 | if (!lua_istable(L, -1)) { /* not found? */ |
551 | lua_pop(L, 1); /* remove previous result */ | 551 | lua_pop(L, 1); /* remove previous result */ |
552 | /* try global variable (and create one if it does not exist) */ | 552 | /* try global variable (and create one if it does not exist) */ |
553 | if (luaL_findtable(L, LUA_GLOBALSINDEX, modname) != NULL) | 553 | if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != NULL) |
554 | return luaL_error(L, "name conflict for module " LUA_QS, modname); | 554 | return luaL_error(L, "name conflict for module " LUA_QS, modname); |
555 | lua_pushvalue(L, -1); | 555 | lua_pushvalue(L, -1); |
556 | lua_setfield(L, loaded, modname); /* _LOADED[modname] = new table */ | 556 | lua_setfield(L, loaded, modname); /* _LOADED[modname] = new table */ |
@@ -573,7 +573,7 @@ static int ll_module (lua_State *L) { | |||
573 | static int ll_seeall (lua_State *L) { | 573 | static int ll_seeall (lua_State *L) { |
574 | luaL_checktype(L, 1, LUA_TTABLE); | 574 | luaL_checktype(L, 1, LUA_TTABLE); |
575 | if (!lua_getmetatable(L, 1)) { | 575 | if (!lua_getmetatable(L, 1)) { |
576 | lua_newtable(L); /* create new metatable */ | 576 | lua_createtable(L, 0, 1); /* create new metatable */ |
577 | lua_pushvalue(L, -1); | 577 | lua_pushvalue(L, -1); |
578 | lua_setmetatable(L, 1); | 578 | lua_setmetatable(L, 1); |
579 | } | 579 | } |
@@ -640,7 +640,7 @@ LUALIB_API int luaopen_package (lua_State *L) { | |||
640 | lua_pushvalue(L, -1); | 640 | lua_pushvalue(L, -1); |
641 | lua_replace(L, LUA_ENVIRONINDEX); | 641 | lua_replace(L, LUA_ENVIRONINDEX); |
642 | /* create `loaders' table */ | 642 | /* create `loaders' table */ |
643 | lua_newtable(L); | 643 | lua_createtable(L, 0, sizeof(loaders)/sizeof(loaders[0]) - 1); |
644 | /* fill it with pre-defined loaders */ | 644 | /* fill it with pre-defined loaders */ |
645 | for (i=0; loaders[i] != NULL; i++) { | 645 | for (i=0; loaders[i] != NULL; i++) { |
646 | lua_pushcfunction(L, loaders[i]); | 646 | lua_pushcfunction(L, loaders[i]); |
@@ -654,7 +654,7 @@ LUALIB_API int luaopen_package (lua_State *L) { | |||
654 | LUA_EXECDIR "\n" LUA_IGMARK); | 654 | LUA_EXECDIR "\n" LUA_IGMARK); |
655 | lua_setfield(L, -2, "config"); | 655 | lua_setfield(L, -2, "config"); |
656 | /* set field `loaded' */ | 656 | /* set field `loaded' */ |
657 | luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED"); | 657 | luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2); |
658 | lua_setfield(L, -2, "loaded"); | 658 | lua_setfield(L, -2, "loaded"); |
659 | /* set field `preload' */ | 659 | /* set field `preload' */ |
660 | lua_newtable(L); | 660 | lua_newtable(L); |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lstrlib.c,v 1.128 2005/12/15 18:53:34 roberto Exp roberto $ | 2 | ** $Id: lstrlib.c,v 1.129 2005/12/21 12:59:43 roberto Exp roberto $ |
3 | ** Standard library for string operations and pattern-matching | 3 | ** Standard library for string operations and pattern-matching |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -837,7 +837,7 @@ static const luaL_Reg strlib[] = { | |||
837 | 837 | ||
838 | 838 | ||
839 | static void createmetatable (lua_State *L) { | 839 | static void createmetatable (lua_State *L) { |
840 | lua_newtable(L); /* create metatable for strings */ | 840 | lua_createtable(L, 0, 1); /* create metatable for strings */ |
841 | lua_pushliteral(L, ""); /* dummy string */ | 841 | lua_pushliteral(L, ""); /* dummy string */ |
842 | lua_pushvalue(L, -2); | 842 | lua_pushvalue(L, -2); |
843 | lua_setmetatable(L, -2); /* set string metatable */ | 843 | lua_setmetatable(L, -2); /* set string metatable */ |
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lua.c,v 1.155 2005/11/28 14:44:48 roberto Exp roberto $ | 2 | ** $Id: lua.c,v 1.156 2005/12/29 12:30:16 roberto Exp roberto $ |
3 | ** Lua stand-alone interpreter | 3 | ** Lua stand-alone interpreter |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -120,7 +120,7 @@ static int getargs (lua_State *L, char **argv, int n) { | |||
120 | luaL_checkstack(L, narg + 3, "too many arguments to script"); | 120 | luaL_checkstack(L, narg + 3, "too many arguments to script"); |
121 | for (i=n+1; i < argc; i++) | 121 | for (i=n+1; i < argc; i++) |
122 | lua_pushstring(L, argv[i]); | 122 | lua_pushstring(L, argv[i]); |
123 | lua_newtable(L); | 123 | lua_createtable(L, narg, n + 1); |
124 | for (i=0; i < argc; i++) { | 124 | for (i=0; i < argc; i++) { |
125 | lua_pushstring(L, argv[i]); | 125 | lua_pushstring(L, argv[i]); |
126 | lua_rawseti(L, -2, i - n); | 126 | lua_rawseti(L, -2, i - n); |