small optimization for sizes of array constructors

This commit is contained in:
Roberto Ierusalimschy 2003-02-18 13:02:56 -03:00
parent 07948c3181
commit 60c83ded30
5 changed files with 31 additions and 9 deletions

View File

@ -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,

View File

@ -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);

View File

@ -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 */
}

View File

@ -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},

4
lvm.c
View File

@ -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;