lua-users home
lua-l archive

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


I don't know if I'm missing something but here the code from Lua 5.1.4 VM:

  luaL_Buffer b;
  luaL_buffinit(L, &b);

  [...]

        case 's': {
          size_t l;
          const char *s = luaL_checklstring(L, arg, &l);
          if (!strchr(form, '.') && l >= 100) {
            /* no precision and string is too long to be formatted;
               keep original string */
            lua_pushvalue(L, arg);
            luaL_addvalue(&b);
            continue;  /* skip the `addsize' at the end */
          }
          else {
            sprintf(buff, form, s);
            break;
          }
        }

for me this means that:
- a Lua string buffer object is used => fast and efficient
- if the string is small the value is just blitted to the buffer with sprintf
- if the string is big the value is appended with luaB_addvalue => efficient

so for me the string.format method remains superior, unless I'm
missing something.

I believe that the lua string buffer was created exactly to treat
efficiently this kind of cases and avoid the string concatenation
pattern that is suboptimal. You have exactly the same thing in OCaml,
you can concatenate string but for efficiency a low level buffer
object is available to avoir to always allocate & copy new chunks of
memory.

Francesco