lua-users home
lua-l archive

[Date Prev][Date Next][Thread Prev][Thread Next] [Date Index] [Thread Index]



This brings me to another question apropos of this one.

Given this code in lapi.c:

static TValue *index2adr (lua_State *L, int idx) {
  if (idx > 0) {
    TValue *o = L->base + (idx - 1);
    api_check(L, idx <= L->ci->top - L->base);
    if (o >= L->top) return cast(TValue *, luaO_nilobject);
    else return o;
  }
  else if (idx > LUA_REGISTRYINDEX) {
    api_check(L, idx != 0 && -idx <= L->top - L->base);
    return L->top + idx;
  }

Does this mean it's more efficient to reference stack positions by positive numbers (first if clause) or by negative numbers (couple less lines of code)? I have small functions where I (as a programmer) don't care one way or the other, but if there's a slight gain I could do it that way (some of these small functions are executed many times).

If the stack index is negative but greater than the stack size, that would be bad, right?