From abbf14cd32bf83d5ea5ab70977e0653a03b455c5 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy <roberto@inf.puc-rio.br> Date: Thu, 31 Jul 1997 17:46:59 -0300 Subject: small optimizations; try to find previous use when coding a real constant. --- lua.stx | 82 ++++++++++++++++++++++++++++++++++++----------------------------- 1 file changed, 45 insertions(+), 37 deletions(-) diff --git a/lua.stx b/lua.stx index 66b27e14..45dbb7ee 100644 --- a/lua.stx +++ b/lua.stx @@ -1,6 +1,6 @@ %{ -char *rcs_luastx = "$Id: lua.stx,v 3.48 1997/07/29 20:38:45 roberto Exp roberto $"; +char *rcs_luastx = "$Id: lua.stx,v 3.49 1997/07/30 22:00:50 roberto Exp roberto $"; #include <stdlib.h> @@ -50,8 +50,6 @@ static Long varbuffer[MAXVAR]; /* variables in an assignment list; static int nvarbuffer=0; /* number of variables at a list */ -#define MAXFIELDS FIELDS_PER_FLUSH*2 - int lua_debug = 0; /* Internal functions */ @@ -106,65 +104,75 @@ static void code_constant (int c) static int next_constant (void) { - if (currState->f->nconsts >= currState->maxconsts) { + TFunc *f = currState->f; + if (f->nconsts >= currState->maxconsts) { currState->maxconsts = - growvector(&currState->f->consts, currState->maxconsts, - TObject, constantEM, MAX_WORD); + growvector(&f->consts, currState->maxconsts, TObject, + constantEM, MAX_WORD); } - return currState->f->nconsts++; + return f->nconsts++; } static int string_constant (TaggedString *s) { + TFunc *f = currState->f; int c = s->u.s.constindex; - if (!(0 <= c && c < currState->f->nconsts && - ttype(&currState->f->consts[c]) == LUA_T_STRING && - tsvalue(&currState->f->consts[c]) == s)) { + if (!(0 <= c && c < f->nconsts && + ttype(&f->consts[c]) == LUA_T_STRING && tsvalue(&f->consts[c]) == s)) { c = next_constant(); - ttype(&currState->f->consts[c]) = LUA_T_STRING; - tsvalue(&currState->f->consts[c]) = s; + ttype(&f->consts[c]) = LUA_T_STRING; + tsvalue(&f->consts[c]) = s; s->u.s.constindex = c; /* hint for next time */ - luaI_releasestring(s); } + luaI_releasestring(s); return c; } static void code_string (TaggedString *s) { - int c = string_constant(s); - code_constant(c); + code_constant(string_constant(s)); } -static void code_float (real n) + +#define LIM 10 +static int real_constant (real r) { - int c = next_constant(); - ttype(&currState->f->consts[c]) = LUA_T_NUMBER; - nvalue(&currState->f->consts[c]) = n; - code_constant(c); + /* check whether 'r' has appeared within the last LIM entries */ + TObject *cnt = currState->f->consts; + int c = currState->f->nconsts; + int lim = c < LIM ? 0 : c-LIM; + while (--c >= lim) { + if (ttype(&cnt[c]) == LUA_T_NUMBER && nvalue(&cnt[c]) == r) + return c; + } + /* not found; create a new entry */ + c = next_constant(); + cnt = currState->f->consts; /* 'next_constant' may reallocate this vector */ + ttype(&cnt[c]) = LUA_T_NUMBER; + nvalue(&cnt[c]) = r; + return c; } -static void code_number (float f) +static void code_number (real f) { Word i; - if (f >= 0 && f <= (float)MAX_WORD && (float)(i=(Word)f) == f) { - /* f has an (short) integer value */ - if (i <= 2) code_byte(PUSH0 + i); - else if (i <= 255) - { - code_byte(PUSHBYTE); - code_byte(i); - } - else - { - code_byte(PUSHWORD); - code_word(i); - } + if (f >= 0 && f <= (real)MAX_WORD && (real)(i=(Word)f) == f) { + /* f has an (short) integer value */ + if (i <= 2) code_byte(PUSH0 + i); + else if (i <= 255) { + code_byte(PUSHBYTE); + code_byte(i); + } + else { + code_byte(PUSHWORD); + code_word(i); + } } else - code_float(f); + code_constant(real_constant(f)); } @@ -483,7 +491,7 @@ void lua_parse (TFunc *tf) %union { int vInt; - float vFloat; + real vReal; char *pChar; Long vLong; TaggedString *pTStr; @@ -498,7 +506,7 @@ void lua_parse (TFunc *tf) %token LOCAL %token FUNCTION %token DOTS -%token <vFloat> NUMBER +%token <vReal> NUMBER %token <pTStr> NAME STRING %type <vLong> PrepJump -- cgit v1.2.3-55-g6feb