small optimization in the access to i.m. table.

This commit is contained in:
Roberto Ierusalimschy
1997-04-24 19:59:57 -03:00
parent 209602ac31
commit 369dd65318
3 changed files with 49 additions and 40 deletions

View File

@@ -3,7 +3,7 @@
** TecCGraf - PUC-Rio ** TecCGraf - PUC-Rio
*/ */
char *rcs_fallback="$Id: fallback.c,v 2.3 1997/04/06 14:08:08 roberto Exp roberto $"; char *rcs_fallback="$Id: fallback.c,v 2.4 1997/04/07 14:48:53 roberto Exp roberto $";
#include <stdio.h> #include <stdio.h>
#include <string.h> #include <string.h>
@@ -111,9 +111,7 @@ static int luaI_checkevent (char *name, char *list[])
} }
static struct IM { struct IM *luaI_IMtable = NULL;
TObject int_method[IM_N];
} *luaI_IMtable = NULL;
static int IMtable_size = 0; static int IMtable_size = 0;
static int last_tag = LUA_T_NIL; /* ORDER LUA_T */ static int last_tag = LUA_T_NIL; /* ORDER LUA_T */
@@ -141,7 +139,7 @@ static void init_entry (int tag)
{ {
int i; int i;
for (i=0; i<IM_N; i++) for (i=0; i<IM_N; i++)
luaI_IMtable[-tag].int_method[i].ttype = LUA_T_NIL; ttype(luaI_getim(tag, i)) = LUA_T_NIL;
} }
void luaI_initfallbacks (void) void luaI_initfallbacks (void)
@@ -194,32 +192,26 @@ void luaI_settag (int tag, TObject *o)
} }
int luaI_tag (TObject *o) int luaI_efectivetag (TObject *o)
{ {
lua_Type t = ttype(o); lua_Type t = ttype(o);
if (t == LUA_T_USERDATA) if (t == LUA_T_USERDATA) {
return o->value.ts->tag; int tag = o->value.ts->tag;
return (tag >= 0) ? LUA_T_USERDATA : tag;
}
else if (t == LUA_T_ARRAY) else if (t == LUA_T_ARRAY)
return o->value.a->htag; return o->value.a->htag;
else return t; else return t;
} }
TObject *luaI_getim (int tag, IMS event)
{
if (tag > LUA_T_USERDATA)
tag = LUA_T_USERDATA; /* default for non-registered tags */
return &luaI_IMtable[-tag].int_method[event];
}
void luaI_gettagmethod (void) void luaI_gettagmethod (void)
{ {
int t = (int)luaL_check_number(1); int t = (int)luaL_check_number(1);
int e = luaI_checkevent(luaL_check_string(2), luaI_eventname); int e = luaI_checkevent(luaL_check_string(2), luaI_eventname);
checktag(t); checktag(t);
if (validevent(t, e)) if (validevent(t, e))
luaI_pushobject(&luaI_IMtable[-t].int_method[e]); luaI_pushobject(luaI_getim(t,e));
} }
@@ -234,8 +226,8 @@ void luaI_settagmethod (void)
luaI_eventname[e], t); luaI_eventname[e], t);
luaL_arg_check(lua_isnil(func) || lua_isfunction(func), luaL_arg_check(lua_isnil(func) || lua_isfunction(func),
3, "function expected"); 3, "function expected");
luaI_pushobject(&luaI_IMtable[-t].int_method[e]); luaI_pushobject(luaI_getim(t,e));
luaI_IMtable[-t].int_method[e] = *luaI_Address(func); *luaI_getim(t, e) = *luaI_Address(func);
} }
@@ -264,7 +256,7 @@ char *luaI_travfallbacks (int (*fn)(TObject *))
for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */ for (e=IM_GETTABLE; e<=IM_FUNCTION; e++) { /* ORDER IM */
int t; int t;
for (t=0; t>=last_tag; t--) for (t=0; t>=last_tag; t--)
if (fn(&luaI_IMtable[-t].int_method[e])) if (fn(luaI_getim(t,e)))
return luaI_eventname[e]; return luaI_eventname[e];
} }
return NULL; return NULL;
@@ -301,7 +293,7 @@ static void fillvalids (IMS e, TObject *func)
int t; int t;
for (t=LUA_T_NIL; t<=LUA_T_USERDATA; t++) for (t=LUA_T_NIL; t<=LUA_T_USERDATA; t++)
if (validevent(t, e)) if (validevent(t, e))
luaI_IMtable[-t].int_method[e] = *func; *luaI_getim(t, e) = *func;
} }
void luaI_setfallback (void) void luaI_setfallback (void)
@@ -320,13 +312,13 @@ void luaI_setfallback (void)
replace = errorFB; replace = errorFB;
break; break;
case 1: /* old getglobal fallback */ case 1: /* old getglobal fallback */
oldfunc = luaI_IMtable[-LUA_T_NIL].int_method[IM_GETGLOBAL]; oldfunc = *luaI_getim(LUA_T_NIL, IM_GETGLOBAL);
luaI_IMtable[-LUA_T_NIL].int_method[IM_GETGLOBAL] = *luaI_Address(func); *luaI_getim(LUA_T_NIL, IM_GETGLOBAL) = *luaI_Address(func);
replace = nilFB; replace = nilFB;
break; break;
case 2: { /* old arith fallback */ case 2: { /* old arith fallback */
int i; int i;
oldfunc = luaI_IMtable[-LUA_T_USERDATA].int_method[IM_POW]; oldfunc = *luaI_getim(LUA_T_USERDATA, IM_POW);
for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */ for (i=IM_ADD; i<=IM_UNM; i++) /* ORDER IM */
fillvalids(i, luaI_Address(func)); fillvalids(i, luaI_Address(func));
replace = typeFB; replace = typeFB;
@@ -334,7 +326,7 @@ void luaI_setfallback (void)
} }
case 3: { /* old order fallback */ case 3: { /* old order fallback */
int i; int i;
oldfunc = luaI_IMtable[-LUA_T_USERDATA].int_method[IM_LT]; oldfunc = *luaI_getim(LUA_T_USERDATA, IM_LT);
for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */ for (i=IM_LT; i<=IM_GE; i++) /* ORDER IM */
fillvalids(i, luaI_Address(func)); fillvalids(i, luaI_Address(func));
replace = typeFB; replace = typeFB;
@@ -343,7 +335,7 @@ void luaI_setfallback (void)
default: { default: {
int e; int e;
if ((e = luaI_findstring(name, luaI_eventname)) >= 0) { if ((e = luaI_findstring(name, luaI_eventname)) >= 0) {
oldfunc = luaI_IMtable[-LUA_T_USERDATA].int_method[e]; oldfunc = *luaI_getim(LUA_T_USERDATA, e);
fillvalids(e, luaI_Address(func)); fillvalids(e, luaI_Address(func));
replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB; replace = (e == IM_GC || e == IM_INDEX) ? nilFB : typeFB;
} }

View File

@@ -1,5 +1,5 @@
/* /*
** $Id: fallback.h,v 1.21 1997/04/02 23:04:12 roberto Exp roberto $ ** $Id: fallback.h,v 1.22 1997/04/04 22:24:51 roberto Exp roberto $
*/ */
#ifndef fallback_h #ifndef fallback_h
@@ -35,8 +35,15 @@ typedef enum {
#define IM_N 18 #define IM_N 18
extern struct IM {
TObject int_method[IM_N];
} *luaI_IMtable;
extern char *luaI_eventname[]; extern char *luaI_eventname[];
#define luaI_getim(tag,event) (&luaI_IMtable[-(tag)].int_method[event])
#define luaI_getimbyObj(o,e) (luaI_getim(luaI_efectivetag(o),(e)))
void luaI_setfallback (void); void luaI_setfallback (void);
int luaI_ref (TObject *object, int lock); int luaI_ref (TObject *object, int lock);
@@ -47,10 +54,8 @@ char *luaI_travfallbacks (int (*fn)(TObject *));
void luaI_settag (int tag, TObject *o); void luaI_settag (int tag, TObject *o);
void luaI_realtag (int tag); void luaI_realtag (int tag);
TObject *luaI_getim (int tag, IMS event);
#define luaI_getimbyObj(o,e) (luaI_getim(luaI_tag(o),(e)))
TObject *luaI_geterrorim (void); TObject *luaI_geterrorim (void);
int luaI_tag (TObject *o); int luaI_efectivetag (TObject *o);
void luaI_settagmethod (void); void luaI_settagmethod (void);
void luaI_gettagmethod (void); void luaI_gettagmethod (void);
void luaI_seterrormethod (void); void luaI_seterrormethod (void);

View File

@@ -3,7 +3,7 @@
** TecCGraf - PUC-Rio ** TecCGraf - PUC-Rio
*/ */
char *rcs_opcode="$Id: opcode.c,v 4.2 1997/04/04 22:24:51 roberto Exp roberto $"; char *rcs_opcode="$Id: opcode.c,v 4.3 1997/04/15 17:32:47 roberto Exp roberto $";
#include <setjmp.h> #include <setjmp.h>
#include <stdio.h> #include <stdio.h>
@@ -180,11 +180,14 @@ static int lua_tostring (TObject *obj)
*/ */
static void adjust_top (StkId newtop) static void adjust_top (StkId newtop)
{ {
TObject *nt; if (newtop <= top-stack) /* int arith, since newtop may be out of stack */
lua_checkstack(stack+newtop); top = stack+newtop;
nt = stack+newtop; /* warning: previous call may change stack */ else {
while (top < nt) ttype(top++) = LUA_T_NIL; TObject *nt;
top = nt; /* top could be bigger than newtop */ lua_checkstack(stack+newtop);
nt = stack+newtop; /* warning: previous call may change stack */
while (top < nt) ttype(top++) = LUA_T_NIL;
}
} }
#define adjustC(nParams) adjust_top(CLS_current.base+nParams) #define adjustC(nParams) adjust_top(CLS_current.base+nParams)
@@ -300,7 +303,7 @@ static void do_call (StkId base, int nResults)
return; return;
} }
/* adjust the number of results */ /* adjust the number of results */
if (nResults != MULT_RET && top - (stack+firstResult) != nResults) if (nResults != MULT_RET)
adjust_top(firstResult+nResults); adjust_top(firstResult+nResults);
/* move results to base-1 (to erase parameters and function) */ /* move results to base-1 (to erase parameters and function) */
base--; base--;
@@ -317,7 +320,7 @@ static void do_call (StkId base, int nResults)
*/ */
static void pushsubscript (void) static void pushsubscript (void)
{ {
int tg = luaI_tag(top-2); int tg = luaI_efectivetag(top-2);
TObject *im = luaI_getim(tg, IM_GETTABLE); TObject *im = luaI_getim(tg, IM_GETTABLE);
if (ttype(top-2) == LUA_T_ARRAY && ttype(im) == LUA_T_NIL) { if (ttype(top-2) == LUA_T_ARRAY && ttype(im) == LUA_T_NIL) {
TObject *h = lua_hashget(avalue(top-2), top-1); TObject *h = lua_hashget(avalue(top-2), top-1);
@@ -1033,9 +1036,18 @@ void lua_pushobject (lua_Object o)
incr_top; incr_top;
} }
int lua_tag (lua_Object o) int lua_tag (lua_Object lo)
{ {
return (o == LUA_NOOBJECT) ? LUA_T_NIL : luaI_tag(Address(o)); if (lo == LUA_NOOBJECT) return LUA_T_NIL;
else {
TObject *o = Address(lo);
lua_Type t = ttype(o);
if (t == LUA_T_USERDATA)
return o->value.ts->tag;
else if (t == LUA_T_ARRAY)
return o->value.a->htag;
else return t;
}
} }