diff options
author | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2015-01-16 14:54:37 -0200 |
---|---|---|
committer | Roberto Ierusalimschy <roberto@inf.puc-rio.br> | 2015-01-16 14:54:37 -0200 |
commit | 7e2015a46df7976bddee313b994742e49e420714 (patch) | |
tree | 0b2db30f1214a478ccb3664d165c8a431f0d5850 /lvm.c | |
parent | 5b01cb39b5ec36c544152351c35c43149d9bbfec (diff) | |
download | lua-7e2015a46df7976bddee313b994742e49e420714.tar.gz lua-7e2015a46df7976bddee313b994742e49e420714.tar.bz2 lua-7e2015a46df7976bddee313b994742e49e420714.zip |
size of short strings stored in a single byte, to reduce the size
of struct 'TString'
Diffstat (limited to '')
-rw-r--r-- | lvm.c | 32 |
1 files changed, 19 insertions, 13 deletions
@@ -1,5 +1,5 @@ | |||
1 | /* | 1 | /* |
2 | ** $Id: lvm.c,v 2.231 2014/12/19 13:36:32 roberto Exp roberto $ | 2 | ** $Id: lvm.c,v 2.232 2014/12/27 20:30:38 roberto Exp roberto $ |
3 | ** Lua virtual machine | 3 | ** Lua virtual machine |
4 | ** See Copyright Notice in lua.h | 4 | ** See Copyright Notice in lua.h |
5 | */ | 5 | */ |
@@ -73,7 +73,7 @@ int luaV_tonumber_ (const TValue *obj, lua_Number *n) { | |||
73 | return 1; | 73 | return 1; |
74 | } | 74 | } |
75 | else if (cvt2num(obj) && /* string convertible to number? */ | 75 | else if (cvt2num(obj) && /* string convertible to number? */ |
76 | luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) { | 76 | luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) { |
77 | *n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */ | 77 | *n = nvalue(&v); /* convert result of 'luaO_str2num' to a float */ |
78 | return 1; | 78 | return 1; |
79 | } | 79 | } |
@@ -106,7 +106,7 @@ static int tointeger_aux (const TValue *obj, lua_Integer *p, int mode) { | |||
106 | return 1; | 106 | return 1; |
107 | } | 107 | } |
108 | else if (cvt2num(obj) && | 108 | else if (cvt2num(obj) && |
109 | luaO_str2num(svalue(obj), &v) == tsvalue(obj)->len + 1) { | 109 | luaO_str2num(svalue(obj), &v) == vslen(obj) + 1) { |
110 | obj = &v; | 110 | obj = &v; |
111 | goto again; /* convert result from 'luaO_str2num' to an integer */ | 111 | goto again; /* convert result from 'luaO_str2num' to an integer */ |
112 | } | 112 | } |
@@ -239,9 +239,9 @@ void luaV_settable (lua_State *L, const TValue *t, TValue *key, StkId val) { | |||
239 | */ | 239 | */ |
240 | static int l_strcmp (const TString *ls, const TString *rs) { | 240 | static int l_strcmp (const TString *ls, const TString *rs) { |
241 | const char *l = getstr(ls); | 241 | const char *l = getstr(ls); |
242 | size_t ll = ls->len; | 242 | size_t ll = tsslen(ls); |
243 | const char *r = getstr(rs); | 243 | const char *r = getstr(rs); |
244 | size_t lr = rs->len; | 244 | size_t lr = tsslen(rs); |
245 | for (;;) { /* for each segment */ | 245 | for (;;) { /* for each segment */ |
246 | int temp = strcoll(l, r); | 246 | int temp = strcoll(l, r); |
247 | if (temp != 0) /* not equal? */ | 247 | if (temp != 0) /* not equal? */ |
@@ -354,6 +354,8 @@ int luaV_equalobj (lua_State *L, const TValue *t1, const TValue *t2) { | |||
354 | #define tostring(L,o) \ | 354 | #define tostring(L,o) \ |
355 | (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1))) | 355 | (ttisstring(o) || (cvt2str(o) && (luaO_tostring(L, o), 1))) |
356 | 356 | ||
357 | #define isemptystr(o) (ttisshrstring(o) && tsvalue(o)->shrlen == 0) | ||
358 | |||
357 | /* | 359 | /* |
358 | ** Main operation for concatenation: concat 'total' values in the stack, | 360 | ** Main operation for concatenation: concat 'total' values in the stack, |
359 | ** from 'L->top - total' up to 'L->top - 1'. | 361 | ** from 'L->top - total' up to 'L->top - 1'. |
@@ -365,19 +367,19 @@ void luaV_concat (lua_State *L, int total) { | |||
365 | int n = 2; /* number of elements handled in this pass (at least 2) */ | 367 | int n = 2; /* number of elements handled in this pass (at least 2) */ |
366 | if (!(ttisstring(top-2) || cvt2str(top-2)) || !tostring(L, top-1)) | 368 | if (!(ttisstring(top-2) || cvt2str(top-2)) || !tostring(L, top-1)) |
367 | luaT_trybinTM(L, top-2, top-1, top-2, TM_CONCAT); | 369 | luaT_trybinTM(L, top-2, top-1, top-2, TM_CONCAT); |
368 | else if (tsvalue(top-1)->len == 0) /* second operand is empty? */ | 370 | else if (isemptystr(top - 1)) /* second operand is empty? */ |
369 | cast_void(tostring(L, top - 2)); /* result is first operand */ | 371 | cast_void(tostring(L, top - 2)); /* result is first operand */ |
370 | else if (ttisstring(top-2) && tsvalue(top-2)->len == 0) { | 372 | else if (isemptystr(top - 2)) { /* first operand is an empty string? */ |
371 | setobjs2s(L, top - 2, top - 1); /* result is second op. */ | 373 | setobjs2s(L, top - 2, top - 1); /* result is second op. */ |
372 | } | 374 | } |
373 | else { | 375 | else { |
374 | /* at least two non-empty string values; get as many as possible */ | 376 | /* at least two non-empty string values; get as many as possible */ |
375 | size_t tl = tsvalue(top-1)->len; | 377 | size_t tl = vslen(top - 1); |
376 | char *buffer; | 378 | char *buffer; |
377 | int i; | 379 | int i; |
378 | /* collect total length */ | 380 | /* collect total length */ |
379 | for (i = 1; i < total && tostring(L, top-i-1); i++) { | 381 | for (i = 1; i < total && tostring(L, top-i-1); i++) { |
380 | size_t l = tsvalue(top-i-1)->len; | 382 | size_t l = vslen(top - i - 1); |
381 | if (l >= (MAX_SIZE/sizeof(char)) - tl) | 383 | if (l >= (MAX_SIZE/sizeof(char)) - tl) |
382 | luaG_runerror(L, "string length overflow"); | 384 | luaG_runerror(L, "string length overflow"); |
383 | tl += l; | 385 | tl += l; |
@@ -386,7 +388,7 @@ void luaV_concat (lua_State *L, int total) { | |||
386 | tl = 0; | 388 | tl = 0; |
387 | n = i; | 389 | n = i; |
388 | do { /* copy all strings to buffer */ | 390 | do { /* copy all strings to buffer */ |
389 | size_t l = tsvalue(top-i)->len; | 391 | size_t l = vslen(top - i); |
390 | memcpy(buffer+tl, svalue(top-i), l * sizeof(char)); | 392 | memcpy(buffer+tl, svalue(top-i), l * sizeof(char)); |
391 | tl += l; | 393 | tl += l; |
392 | } while (--i > 0); | 394 | } while (--i > 0); |
@@ -403,7 +405,7 @@ void luaV_concat (lua_State *L, int total) { | |||
403 | */ | 405 | */ |
404 | void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { | 406 | void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { |
405 | const TValue *tm; | 407 | const TValue *tm; |
406 | switch (ttnov(rb)) { | 408 | switch (ttype(rb)) { |
407 | case LUA_TTABLE: { | 409 | case LUA_TTABLE: { |
408 | Table *h = hvalue(rb); | 410 | Table *h = hvalue(rb); |
409 | tm = fasttm(L, h->metatable, TM_LEN); | 411 | tm = fasttm(L, h->metatable, TM_LEN); |
@@ -411,8 +413,12 @@ void luaV_objlen (lua_State *L, StkId ra, const TValue *rb) { | |||
411 | setivalue(ra, luaH_getn(h)); /* else primitive len */ | 413 | setivalue(ra, luaH_getn(h)); /* else primitive len */ |
412 | return; | 414 | return; |
413 | } | 415 | } |
414 | case LUA_TSTRING: { | 416 | case LUA_TSHRSTR: { |
415 | setivalue(ra, tsvalue(rb)->len); | 417 | setivalue(ra, tsvalue(rb)->shrlen); |
418 | return; | ||
419 | } | ||
420 | case LUA_TLNGSTR: { | ||
421 | setivalue(ra, tsvalue(rb)->u.lnglen); | ||
416 | return; | 422 | return; |
417 | } | 423 | } |
418 | default: { /* try metamethod */ | 424 | default: { /* try metamethod */ |