lua-users home
lua-l archive

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


Em sáb., 21 de nov. de 2020 às 13:13, Roberto Ierusalimschy <roberto@inf.puc-rio.br> escreveu:
> anyway, usage of lua_pushlstring instead of lua_pushstring is much better.

 For literal strings, lua_pushstring is both simpler to use and faster. 
lua_pushstring(L, "cannot close standard file");
Can be simpler, but it is not faster than:

/* avoid strlen call or inline */
lua_pushlstring(L, "cannot close standard file", sizeof("cannot close standard file") - 1);

Another example:
void luaT_init (lua_State *L) {
  static const char *const luaT_eventname[] = {  /* ORDER TM */
    "__index", "__newindex",
    "__gc", "__mode", "__len", "__eq",
    "__add", "__sub", "__mul", "__mod", "__pow",
    "__div", "__idiv",
    "__band", "__bor", "__bxor", "__shl", "__shr",
    "__unm", "__bnot", "__lt", "__le",
    "__concat", "__call", "__close"
  };
  unsigned int i;
  for (i=0; i<TM_N; ++i) {
    G(L)->tmname[i] = luaS_newlstr(L, luaT_eventname[i], sizeof(luaT_eventname[i]) - 1); /* build-time size */
    luaC_fix(L, obj2gco(G(L)->tmname[i]));  /* never collect these names */
  }
}

For mutable strings, lua_pushstring may not be even correct. (The string
might contain embedded zeros.)

/* mutable mylargestring with 1Gb size, which certainly *does not* contain zeros. */
lua_pushstring(L, mylargestring); /* always calling strlen or inline */

/* mutable mylargestring with 1Gb size, which certainly *does not* contain zeros. */
lua_pushlstring(L, mylargestring, len); /* avoid strlen call or inline */
 
Ranier Vilela