mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
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.
This commit is contained in:
@@ -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_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);
|
||||
mrb_value mrb_str_dump(mrb_state *mrb, mrb_value str);
|
||||
|
||||
|
||||
@@ -599,7 +599,7 @@ new_lit(codegen_scope *s, mrb_value val)
|
||||
|
||||
switch (mrb_type(val)) {
|
||||
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;
|
||||
|
||||
#ifndef MRB_WITHOUT_FLOAT
|
||||
|
||||
+14
-14
@@ -43,21 +43,25 @@ offset_crc_body(void)
|
||||
|
||||
#ifndef MRB_WITHOUT_FLOAT
|
||||
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);
|
||||
mrb_int len = RSTRING_LEN(str);
|
||||
char buf[64];
|
||||
|
||||
/* `i`, `inf`, `infinity` */
|
||||
if (len > 0 && p[0] == 'i') return INFINITY;
|
||||
|
||||
/* `I`, `-inf`, `-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
|
||||
|
||||
mrb_value mrb_str_len_to_inum(mrb_state *mrb, const char *str, mrb_int len, mrb_int base, int badcheck);
|
||||
|
||||
static mrb_irep*
|
||||
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
|
||||
irep->pool = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value) * plen);
|
||||
|
||||
for (i = 0; i < plen; i++) {
|
||||
mrb_value s;
|
||||
const char *s;
|
||||
mrb_bool st = (flags & FLAG_SRC_MALLOC)==0;
|
||||
|
||||
tt = *src++; /* pool TT */
|
||||
pool_data_len = bin_to_uint16(src); /* pool data length */
|
||||
src += sizeof(uint16_t);
|
||||
if (flags & FLAG_SRC_MALLOC) {
|
||||
s = mrb_str_new(mrb, (char *)src, pool_data_len);
|
||||
}
|
||||
else {
|
||||
s = mrb_str_new_static(mrb, (char *)src, pool_data_len);
|
||||
}
|
||||
s = (const char*)src;
|
||||
src += pool_data_len;
|
||||
switch (tt) { /* pool data */
|
||||
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
|
||||
irep->pool[i] = num;
|
||||
#else
|
||||
@@ -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
|
||||
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;
|
||||
#endif
|
||||
|
||||
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;
|
||||
|
||||
default:
|
||||
|
||||
+4
-7
@@ -576,12 +576,9 @@ str_share(mrb_state *mrb, struct RString *orig, struct RString *s)
|
||||
}
|
||||
|
||||
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 *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->c = mrb->string_class;
|
||||
@@ -590,7 +587,7 @@ mrb_str_pool(mrb_state *mrb, mrb_value str)
|
||||
if (RSTR_EMBEDDABLE_P(len)) {
|
||||
str_init_embed(s, p, len);
|
||||
}
|
||||
else if (RSTR_NOFREE_P(orig)) {
|
||||
else if (nofree) {
|
||||
str_init_nofree(s, p, len);
|
||||
}
|
||||
else {
|
||||
@@ -2242,7 +2239,7 @@ mrb_str_split_m(mrb_state *mrb, mrb_value str)
|
||||
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)
|
||||
{
|
||||
const char *p = str;
|
||||
@@ -2492,7 +2489,7 @@ mrb_str_to_i(mrb_state *mrb, mrb_value self)
|
||||
|
||||
#ifndef MRB_WITHOUT_FLOAT
|
||||
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;
|
||||
char *end;
|
||||
|
||||
Reference in New Issue
Block a user