Pārlūkot izejas kodu

small optimizations (lua_newtable -> lua_createtable)

Roberto Ierusalimschy 19 gadi atpakaļ
vecāks
revīzija
bfdcbbcd76
9 mainītis faili ar 35 papildinājumiem un 27 dzēšanām
  1. 13 5
      lauxlib.c
  2. 2 2
      lauxlib.h
  3. 2 2
      lbaselib.c
  4. 3 3
      ldblib.c
  5. 2 2
      linit.c
  6. 4 4
      liolib.c
  7. 5 5
      loadlib.c
  8. 2 2
      lstrlib.c
  9. 2 2
      lua.c

+ 13 - 5
lauxlib.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lauxlib.c,v 1.155 2005/10/20 11:35:25 roberto Exp roberto $
+** $Id: lauxlib.c,v 1.156 2005/10/21 13:47:42 roberto Exp roberto $
 ** Auxiliary functions for building Lua libraries
 ** See Copyright Notice in lua.h
 */
@@ -226,16 +226,24 @@ LUALIB_API void (luaL_register) (lua_State *L, const char *libname,
 }
 
 
+static int libsize (const luaL_Reg *l) {
+  int size = 0;
+  for (; l->name; l++) size++;
+  return size;
+}
+
+
 LUALIB_API void luaI_openlib (lua_State *L, const char *libname,
                               const luaL_Reg *l, int nup) {
   if (libname) {
+    int size = libsize(l);
     /* check whether lib already exists */
-    luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED");
+    luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", size);
     lua_getfield(L, -1, libname);  /* get _LOADED[libname] */
     if (!lua_istable(L, -1)) {  /* not found? */
       lua_pop(L, 1);  /* remove previous result */
       /* try global variable (and create one if it does not exist) */
-      if (luaL_findtable(L, LUA_GLOBALSINDEX, libname) != NULL)
+      if (luaL_findtable(L, LUA_GLOBALSINDEX, libname, size) != NULL)
         luaL_error(L, "name conflict for module " LUA_QS, libname);
       lua_pushvalue(L, -1);
       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,
 
 
 LUALIB_API const char *luaL_findtable (lua_State *L, int idx,
-                                       const char *fname) {
+                                       const char *fname, int szhint) {
   const char *e;
   lua_pushvalue(L, idx);
   do {
@@ -341,7 +349,7 @@ LUALIB_API const char *luaL_findtable (lua_State *L, int idx,
     lua_rawget(L, -2);
     if (lua_isnil(L, -1)) {  /* no such field? */
       lua_pop(L, 1);  /* remove this nil */
-      lua_newtable(L);  /* create a new table for field */
+      lua_createtable(L, 0, (*e == '.' ? 1 : szhint)); /* new table for field */
       lua_pushlstring(L, fname, e - fname);
       lua_pushvalue(L, -2);
       lua_settable(L, -4);  /* set new table into field */

+ 2 - 2
lauxlib.h

@@ -1,5 +1,5 @@
 /*
-** $Id: lauxlib.h,v 1.85 2005/09/06 17:19:51 roberto Exp roberto $
+** $Id: lauxlib.h,v 1.86 2005/10/21 13:47:42 roberto Exp roberto $
 ** Auxiliary functions for building Lua libraries
 ** See Copyright Notice in lua.h
 */
@@ -86,7 +86,7 @@ LUALIB_API const char *(luaL_gsub) (lua_State *L, const char *s, const char *p,
                                                   const char *r);
 
 LUALIB_API const char *(luaL_findtable) (lua_State *L, int idx,
-                                         const char *fname);
+                                         const char *fname, int szhint);
 
 
 

+ 2 - 2
lbaselib.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lbaselib.c,v 1.186 2005/10/21 13:47:42 roberto Exp roberto $
+** $Id: lbaselib.c,v 1.187 2005/12/27 17:10:11 roberto Exp roberto $
 ** Basic library
 ** See Copyright Notice in lua.h
 */
@@ -625,7 +625,7 @@ static void base_open (lua_State *L) {
   auxopen(L, "ipairs", luaB_ipairs, ipairsaux);
   auxopen(L, "pairs", luaB_pairs, luaB_next);
   /* `newproxy' needs a weaktable as upvalue */
-  lua_newtable(L);  /* new table `w' */
+  lua_createtable(L, 0, 1);  /* new table `w' */
   lua_pushvalue(L, -1);  /* `w' will be its own metatable */
   lua_setmetatable(L, -2);
   lua_pushliteral(L, "kv");

+ 3 - 3
ldblib.c

@@ -1,5 +1,5 @@
 /*
-** $Id: ldblib.c,v 1.102 2005/10/19 13:05:11 roberto Exp roberto $
+** $Id: ldblib.c,v 1.103 2005/11/01 16:08:32 roberto Exp roberto $
 ** Interface from Lua to its debug API
 ** See Copyright Notice in lua.h
 */
@@ -116,7 +116,7 @@ static int db_getinfo (lua_State *L) {
     return luaL_argerror(L, arg+1, "function or level expected");
   if (!lua_getinfo(L1, options, &ar))
     return luaL_argerror(L, arg+2, "invalid option");
-  lua_newtable(L);
+  lua_createtable(L, 0, 2);
   if (strchr(options, 'S')) {
     settabss(L, "source", ar.source);
     settabss(L, "short_src", ar.short_src);
@@ -246,7 +246,7 @@ static void gethooktable (lua_State *L) {
   lua_rawget(L, LUA_REGISTRYINDEX);
   if (!lua_istable(L, -1)) {
     lua_pop(L, 1);
-    lua_newtable(L);
+    lua_createtable(L, 0, 1);
     lua_pushlightuserdata(L, (void *)&KEY_HOOK);
     lua_pushvalue(L, -2);
     lua_rawset(L, LUA_REGISTRYINDEX);

+ 2 - 2
linit.c

@@ -1,5 +1,5 @@
 /*
-** $Id: linit.c,v 1.12 2005/08/10 18:06:58 roberto Exp roberto $
+** $Id: linit.c,v 1.13 2005/08/26 17:36:32 roberto Exp roberto $
 ** Initialization of libraries for lua.c
 ** See Copyright Notice in lua.h
 */
@@ -16,13 +16,13 @@
 
 static const luaL_Reg lualibs[] = {
   {"", luaopen_base},
+  {LUA_LOADLIBNAME, luaopen_package},
   {LUA_TABLIBNAME, luaopen_table},
   {LUA_IOLIBNAME, luaopen_io},
   {LUA_OSLIBNAME, luaopen_os},
   {LUA_STRLIBNAME, luaopen_string},
   {LUA_MATHLIBNAME, luaopen_math},
   {LUA_DBLIBNAME, luaopen_debug},
-  {LUA_LOADLIBNAME, luaopen_package},
   {NULL, NULL}
 };
 

+ 4 - 4
liolib.c

@@ -1,5 +1,5 @@
 /*
-** $Id: liolib.c,v 2.68 2005/10/14 16:24:11 roberto Exp roberto $
+** $Id: liolib.c,v 2.69 2005/10/19 13:05:11 roberto Exp roberto $
 ** Standard I/O (and system) library
 ** See Copyright Notice in lua.h
 */
@@ -507,8 +507,8 @@ static void createstdfile (lua_State *L, FILE *f, int k, const char *fname) {
 
 LUALIB_API int luaopen_io (lua_State *L) {
   createmeta(L);
-  /* create new (private) environment */
-  lua_newtable(L);
+  /* create (private) environment (with fields IO_INPUT, IO_OUTPUT, __close) */
+  lua_createtable(L, 2, 1);
   lua_replace(L, LUA_ENVIRONINDEX);
   /* open library */
   luaL_register(L, LUA_IOLIBNAME, iolib);
@@ -518,7 +518,7 @@ LUALIB_API int luaopen_io (lua_State *L) {
   createstdfile(L, stderr, 0, "stderr");
   /* create environment for 'popen' */
   lua_getfield(L, -1, "popen");
-  lua_newtable(L);
+  lua_createtable(L, 0, 1);
   lua_pushcfunction(L, io_pclose);
   lua_setfield(L, -2, "__close");
   lua_setfenv(L, -2);

+ 5 - 5
loadlib.c

@@ -1,5 +1,5 @@
 /*
-** $Id: loadlib.c,v 1.49 2005/12/07 15:42:32 roberto Exp roberto $
+** $Id: loadlib.c,v 1.50 2005/12/19 20:56:39 roberto Exp roberto $
 ** Dynamic library loader for Lua
 ** See Copyright Notice in lua.h
 **
@@ -550,7 +550,7 @@ static int ll_module (lua_State *L) {
   if (!lua_istable(L, -1)) {  /* not found? */
     lua_pop(L, 1);  /* remove previous result */
     /* try global variable (and create one if it does not exist) */
-    if (luaL_findtable(L, LUA_GLOBALSINDEX, modname) != NULL)
+    if (luaL_findtable(L, LUA_GLOBALSINDEX, modname, 1) != NULL)
       return luaL_error(L, "name conflict for module " LUA_QS, modname);
     lua_pushvalue(L, -1);
     lua_setfield(L, loaded, modname);  /* _LOADED[modname] = new table */
@@ -573,7 +573,7 @@ static int ll_module (lua_State *L) {
 static int ll_seeall (lua_State *L) {
   luaL_checktype(L, 1, LUA_TTABLE);
   if (!lua_getmetatable(L, 1)) {
-    lua_newtable(L); /* create new metatable */
+    lua_createtable(L, 0, 1); /* create new metatable */
     lua_pushvalue(L, -1);
     lua_setmetatable(L, 1);
   }
@@ -640,7 +640,7 @@ LUALIB_API int luaopen_package (lua_State *L) {
   lua_pushvalue(L, -1);
   lua_replace(L, LUA_ENVIRONINDEX);
   /* create `loaders' table */
-  lua_newtable(L);
+  lua_createtable(L, 0, sizeof(loaders)/sizeof(loaders[0]) - 1);
   /* fill it with pre-defined loaders */
   for (i=0; loaders[i] != NULL; i++) {
     lua_pushcfunction(L, loaders[i]);
@@ -654,7 +654,7 @@ LUALIB_API int luaopen_package (lua_State *L) {
                     LUA_EXECDIR "\n" LUA_IGMARK);
   lua_setfield(L, -2, "config");
   /* set field `loaded' */
-  luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED");
+  luaL_findtable(L, LUA_REGISTRYINDEX, "_LOADED", 2);
   lua_setfield(L, -2, "loaded");
   /* set field `preload' */
   lua_newtable(L);

+ 2 - 2
lstrlib.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lstrlib.c,v 1.128 2005/12/15 18:53:34 roberto Exp roberto $
+** $Id: lstrlib.c,v 1.129 2005/12/21 12:59:43 roberto Exp roberto $
 ** Standard library for string operations and pattern-matching
 ** See Copyright Notice in lua.h
 */
@@ -837,7 +837,7 @@ static const luaL_Reg strlib[] = {
 
 
 static void createmetatable (lua_State *L) {
-  lua_newtable(L);  /* create metatable for strings */
+  lua_createtable(L, 0, 1);  /* create metatable for strings */
   lua_pushliteral(L, "");  /* dummy string */
   lua_pushvalue(L, -2);
   lua_setmetatable(L, -2);  /* set string metatable */

+ 2 - 2
lua.c

@@ -1,5 +1,5 @@
 /*
-** $Id: lua.c,v 1.155 2005/11/28 14:44:48 roberto Exp roberto $
+** $Id: lua.c,v 1.156 2005/12/29 12:30:16 roberto Exp roberto $
 ** Lua stand-alone interpreter
 ** See Copyright Notice in lua.h
 */
@@ -120,7 +120,7 @@ static int getargs (lua_State *L, char **argv, int n) {
   luaL_checkstack(L, narg + 3, "too many arguments to script");
   for (i=n+1; i < argc; i++)
     lua_pushstring(L, argv[i]);
-  lua_newtable(L);
+  lua_createtable(L, narg, n + 1);
   for (i=0; i < argc; i++) {
     lua_pushstring(L, argv[i]);
     lua_rawseti(L, -2, i - n);