Avoid creating temporary objects in `read_irep_record_1`; close #4920

The basic idea of this change is from @dearblue.

Note: the arguments of `mrb_str_pool()` have changed, but the function
is provided for internal use (No `MRB_API`). So basically you don't have
to worry about the change.
parent 282f907f
...@@ -447,7 +447,7 @@ MRB_API int mrb_str_cmp(mrb_state *mrb, mrb_value str1, mrb_value str2); ...@@ -447,7 +447,7 @@ MRB_API int mrb_str_cmp(mrb_state *mrb, mrb_value str1, mrb_value str2);
*/ */
MRB_API char *mrb_str_to_cstr(mrb_state *mrb, mrb_value str); MRB_API char *mrb_str_to_cstr(mrb_state *mrb, mrb_value str);
mrb_value mrb_str_pool(mrb_state *mrb, mrb_value str); mrb_value mrb_str_pool(mrb_state *mrb, const char *s, mrb_int len, mrb_bool nofree);
uint32_t mrb_str_hash(mrb_state *mrb, mrb_value str); uint32_t mrb_str_hash(mrb_state *mrb, mrb_value str);
mrb_value mrb_str_dump(mrb_state *mrb, mrb_value str); mrb_value mrb_str_dump(mrb_state *mrb, mrb_value str);
......
...@@ -599,7 +599,7 @@ new_lit(codegen_scope *s, mrb_value val) ...@@ -599,7 +599,7 @@ new_lit(codegen_scope *s, mrb_value val)
switch (mrb_type(val)) { switch (mrb_type(val)) {
case MRB_TT_STRING: case MRB_TT_STRING:
*pv = mrb_str_pool(s->mrb, val); *pv = mrb_str_pool(s->mrb, RSTRING_PTR(val), RSTRING_LEN(val), RSTR_NOFREE_P(RSTRING(val)));
break; break;
#ifndef MRB_WITHOUT_FLOAT #ifndef MRB_WITHOUT_FLOAT
......
...@@ -43,21 +43,25 @@ offset_crc_body(void) ...@@ -43,21 +43,25 @@ offset_crc_body(void)
#ifndef MRB_WITHOUT_FLOAT #ifndef MRB_WITHOUT_FLOAT
static double static double
str_to_double(mrb_state *mrb, mrb_value str) str_to_double(mrb_state *mrb, const char *p, size_t len)
{ {
const char *p = RSTRING_PTR(str); char buf[64];
mrb_int len = RSTRING_LEN(str);
/* `i`, `inf`, `infinity` */ /* `i`, `inf`, `infinity` */
if (len > 0 && p[0] == 'i') return INFINITY; if (len > 0 && p[0] == 'i') return INFINITY;
/* `I`, `-inf`, `-infinity` */ /* `I`, `-inf`, `-infinity` */
if (p[0] == 'I' || (len > 1 && p[0] == '-' && p[1] == 'i')) return -INFINITY; if (p[0] == 'I' || (len > 1 && p[0] == '-' && p[1] == 'i')) return -INFINITY;
mrb_assert(len < sizeof(buf));
strncpy(buf, p, len);
buf[len] = '\0';
return mrb_str_to_dbl(mrb, str, TRUE); return mrb_cstr_to_dbl(mrb, buf, TRUE);
} }
#endif #endif
mrb_value mrb_str_len_to_inum(mrb_state *mrb, const char *str, mrb_int len, mrb_int base, int badcheck);
static mrb_irep* static mrb_irep*
read_irep_record_1(mrb_state *mrb, const uint8_t *bin, size_t *len, uint8_t flags) read_irep_record_1(mrb_state *mrb, const uint8_t *bin, size_t *len, uint8_t flags)
{ {
...@@ -119,21 +123,17 @@ read_irep_record_1(mrb_state *mrb, const uint8_t *bin, size_t *len, uint8_t flag ...@@ -119,21 +123,17 @@ read_irep_record_1(mrb_state *mrb, const uint8_t *bin, size_t *len, uint8_t flag
irep->pool = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value) * plen); irep->pool = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value) * plen);
for (i = 0; i < plen; i++) { for (i = 0; i < plen; i++) {
mrb_value s; const char *s;
mrb_bool st = (flags & FLAG_SRC_MALLOC)==0;
tt = *src++; /* pool TT */ tt = *src++; /* pool TT */
pool_data_len = bin_to_uint16(src); /* pool data length */ pool_data_len = bin_to_uint16(src); /* pool data length */
src += sizeof(uint16_t); src += sizeof(uint16_t);
if (flags & FLAG_SRC_MALLOC) { s = (const char*)src;
s = mrb_str_new(mrb, (char *)src, pool_data_len);
}
else {
s = mrb_str_new_static(mrb, (char *)src, pool_data_len);
}
src += pool_data_len; src += pool_data_len;
switch (tt) { /* pool data */ switch (tt) { /* pool data */
case IREP_TT_FIXNUM: { case IREP_TT_FIXNUM: {
mrb_value num = mrb_str_to_inum(mrb, s, 10, FALSE); mrb_value num = mrb_str_len_to_inum(mrb, s, pool_data_len, 10, FALSE);
#ifdef MRB_WITHOUT_FLOAT #ifdef MRB_WITHOUT_FLOAT
irep->pool[i] = num; irep->pool[i] = num;
#else #else
...@@ -144,12 +144,12 @@ read_irep_record_1(mrb_state *mrb, const uint8_t *bin, size_t *len, uint8_t flag ...@@ -144,12 +144,12 @@ read_irep_record_1(mrb_state *mrb, const uint8_t *bin, size_t *len, uint8_t flag
#ifndef MRB_WITHOUT_FLOAT #ifndef MRB_WITHOUT_FLOAT
case IREP_TT_FLOAT: case IREP_TT_FLOAT:
irep->pool[i] = mrb_float_pool(mrb, str_to_double(mrb, s)); irep->pool[i] = mrb_float_pool(mrb, str_to_double(mrb, s, pool_data_len));
break; break;
#endif #endif
case IREP_TT_STRING: case IREP_TT_STRING:
irep->pool[i] = mrb_str_pool(mrb, s); irep->pool[i] = mrb_str_pool(mrb, s, pool_data_len, st);
break; break;
default: default:
......
...@@ -576,12 +576,9 @@ str_share(mrb_state *mrb, struct RString *orig, struct RString *s) ...@@ -576,12 +576,9 @@ str_share(mrb_state *mrb, struct RString *orig, struct RString *s)
} }
mrb_value mrb_value
mrb_str_pool(mrb_state *mrb, mrb_value str) mrb_str_pool(mrb_state *mrb, const char *p, mrb_int len, mrb_bool nofree)
{ {
struct RString *s = (struct RString *)mrb_malloc(mrb, sizeof(struct RString)); struct RString *s = (struct RString *)mrb_malloc(mrb, sizeof(struct RString));
struct RString *orig = mrb_str_ptr(str);
const char *p = RSTR_PTR(orig);
size_t len = (size_t)RSTR_LEN(orig);
s->tt = MRB_TT_STRING; s->tt = MRB_TT_STRING;
s->c = mrb->string_class; s->c = mrb->string_class;
...@@ -590,7 +587,7 @@ mrb_str_pool(mrb_state *mrb, mrb_value str) ...@@ -590,7 +587,7 @@ mrb_str_pool(mrb_state *mrb, mrb_value str)
if (RSTR_EMBEDDABLE_P(len)) { if (RSTR_EMBEDDABLE_P(len)) {
str_init_embed(s, p, len); str_init_embed(s, p, len);
} }
else if (RSTR_NOFREE_P(orig)) { else if (nofree) {
str_init_nofree(s, p, len); str_init_nofree(s, p, len);
} }
else { else {
...@@ -2242,7 +2239,7 @@ mrb_str_split_m(mrb_state *mrb, mrb_value str) ...@@ -2242,7 +2239,7 @@ mrb_str_split_m(mrb_state *mrb, mrb_value str)
return result; return result;
} }
static mrb_value mrb_value
mrb_str_len_to_inum(mrb_state *mrb, const char *str, mrb_int len, mrb_int base, int badcheck) mrb_str_len_to_inum(mrb_state *mrb, const char *str, mrb_int len, mrb_int base, int badcheck)
{ {
const char *p = str; const char *p = str;
...@@ -2492,7 +2489,7 @@ mrb_str_to_i(mrb_state *mrb, mrb_value self) ...@@ -2492,7 +2489,7 @@ mrb_str_to_i(mrb_state *mrb, mrb_value self)
#ifndef MRB_WITHOUT_FLOAT #ifndef MRB_WITHOUT_FLOAT
MRB_API double MRB_API double
mrb_cstr_to_dbl(mrb_state *mrb, const char * s, mrb_bool badcheck) mrb_cstr_to_dbl(mrb_state *mrb, const char *s, mrb_bool badcheck)
{ {
const char *p = s; const char *p = s;
char *end; char *end;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment