lua-users home
lua-l archive

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


on 12/17/07 12:36 PM, Luiz Henrique de Figueiredo at lhf@tecgraf.puc-rio.br
wrote:

>> using pushstring() feels unoptimal.
> 
> That's the problem: it *feels* unoptimal but in reality, as Rici reported,
> it does not make any actual difference, not enough to warrant a complicated,
> error-prone, ugly API...
> 
> Yet another lesson at the wrong perceptions that we programmers have about
> performance...

I've done profiles where string pushing rated surprisingly high given that
Lightroom isn't a string intensive app. Not huge, but more than one would
expect. (Sorry. I can't recall numbers just now.)

On the other hand, changing luaB_type to use upvalues slashed some of that
away.

Mark

----

static int luaB_type (lua_State *L) {
  luaL_checkany(L, 1);
  lua_pushstring(L, luaL_typename(L, 1));
  return 1;
}

#if ADOBE_EXTENSIONS

#define MIN_LUATYPE LUA_TNIL
#define MAX_LUATYPE LUA_TTHREAD

static int luaB_type_optimized (lua_State *L) {
  int ltype = lua_type(L, 1);
  if (MIN_LUATYPE <= ltype && ltype <= MAX_LUATYPE) {
    lua_pushvalue(L, lua_upvalueindex(ltype + (1 - MIN_LUATYPE)));
    return 1;
  } else {
    /* Fall back to non-optimized version. */
    return luaB_type (L);
  }
}

/* Takes the target table on the top of the stack. */

static void register_optimized_type (lua_State *L) {
  int i;
  for (i = MIN_LUATYPE; i <= MAX_LUATYPE; ++i) {
    lua_pushstring (L, lua_typename(L, i));
  }
  lua_pushcclosure(L, luaB_type_optimized, MAX_LUATYPE - MIN_LUATYPE + 1);
  lua_setfield(L, -2, "type");
}

#endif