From f292760f12022a83cf01e788482a264aeeb3c276 Mon Sep 17 00:00:00 2001 From: Roberto Ierusalimschy Date: Fri, 12 Mar 2010 15:59:32 -0300 Subject: [PATCH] small optimization in luaL_addlstring (avoid adding chars one by one) (suggested by Chuck Coffing) --- lauxlib.c | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/lauxlib.c b/lauxlib.c index 8e49c311..46c7e4f9 100644 --- a/lauxlib.c +++ b/lauxlib.c @@ -1,5 +1,5 @@ /* -** $Id: lauxlib.c,v 1.200 2010/02/18 19:32:41 roberto Exp roberto $ +** $Id: lauxlib.c,v 1.201 2010/02/18 19:37:57 roberto Exp roberto $ ** Auxiliary functions for building Lua libraries ** See Copyright Notice in lua.h */ @@ -393,8 +393,19 @@ LUALIB_API char *luaL_prepbuffer (luaL_Buffer *B) { LUALIB_API void luaL_addlstring (luaL_Buffer *B, const char *s, size_t l) { - while (l--) - luaL_addchar(B, *s++); + while (l) { + size_t space = bufffree(B); + if (space == 0) { + luaL_prepbuffer(B); + lua_assert(bufffree(B) == LUAL_BUFFERSIZE); + space = LUAL_BUFFERSIZE; + } + if (space > l) space = l; + memcpy(B->p, s, space); + B->p += space; + s += space; + l -= space; + } }