mirror of
https://github.com/mruby/mruby
synced 2026-06-08 16:11:16 +00:00
mange arena_idx smarter for smaller ARENA_SIZE; now default to 100 from 1024
This commit is contained in:
+1
-1
@@ -44,7 +44,7 @@ struct mrb_state;
|
||||
typedef void* (*mrb_allocf) (struct mrb_state *mrb, void*, size_t, void *ud);
|
||||
|
||||
#ifndef MRB_ARENA_SIZE
|
||||
#define MRB_ARENA_SIZE 1024
|
||||
#define MRB_ARENA_SIZE 100
|
||||
#endif
|
||||
|
||||
typedef struct {
|
||||
|
||||
@@ -9,8 +9,6 @@ extern const char mrblib_irep[];
|
||||
void
|
||||
mrb_init_mrblib(mrb_state *mrb)
|
||||
{
|
||||
int n = mrb_read_irep(mrb, mrblib_irep);
|
||||
|
||||
mrb_run(mrb, mrb_proc_new(mrb, mrb->irep[n]), mrb_top_self(mrb));
|
||||
mrb_load_irep(mrb, mrblib_irep);
|
||||
}
|
||||
|
||||
|
||||
+42
-47
@@ -60,12 +60,9 @@ typedef struct scope {
|
||||
short *lines;
|
||||
int icapa;
|
||||
|
||||
mrb_value *pool;
|
||||
int plen;
|
||||
mrb_irep *irep;
|
||||
int pcapa;
|
||||
|
||||
mrb_sym *syms;
|
||||
int slen;
|
||||
int scapa;
|
||||
|
||||
int nlocals;
|
||||
int nregs;
|
||||
@@ -75,7 +72,7 @@ typedef struct scope {
|
||||
} codegen_scope;
|
||||
|
||||
static codegen_scope* scope_new(mrb_state *mrb, codegen_scope *prev, node *lv);
|
||||
static void scope_finish(codegen_scope *s, int idx);
|
||||
static void scope_finish(codegen_scope *s);
|
||||
static struct loopinfo *loop_push(codegen_scope *s, enum looptype t);
|
||||
static void loop_break(codegen_scope *s, node *tree);
|
||||
static void loop_pop(codegen_scope *s, int val);
|
||||
@@ -413,15 +410,17 @@ new_lit(codegen_scope *s, mrb_value val)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i=0; i<s->plen; i++) {
|
||||
if (mrb_obj_equal(s->mrb, s->pool[i], val)) return i;
|
||||
for (i=0; i<s->irep->plen; i++) {
|
||||
if (mrb_obj_equal(s->mrb, s->irep->pool[i], val)) return i;
|
||||
}
|
||||
if (s->plen == s->pcapa) {
|
||||
if (s->irep->plen == s->pcapa) {
|
||||
s->pcapa *= 2;
|
||||
s->pool = (mrb_value *)codegen_realloc(s, s->pool, sizeof(mrb_value)*s->pcapa);
|
||||
s->irep->pool = (mrb_value *)codegen_realloc(s, s->irep->pool, sizeof(mrb_value)*s->pcapa);
|
||||
}
|
||||
s->pool[s->plen] = val;
|
||||
return s->plen++;
|
||||
s->irep->pool[s->irep->plen] = val;
|
||||
i = s->irep->plen++;
|
||||
|
||||
return i;
|
||||
}
|
||||
|
||||
static inline int
|
||||
@@ -429,17 +428,17 @@ new_msym(codegen_scope *s, mrb_sym sym)
|
||||
{
|
||||
int i, len;
|
||||
|
||||
len = s->slen;
|
||||
len = s->irep->slen;
|
||||
if (len > 255) len = 255;
|
||||
for (i=0; i<len; i++) {
|
||||
if (s->syms[i] == sym) return i;
|
||||
if (s->syms[i] == 0) break;
|
||||
if (s->irep->syms[i] == sym) return i;
|
||||
if (s->irep->syms[i] == 0) break;
|
||||
}
|
||||
if (i > 255) {
|
||||
codegen_error(s, "too many symbols (max 256)");
|
||||
}
|
||||
s->syms[i] = sym;
|
||||
if (i == s->slen) s->slen++;
|
||||
s->irep->syms[i] = sym;
|
||||
if (i == s->irep->slen) s->irep->slen++;
|
||||
return i;
|
||||
}
|
||||
|
||||
@@ -448,19 +447,19 @@ new_sym(codegen_scope *s, mrb_sym sym)
|
||||
{
|
||||
int i;
|
||||
|
||||
for (i=0; i<s->slen; i++) {
|
||||
if (s->syms[i] == sym) return i;
|
||||
for (i=0; i<s->irep->slen; i++) {
|
||||
if (s->irep->syms[i] == sym) return i;
|
||||
}
|
||||
if (s->slen > 125 && s->slen < 256) {
|
||||
s->syms = (mrb_sym *)codegen_realloc(s, s->syms, sizeof(mrb_sym)*65536);
|
||||
for (i = 0; i < 256 - s->slen; i++) {
|
||||
if (s->irep->slen > 125 && s->irep->slen < 256) {
|
||||
s->irep->syms = (mrb_sym *)codegen_realloc(s, s->irep->syms, sizeof(mrb_sym)*65536);
|
||||
for (i = 0; i < 256 - s->irep->slen; i++) {
|
||||
static const mrb_sym mrb_sym_zero = { 0 };
|
||||
s->syms[i + s->slen] = mrb_sym_zero;
|
||||
s->irep->syms[i + s->irep->slen] = mrb_sym_zero;
|
||||
}
|
||||
s->slen = 256;
|
||||
s->irep->slen = 256;
|
||||
}
|
||||
s->syms[s->slen] = sym;
|
||||
return s->slen++;
|
||||
s->irep->syms[s->irep->slen] = sym;
|
||||
return s->irep->slen++;
|
||||
}
|
||||
|
||||
static int
|
||||
@@ -527,7 +526,7 @@ for_body(codegen_scope *s, node *tree)
|
||||
genop_peep(s, MKOP_AB(OP_RETURN, cursp(), OP_R_NORMAL), NOVAL);
|
||||
}
|
||||
loop_pop(s, NOVAL);
|
||||
scope_finish(s, idx);
|
||||
scope_finish(s);
|
||||
s = prev;
|
||||
genop(s, MKOP_Abc(OP_LAMBDA, cursp(), idx - base, OP_L_BLOCK));
|
||||
pop();
|
||||
@@ -619,7 +618,7 @@ lambda_body(codegen_scope *s, node *tree, int blk)
|
||||
if (blk) {
|
||||
loop_pop(s, NOVAL);
|
||||
}
|
||||
scope_finish(s, idx);
|
||||
scope_finish(s);
|
||||
|
||||
return idx - base;
|
||||
}
|
||||
@@ -643,7 +642,7 @@ scope_body(codegen_scope *s, node *tree)
|
||||
genop_peep(scope, MKOP_AB(OP_RETURN, scope->sp, OP_R_NORMAL), NOVAL);
|
||||
}
|
||||
}
|
||||
scope_finish(scope, idx);
|
||||
scope_finish(scope);
|
||||
|
||||
return idx - s->idx;
|
||||
}
|
||||
@@ -1813,8 +1812,10 @@ codegen(codegen_scope *s, node *tree, int val)
|
||||
if (val) {
|
||||
char *p = (char*)tree->car;
|
||||
size_t len = (intptr_t)tree->cdr;
|
||||
int ai = s->mrb->arena_idx;
|
||||
int off = new_lit(s, mrb_str_new(s->mrb, p, len));
|
||||
|
||||
s->mrb->arena_idx = ai;
|
||||
genop(s, MKOP_ABx(OP_STRING, cursp(), off));
|
||||
push();
|
||||
}
|
||||
@@ -2055,7 +2056,6 @@ scope_new(mrb_state *mrb, codegen_scope *prev, node *lv)
|
||||
static const codegen_scope codegen_scope_zero = { 0 };
|
||||
mrb_pool *pool = mrb_pool_open(mrb);
|
||||
codegen_scope *p = (codegen_scope *)mrb_pool_alloc(pool, sizeof(codegen_scope));
|
||||
mrb_irep *irep;
|
||||
|
||||
if (!p) return 0;
|
||||
*p = codegen_scope_zero;
|
||||
@@ -2066,23 +2066,25 @@ scope_new(mrb_state *mrb, codegen_scope *prev, node *lv)
|
||||
p->ainfo = -1;
|
||||
p->mscope = 0;
|
||||
|
||||
p->mrb = prev->mrb;
|
||||
p->irep = mrb_add_irep(mrb);
|
||||
p->idx = p->irep->idx;
|
||||
|
||||
p->icapa = 1024;
|
||||
p->iseq = (mrb_code*)mrb_malloc(mrb, sizeof(mrb_code)*p->icapa);
|
||||
|
||||
p->pcapa = 32;
|
||||
p->pool = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value)*p->pcapa);
|
||||
p->irep->pool = (mrb_value*)mrb_malloc(mrb, sizeof(mrb_value)*p->pcapa);
|
||||
p->irep->plen = 0;
|
||||
|
||||
p->syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym)*256);
|
||||
p->scapa = 256;
|
||||
p->irep->syms = (mrb_sym*)mrb_malloc(mrb, sizeof(mrb_sym)*256);
|
||||
p->irep->slen = 0;
|
||||
|
||||
p->lv = lv;
|
||||
p->sp += node_len(lv)+1; /* add self */
|
||||
p->nlocals = p->sp;
|
||||
p->ai = mrb->arena_idx;
|
||||
|
||||
irep = mrb_add_irep(mrb);
|
||||
p->idx = irep->idx;
|
||||
|
||||
p->filename = prev->filename;
|
||||
if (p->filename) {
|
||||
p->lines = (short*)mrb_malloc(mrb, sizeof(short)*p->icapa);
|
||||
@@ -2091,12 +2093,11 @@ scope_new(mrb_state *mrb, codegen_scope *prev, node *lv)
|
||||
}
|
||||
|
||||
static void
|
||||
scope_finish(codegen_scope *s, int idx)
|
||||
scope_finish(codegen_scope *s)
|
||||
{
|
||||
mrb_state *mrb = s->mrb;
|
||||
mrb_irep *irep;
|
||||
mrb_irep *irep = s->irep;
|
||||
|
||||
irep = mrb->irep[idx];
|
||||
irep->flags = 0;
|
||||
if (s->iseq) {
|
||||
irep->iseq = (mrb_code *)codegen_realloc(s, s->iseq, sizeof(mrb_code)*s->pc);
|
||||
@@ -2108,14 +2109,8 @@ scope_finish(codegen_scope *s, int idx)
|
||||
irep->lines = 0;
|
||||
}
|
||||
}
|
||||
if (s->pool) {
|
||||
irep->pool = (mrb_value *)codegen_realloc(s, s->pool, sizeof(mrb_value)*s->plen);
|
||||
irep->plen = s->plen;
|
||||
}
|
||||
if (s->syms) {
|
||||
irep->syms = (mrb_sym *)codegen_realloc(s, s->syms, sizeof(mrb_sym)*s->slen);
|
||||
irep->slen = s->slen;
|
||||
}
|
||||
irep->pool = (mrb_value *)codegen_realloc(s, irep->pool, sizeof(mrb_value)*irep->plen);
|
||||
irep->syms = (mrb_sym *)codegen_realloc(s, irep->syms, sizeof(mrb_sym)*irep->slen);
|
||||
if (s->filename) {
|
||||
irep->filename = s->filename;
|
||||
}
|
||||
|
||||
+22
-24
@@ -28,44 +28,42 @@ void mrb_init_time(mrb_state*);
|
||||
void mrb_init_math(mrb_state*);
|
||||
void mrb_init_mrblib(mrb_state*);
|
||||
|
||||
|
||||
#define DONE mrb_gc_arena_restore(mrb, 0);
|
||||
void
|
||||
mrb_init_core(mrb_state *mrb)
|
||||
{
|
||||
mrb_init_symtbl(mrb);
|
||||
mrb_init_symtbl(mrb); DONE;
|
||||
|
||||
mrb_init_class(mrb);
|
||||
mrb_init_object(mrb);
|
||||
mrb_init_kernel(mrb);
|
||||
mrb_init_comparable(mrb);
|
||||
mrb_init_enumerable(mrb);
|
||||
mrb_init_class(mrb); DONE;
|
||||
mrb_init_object(mrb); DONE;
|
||||
mrb_init_kernel(mrb); DONE;
|
||||
mrb_init_comparable(mrb); DONE;
|
||||
mrb_init_enumerable(mrb); DONE;
|
||||
|
||||
mrb_init_symbol(mrb);
|
||||
mrb_init_exception(mrb);
|
||||
mrb_init_proc(mrb);
|
||||
mrb_init_string(mrb);
|
||||
mrb_init_array(mrb);
|
||||
mrb_init_hash(mrb);
|
||||
mrb_init_numeric(mrb);
|
||||
mrb_init_range(mrb);
|
||||
mrb_init_symbol(mrb); DONE;
|
||||
mrb_init_exception(mrb); DONE;
|
||||
mrb_init_proc(mrb); DONE;
|
||||
mrb_init_string(mrb); DONE;
|
||||
mrb_init_array(mrb); DONE;
|
||||
mrb_init_hash(mrb); DONE;
|
||||
mrb_init_numeric(mrb); DONE;
|
||||
mrb_init_range(mrb); DONE;
|
||||
#ifdef ENABLE_STRUCT
|
||||
mrb_init_struct(mrb);
|
||||
mrb_init_struct(mrb); DONE;
|
||||
#endif
|
||||
mrb_init_gc(mrb);
|
||||
mrb_init_gc(mrb); DONE;
|
||||
#ifdef ENABLE_REGEXP
|
||||
mrb_init_regexp(mrb);
|
||||
mrb_init_regexp(mrb); DONE;
|
||||
#endif
|
||||
#ifdef ENABLE_STDIO
|
||||
mrb_init_print(mrb);
|
||||
mrb_init_print(mrb); DONE;
|
||||
#endif
|
||||
#ifdef ENABLE_TIME
|
||||
mrb_init_time(mrb);
|
||||
mrb_init_time(mrb); DONE;
|
||||
#endif
|
||||
#ifdef ENABLE_MATH
|
||||
mrb_init_math(mrb);
|
||||
mrb_init_math(mrb); DONE;
|
||||
#endif
|
||||
|
||||
mrb_init_mrblib(mrb);
|
||||
|
||||
mrb_gc_arena_restore(mrb, 0);
|
||||
mrb_init_mrblib(mrb); DONE;
|
||||
}
|
||||
|
||||
+10
-6
@@ -329,6 +329,7 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, uint32_t* len)
|
||||
uint16_t crc, tt, pdl, snl, offset, bufsize=MRB_DUMP_DEFAULT_STR_LEN;
|
||||
mrb_int fix_num;
|
||||
mrb_float f;
|
||||
int plen;
|
||||
int ai = mrb_gc_arena_save(mrb);
|
||||
mrb_irep *irep = mrb_add_irep(mrb);
|
||||
|
||||
@@ -379,16 +380,16 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, uint32_t* len)
|
||||
|
||||
//POOL BLOCK
|
||||
pStart = src;
|
||||
irep->plen = bin_to_uint32(src); //pool length
|
||||
plen = bin_to_uint32(src); //pool length
|
||||
src += MRB_DUMP_SIZE_OF_LONG;
|
||||
if (irep->plen > 0) {
|
||||
irep->pool = (mrb_value *)mrb_malloc(mrb, sizeof(mrb_value) * irep->plen);
|
||||
if (plen > 0) {
|
||||
irep->pool = (mrb_value *)mrb_malloc(mrb, sizeof(mrb_value) * plen);
|
||||
if (irep->pool == NULL) {
|
||||
ret = MRB_DUMP_INVALID_IREP;
|
||||
goto error_exit;
|
||||
}
|
||||
|
||||
for (i=0; i<irep->plen; i++) {
|
||||
for (i=0; i<plen; i++) {
|
||||
tt = *src; //pool TT
|
||||
src += sizeof(unsigned char);
|
||||
pdl = bin_to_uint16(src); //pool data length
|
||||
@@ -431,6 +432,8 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, uint32_t* len)
|
||||
irep->pool[i] = mrb_nil_value();
|
||||
break;
|
||||
}
|
||||
irep->plen++;
|
||||
mrb_gc_arena_restore(mrb, ai);
|
||||
}
|
||||
}
|
||||
crc = calc_crc_16_ccitt((unsigned char*)pStart, src - pStart); //Calculate CRC
|
||||
@@ -486,7 +489,6 @@ read_rite_irep_record(mrb_state *mrb, unsigned char *src, uint32_t* len)
|
||||
|
||||
*len = src - recordStart;
|
||||
error_exit:
|
||||
mrb_gc_arena_restore(mrb, ai);
|
||||
if (buf)
|
||||
mrb_free(mrb, buf);
|
||||
|
||||
@@ -510,6 +512,7 @@ mrb_read_irep(mrb_state *mrb, const char *bin)
|
||||
//Read File Header Section
|
||||
if ((nirep = read_rite_header(mrb, src, &bin_header)) < 0)
|
||||
return nirep;
|
||||
|
||||
src += sizeof(bin_header) + MRB_DUMP_SIZE_OF_SHORT; //header + crc
|
||||
|
||||
//Read Binary Data Section
|
||||
@@ -525,7 +528,7 @@ mrb_read_irep(mrb_state *mrb, const char *bin)
|
||||
|
||||
error_exit:
|
||||
if (ret != MRB_DUMP_OK) {
|
||||
for (n=0,i=sirep; n<nirep; n++,i++) {
|
||||
for (n=0,i=sirep; i<mrb->irep_len; n++,i++) {
|
||||
if (mrb->irep[i]) {
|
||||
if (mrb->irep[i]->iseq)
|
||||
mrb_free(mrb, mrb->irep[i]->iseq);
|
||||
@@ -539,6 +542,7 @@ error_exit:
|
||||
mrb_free(mrb, mrb->irep[i]);
|
||||
}
|
||||
}
|
||||
// mrb->irep_len = sirep;
|
||||
return ret;
|
||||
}
|
||||
return sirep + hex_to_uint8(bin_header.sirep);
|
||||
|
||||
Reference in New Issue
Block a user