From 60c83ded3080a23bc661ab440c36d0a71b399e2e Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Tue, 18 Feb 2003 13:02:56 -0300 Subject: small optimization for sizes of array constructors --- lobject.c | 16 +++++++++++++++- lobject.h | 5 +++-- lparser.c | 5 ++--- ltests.c | 10 +++++++++- lvm.c | 4 ++-- 5 files changed, 31 insertions(+), 9 deletions(-) diff --git a/lobject.c b/lobject.c index ebe2728f..5c5c1383 100644 --- a/lobject.c +++ b/lobject.c @@ -1,5 +1,5 @@ /* -** $Id: lobject.c,v 1.94 2002/12/04 17:38:31 roberto Exp roberto $ +** $Id: lobject.c,v 1.95 2003/01/27 13:00:43 roberto Exp roberto $ ** Some generic functions over Lua objects ** See Copyright Notice in lua.h */ @@ -30,6 +30,20 @@ const TObject luaO_nilobject = {LUA_TNIL, {NULL}}; +/* +** converts an integer to a "floating point byte", represented as +** (mmmmmxxx), where the real value is (xxx) * 2^(mmmmm) +*/ +int luaO_int2fb (unsigned int x) { + int m = 0; /* mantissa */ + while (x >= (1<<3)) { + x = (x+1) >> 1; + m++; + } + return (m << 3) | cast(int, x); +} + + int luaO_log2 (unsigned int x) { static const lu_byte log_8[255] = { 0, diff --git a/lobject.h b/lobject.h index 90533240..420eb6a9 100644 --- a/lobject.h +++ b/lobject.h @@ -1,5 +1,5 @@ /* -** $Id: lobject.h,v 1.156 2002/12/19 11:11:55 roberto Exp roberto $ +** $Id: lobject.h,v 1.157 2003/02/11 10:46:24 roberto Exp roberto $ ** Type definitions for Lua objects ** See Copyright Notice in lua.h */ @@ -323,7 +323,8 @@ typedef struct Table { extern const TObject luaO_nilobject; int luaO_log2 (unsigned int x); - +int luaO_int2fb (unsigned int x); +#define fb2int(x) (((x) & 7) << ((x) >> 3)) int luaO_rawequalObj (const TObject *t1, const TObject *t2); int luaO_str2d (const char *s, lua_Number *result); diff --git a/lparser.c b/lparser.c index 68373d54..c5b5df85 100644 --- a/lparser.c +++ b/lparser.c @@ -1,5 +1,5 @@ /* -** $Id: lparser.c,v 1.204 2003/02/11 10:46:24 roberto Exp roberto $ +** $Id: lparser.c,v 1.205 2003/02/11 10:49:53 roberto Exp roberto $ ** Lua Parser ** See Copyright Notice in lua.h */ @@ -515,8 +515,7 @@ static void constructor (LexState *ls, expdesc *t) { } while (testnext(ls, ',') || testnext(ls, ';')); check_match(ls, '}', '{', line); lastlistfield(fs, &cc); - if (cc.na > 0) - SETARG_B(fs->f->code[pc], luaO_log2(cc.na-1)+2); /* set initial table size */ + SETARG_B(fs->f->code[pc], luaO_int2fb(cc.na)); /* set initial array size */ SETARG_C(fs->f->code[pc], luaO_log2(cc.nh)+1); /* set initial table size */ } diff --git a/ltests.c b/ltests.c index f0fe854a..1e27c45f 100644 --- a/ltests.c +++ b/ltests.c @@ -1,5 +1,5 @@ /* -** $Id: ltests.c,v 1.151 2003/01/29 10:27:53 roberto Exp roberto $ +** $Id: ltests.c,v 1.152 2003/02/10 17:31:13 roberto Exp roberto $ ** Internal Module for Debugging of the Lua Implementation ** See Copyright Notice in lua.h */ @@ -512,6 +512,13 @@ static int log2_aux (lua_State *L) { return 1; } +static int int2fb_aux (lua_State *L) { + int b = luaO_int2fb(luaL_checkint(L, 1)); + lua_pushnumber(L, b); + lua_pushnumber(L, fb2int(b)); + return 2; +} + static int test_do (lua_State *L) { const char *p = luaL_checkstring(L, 1); @@ -790,6 +797,7 @@ static const struct luaL_reg tests_funcs[] = { {"closestate", closestate}, {"doremote", doremote}, {"log2", log2_aux}, + {"int2fb", int2fb_aux}, {"totalmem", mem_query}, {"resume", coresume}, {"setyhook", setyhook}, diff --git a/lvm.c b/lvm.c index 31e7b083..c66bbc8a 100644 --- a/lvm.c +++ b/lvm.c @@ -1,5 +1,5 @@ /* -** $Id: lvm.c,v 1.274 2003/01/27 15:12:52 roberto Exp roberto $ +** $Id: lvm.c,v 1.275 2003/02/11 10:46:24 roberto Exp roberto $ ** Lua virtual machine ** See Copyright Notice in lua.h */ @@ -495,7 +495,7 @@ StkId luaV_execute (lua_State *L) { } case OP_NEWTABLE: { int b = GETARG_B(i); - if (b > 0) b = twoto(b-1); + b = fb2int(b); sethvalue(ra, luaH_new(L, b, GETARG_C(i))); luaC_checkGC(L); break; -- cgit v1.2.3-55-g6feb