aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorRoberto Ierusalimschy <roberto@inf.puc-rio.br>2020-07-03 11:36:56 -0300
committerRoberto Ierusalimschy <roberto@inf.puc-rio.br>2020-07-03 11:36:56 -0300
commite96385adede47a1abf160a41565ec742d3d4e413 (patch)
tree9aff6c3074d329b72b4d6a10299f30e1487e5010
parent56a165bf0f061f7aff744fafd44691a2beb4b035 (diff)
downloadlua-e96385adede47a1abf160a41565ec742d3d4e413.tar.gz
lua-e96385adede47a1abf160a41565ec742d3d4e413.tar.bz2
lua-e96385adede47a1abf160a41565ec742d3d4e413.zip
Simplification and smaller buffers for 'lua_pushfstring'
The function 'lua_pushfstring' is seldom called with large strings, there is no need to optimize too much for that cases.
-rw-r--r--lobject.c26
-rw-r--r--testes/strings.lua2
2 files changed, 17 insertions, 11 deletions
diff --git a/lobject.c b/lobject.c
index b4efae4f..2a28ebd4 100644
--- a/lobject.c
+++ b/lobject.c
@@ -215,7 +215,7 @@ static lua_Number lua_strx2number (const char *s, char **endptr) {
215/* }====================================================== */ 215/* }====================================================== */
216 216
217 217
218/* maximum length of a numeral */ 218/* maximum length of a numeral to be converted to a number */
219#if !defined (L_MAXLENNUM) 219#if !defined (L_MAXLENNUM)
220#define L_MAXLENNUM 200 220#define L_MAXLENNUM 200
221#endif 221#endif
@@ -333,8 +333,15 @@ int luaO_utf8esc (char *buff, unsigned long x) {
333} 333}
334 334
335 335
336/* maximum length of the conversion of a number to a string */ 336/*
337#define MAXNUMBER2STR 50 337** Maximum length of the conversion of a number to a string. Must be
338** enough to accommodate both LUA_INTEGER_FMT and LUA_NUMBER_FMT.
339** (For a long long int, this is 19 digits plus a sign and a final '\0',
340** adding to 21. For a long double, it can go to a sign, 33 digits,
341** the dot, an exponent letter, an exponent sign, 5 exponent digits,
342** and a final '\0', adding to 43.)
343*/
344#define MAXNUMBER2STR 44
338 345
339 346
340/* 347/*
@@ -375,7 +382,7 @@ void luaO_tostring (lua_State *L, TValue *obj) {
375*/ 382*/
376 383
377/* size for buffer space used by 'luaO_pushvfstring' */ 384/* size for buffer space used by 'luaO_pushvfstring' */
378#define BUFVFS 400 385#define BUFVFS 200
379 386
380/* buffer used by 'luaO_pushvfstring' */ 387/* buffer used by 'luaO_pushvfstring' */
381typedef struct BuffFS { 388typedef struct BuffFS {
@@ -387,16 +394,16 @@ typedef struct BuffFS {
387 394
388 395
389/* 396/*
390** Push given string to the stack, as part of the buffer. If the stack 397** Push given string to the stack, as part of the buffer, and
391** is almost full, join all partial strings in the stack into one. 398** join the partial strings in the stack into one.
392*/ 399*/
393static void pushstr (BuffFS *buff, const char *str, size_t l) { 400static void pushstr (BuffFS *buff, const char *str, size_t l) {
394 lua_State *L = buff->L; 401 lua_State *L = buff->L;
395 setsvalue2s(L, L->top, luaS_newlstr(L, str, l)); 402 setsvalue2s(L, L->top, luaS_newlstr(L, str, l));
396 L->top++; /* may use one extra slot */ 403 L->top++; /* may use one extra slot */
397 buff->pushed++; 404 buff->pushed++;
398 if (buff->pushed > 1 && L->top + 1 >= L->stack_last) { 405 if (buff->pushed > 1) {
399 luaV_concat(L, buff->pushed); /* join all partial results into one */ 406 luaV_concat(L, buff->pushed); /* join partial results into one */
400 buff->pushed = 1; 407 buff->pushed = 1;
401 } 408 }
402} 409}
@@ -521,8 +528,7 @@ const char *luaO_pushvfstring (lua_State *L, const char *fmt, va_list argp) {
521 } 528 }
522 addstr2buff(&buff, fmt, strlen(fmt)); /* rest of 'fmt' */ 529 addstr2buff(&buff, fmt, strlen(fmt)); /* rest of 'fmt' */
523 clearbuff(&buff); /* empty buffer into the stack */ 530 clearbuff(&buff); /* empty buffer into the stack */
524 if (buff.pushed > 1) 531 lua_assert(buff.pushed == 1);
525 luaV_concat(L, buff.pushed); /* join all partial results */
526 return svalue(s2v(L->top - 1)); 532 return svalue(s2v(L->top - 1));
527} 533}
528 534
diff --git a/testes/strings.lua b/testes/strings.lua
index 4a10857e..2fa4a89f 100644
--- a/testes/strings.lua
+++ b/testes/strings.lua
@@ -438,7 +438,7 @@ else
438 438
439 -- formats %U, %f, %I already tested elsewhere 439 -- formats %U, %f, %I already tested elsewhere
440 440
441 local blen = 400 -- internal buffer length in 'luaO_pushfstring' 441 local blen = 200 -- internal buffer length in 'luaO_pushfstring'
442 442
443 local function callpfs (op, fmt, n) 443 local function callpfs (op, fmt, n)
444 local x = {T.testC("pushfstring" .. op .. "; return *", fmt, n)} 444 local x = {T.testC("pushfstring" .. op .. "; return *", fmt, n)}