Remove `filename`&`lines` from `mrb_irep` struct.
This patch slightly reduce memory consumption (2% for my test).
MODIFY
include/mruby/debug.h
include/mruby/debug.h
@@ -55,10 +55,11 @@ MRB_API const char *mrb_debug_get_filename(mrb_irep *irep, ptrdiff_t pc);
*/
MRB_API int32_t mrb_debug_get_line(mrb_irep *irep, ptrdiff_t pc);
+MRB_API mrb_irep_debug_info *mrb_debug_info_alloc(mrb_state *mrb, mrb_irep *irep);
MRB_API mrb_irep_debug_info_file *mrb_debug_info_append_file(
- mrb_state *mrb, mrb_irep *irep,
+ mrb_state *mrb, mrb_irep_debug_info *info,
+ const char *filename, uint16_t *lines,
uint32_t start_pos, uint32_t end_pos);
-MRB_API mrb_irep_debug_info *mrb_debug_info_alloc(mrb_state *mrb, mrb_irep *irep);
MRB_API void mrb_debug_info_free(mrb_state *mrb, mrb_irep_debug_info *d);
MRB_END_DECL

MODIFY
include/mruby/irep.h
include/mruby/irep.h
@@ -39,9 +39,6 @@ typedef struct mrb_irep {
struct mrb_locals *lv;
/* debug info */
- mrb_bool own_filename;
- const char *filename;
- uint16_t *lines;
struct mrb_irep_debug_info* debug_info;
uint16_t ilen, plen, slen, rlen;

MODIFY
mrbgems/mruby-compiler/core/codegen.c
mrbgems/mruby-compiler/core/codegen.c
@@ -124,15 +124,6 @@ codegen_palloc(codegen_scope *s, size_t len)
return p;
}
-static void*
-codegen_malloc(codegen_scope *s, size_t len)
-{
- void *p = mrb_malloc_simple(s->mrb, len);
-
- if (!p) codegen_error(s, "mrb_malloc");
- return p;
-}
-
static void*
codegen_realloc(codegen_scope *s, void *p, size_t len)
{
@@ -162,7 +153,6 @@ emit_B(codegen_scope *s, uint32_t pc, uint8_t i)
s->iseq = (mrb_code *)codegen_realloc(s, s->iseq, sizeof(mrb_code)*s->icapa);
if (s->lines) {
s->lines = (uint16_t*)codegen_realloc(s, s->lines, sizeof(uint16_t)*s->icapa);
- s->irep->lines = s->lines;
}
}
if (s->lines) {
@@ -1371,8 +1361,10 @@ codegen(codegen_scope *s, node *tree, int val)
codegen_error(s, "too complex expression");
}
if (s->irep && s->filename_index != tree->filename_index) {
- s->irep->filename = mrb_parser_get_filename(s->parser, s->filename_index);
- mrb_debug_info_append_file(s->mrb, s->irep, s->debug_start_pos, s->pc);
+ const char *filename = mrb_parser_get_filename(s->parser, s->filename_index);
+
+ mrb_debug_info_append_file(s->mrb, s->irep->debug_info,
+ filename, s->lines, s->debug_start_pos, s->pc);
s->debug_start_pos = s->pc;
s->filename_index = tree->filename_index;
s->filename = mrb_parser_get_filename(s->parser, tree->filename_index);
@@ -2965,8 +2957,6 @@ scope_new(mrb_state *mrb, codegen_scope *prev, node *lv)
p->debug_start_pos = 0;
if (p->filename) {
mrb_debug_info_alloc(mrb, p->irep);
- p->irep->filename = p->filename;
- p->irep->lines = p->lines;
}
else {
p->irep->debug_info = NULL;
@@ -2984,34 +2974,22 @@ scope_finish(codegen_scope *s)
{
mrb_state *mrb = s->mrb;
mrb_irep *irep = s->irep;
- size_t fname_len;
- char *fname;
irep->flags = 0;
if (s->iseq) {
irep->iseq = (mrb_code *)codegen_realloc(s, s->iseq, sizeof(mrb_code)*s->pc);
irep->ilen = s->pc;
- if (s->lines) {
- irep->lines = (uint16_t *)codegen_realloc(s, s->lines, sizeof(uint16_t)*s->pc);
- }
- else {
- irep->lines = 0;
- }
}
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);
irep->reps = (mrb_irep**)codegen_realloc(s, irep->reps, sizeof(mrb_irep*)*irep->rlen);
if (s->filename) {
- irep->filename = mrb_parser_get_filename(s->parser, s->filename_index);
- mrb_debug_info_append_file(mrb, irep, s->debug_start_pos, s->pc);
+ const char *filename = mrb_parser_get_filename(s->parser, s->filename_index);
- fname_len = strlen(s->filename);
- fname = (char*)codegen_malloc(s, fname_len + 1);
- memcpy(fname, s->filename, fname_len);
- fname[fname_len] = '\0';
- irep->filename = fname;
- irep->own_filename = TRUE;
+ mrb_debug_info_append_file(s->mrb, s->irep->debug_info,
+ filename, s->lines, s->debug_start_pos, s->pc);
}
+ mrb_free(s->mrb, s->lines);
irep->nlocals = s->nlocals;
irep->nregs = s->nregs;

MODIFY
src/debug.c
src/debug.c
@@ -55,7 +55,7 @@ mrb_debug_get_filename(mrb_irep *irep, ptrdiff_t pc)
{
if (irep && pc >= 0 && pc < irep->ilen) {
mrb_irep_debug_info_file* f = NULL;
- if (!irep->debug_info) { return irep->filename; }
+ if (!irep->debug_info) return NULL;
else if ((f = get_file(irep->debug_info, (uint32_t)pc))) {
return f->filename;
}
@@ -69,7 +69,7 @@ mrb_debug_get_line(mrb_irep *irep, ptrdiff_t pc)
if (irep && pc >= 0 && pc < irep->ilen) {
mrb_irep_debug_info_file* f = NULL;
if (!irep->debug_info) {
- return irep->lines? irep->lines[pc] : -1;
+ return -1;
}
else if ((f = get_file(irep->debug_info, (uint32_t)pc))) {
switch (f->line_type) {
@@ -122,24 +122,22 @@ mrb_debug_info_alloc(mrb_state *mrb, mrb_irep *irep)
}
MRB_API mrb_irep_debug_info_file*
-mrb_debug_info_append_file(mrb_state *mrb, mrb_irep *irep,
+mrb_debug_info_append_file(mrb_state *mrb, mrb_irep_debug_info *info,
+ const char *filename, uint16_t *lines,
uint32_t start_pos, uint32_t end_pos)
{
- mrb_irep_debug_info *info;
mrb_irep_debug_info_file *ret;
uint32_t file_pc_count;
size_t fn_len;
mrb_int len;
uint32_t i;
- if (!irep->debug_info) { return NULL; }
-
- mrb_assert(irep->filename);
- mrb_assert(irep->lines);
+ if (!info) { return NULL; }
- info = irep->debug_info;
+ mrb_assert(filename);
+ mrb_assert(lines);
- if (info->flen > 0 && strcmp(irep->filename, info->files[info->flen - 1]->filename) == 0) {
+ if (info->flen > 0 && strcmp(filename, info->files[info->flen - 1]->filename) == 0) {
return NULL;
}
@@ -156,12 +154,12 @@ mrb_debug_info_append_file(mrb_state *mrb, mrb_irep *irep,
ret->start_pos = start_pos;
info->pc_count = end_pos;
- fn_len = strlen(irep->filename);
- ret->filename_sym = mrb_intern(mrb, irep->filename, fn_len);
+ fn_len = strlen(filename);
+ ret->filename_sym = mrb_intern(mrb, filename, fn_len);
len = 0;
ret->filename = mrb_sym2name_len(mrb, ret->filename_sym, &len);
- ret->line_type = select_line_type(irep->lines + start_pos, end_pos - start_pos);
+ ret->line_type = select_line_type(lines + start_pos, end_pos - start_pos);
ret->lines.ptr = NULL;
switch (ret->line_type) {
@@ -169,7 +167,7 @@ mrb_debug_info_append_file(mrb_state *mrb, mrb_irep *irep,
ret->line_entry_count = file_pc_count;
ret->lines.ary = (uint16_t*)mrb_malloc(mrb, sizeof(uint16_t) * file_pc_count);
for (i = 0; i < file_pc_count; ++i) {
- ret->lines.ary[i] = irep->lines[start_pos + i];
+ ret->lines.ary[i] = lines[start_pos + i];
}
break;
@@ -179,18 +177,18 @@ mrb_debug_info_append_file(mrb_state *mrb, mrb_irep *irep,
ret->lines.flat_map = (mrb_irep_debug_info_line*)mrb_malloc(mrb, sizeof(mrb_irep_debug_info_line) * 1);
ret->line_entry_count = 0;
for (i = 0; i < file_pc_count; ++i) {
- if (irep->lines[start_pos + i] == prev_line) { continue; }
+ if (lines[start_pos + i] == prev_line) { continue; }
ret->lines.flat_map = (mrb_irep_debug_info_line*)mrb_realloc(
mrb, ret->lines.flat_map,
sizeof(mrb_irep_debug_info_line) * (ret->line_entry_count + 1));
m.start_pos = start_pos + i;
- m.line = irep->lines[start_pos + i];
+ m.line = lines[start_pos + i];
ret->lines.flat_map[ret->line_entry_count] = m;
/* update */
++ret->line_entry_count;
- prev_line = irep->lines[start_pos + i];
+ prev_line = lines[start_pos + i];
}
} break;

MODIFY
src/dump.c
src/dump.c
@@ -353,118 +353,6 @@ write_section_irep(mrb_state *mrb, mrb_irep *irep, uint8_t *bin, size_t *len_p,
return MRB_DUMP_OK;
}
-static int
-write_section_lineno_header(mrb_state *mrb, size_t section_size, uint8_t *bin)
-{
- struct rite_section_lineno_header *header = (struct rite_section_lineno_header*)bin;
-
- memcpy(header->section_ident, RITE_SECTION_LINENO_IDENT, sizeof(header->section_ident));
- uint32_to_bin((uint32_t)section_size, header->section_size);
-
- return MRB_DUMP_OK;
-}
-
-static size_t
-get_lineno_record_size(mrb_state *mrb, mrb_irep *irep)
-{
- size_t size = 0;
-
- size += sizeof(uint32_t); /* record size */
- size += sizeof(uint16_t); /* filename size */
- if (irep->filename) {
- size += strlen(irep->filename); /* filename */
- }
- size += sizeof(uint32_t); /* niseq */
- if (irep->lines) {
- size += sizeof(uint16_t) * irep->ilen; /* lineno */
- }
-
- return size;
-}
-
-static size_t
-write_lineno_record_1(mrb_state *mrb, mrb_irep *irep, uint8_t* bin)
-{
- uint8_t *cur = bin;
- int iseq_no;
- size_t filename_len;
- ptrdiff_t diff;
-
- cur += sizeof(uint32_t); /* record size */
-
- if (irep->filename) {
- filename_len = strlen(irep->filename);
- }
- else {
- filename_len = 0;
- }
- mrb_assert_int_fit(size_t, filename_len, uint16_t, UINT16_MAX);
- cur += uint16_to_bin((uint16_t)filename_len, cur); /* filename size */
-
- if (filename_len) {
- memcpy(cur, irep->filename, filename_len);
- cur += filename_len; /* filename */
- }
-
- if (irep->lines) {
- mrb_assert_int_fit(size_t, irep->ilen, uint32_t, UINT32_MAX);
- cur += uint32_to_bin((uint32_t)(irep->ilen), cur); /* niseq */
- for (iseq_no = 0; iseq_no < irep->ilen; iseq_no++) {
- cur += uint16_to_bin(irep->lines[iseq_no], cur); /* opcode */
- }
- }
- else {
- cur += uint32_to_bin(0, cur); /* niseq */
- }
-
- diff = cur - bin;
- mrb_assert_int_fit(ptrdiff_t, diff, uint32_t, UINT32_MAX);
-
- uint32_to_bin((uint32_t)diff, bin); /* record size */
-
- mrb_assert_int_fit(ptrdiff_t, diff, size_t, SIZE_MAX);
- return (size_t)diff;
-}
-
-static size_t
-write_lineno_record(mrb_state *mrb, mrb_irep *irep, uint8_t* bin)
-{
- size_t rlen, size = 0;
- int i;
-
- rlen = write_lineno_record_1(mrb, irep, bin);
- bin += rlen;
- size += rlen;
- for (i=0; i<irep->rlen; i++) {
- rlen = write_lineno_record(mrb, irep, bin);
- bin += rlen;
- size += rlen;
- }
- return size;
-}
-
-static int
-write_section_lineno(mrb_state *mrb, mrb_irep *irep, uint8_t *bin)
-{
- size_t section_size = 0;
- size_t rlen = 0; /* size of irep record */
- uint8_t *cur = bin;
-
- if (mrb == NULL || bin == NULL) {
- return MRB_DUMP_INVALID_ARGUMENT;
- }
-
- cur += sizeof(struct rite_section_lineno_header);
- section_size += sizeof(struct rite_section_lineno_header);
-
- rlen = write_lineno_record(mrb, irep, cur);
- section_size += rlen;
-
- write_section_lineno_header(mrb, section_size, bin);
-
- return MRB_DUMP_OK;
-}
-
static size_t
get_debug_record_size(mrb_state *mrb, mrb_irep *irep)
{
@@ -911,10 +799,6 @@ dump_irep(mrb_state *mrb, mrb_irep *irep, uint8_t flags, uint8_t **bin, size_t *
section_lineno_size += get_debug_record_size(mrb, irep);
}
- else {
- section_lineno_size += sizeof(struct rite_section_lineno_header);
- section_lineno_size += get_lineno_record_size(mrb, irep);
- }
}
if (lv_defined) {
@@ -942,13 +826,10 @@ dump_irep(mrb_state *mrb, mrb_irep *irep, uint8_t flags, uint8_t **bin, size_t *
if (flags & DUMP_DEBUG_INFO) {
if (debug_info_defined) {
result = write_section_debug(mrb, irep, cur, filenames, filenames_len);
- }
- else {
- result = write_section_lineno(mrb, irep, cur);
- }
if (result != MRB_DUMP_OK) {
goto error_exit;
}
+ }
cur += section_lineno_size;
}

MODIFY
src/load.c
src/load.c
@@ -215,12 +215,11 @@ read_section_irep(mrb_state *mrb, const uint8_t *bin, uint8_t flags)
return read_irep_record(mrb, bin, &len, flags);
}
+/* ignore lineno record */
static int
read_lineno_record_1(mrb_state *mrb, const uint8_t *bin, mrb_irep *irep, size_t *len)
{
size_t i, fname_len, niseq;
- char *fname;
- uint16_t *lines;
*len = 0;
bin += sizeof(uint32_t); /* record size */
@@ -228,9 +227,6 @@ read_lineno_record_1(mrb_state *mrb, const uint8_t *bin, mrb_irep *irep, size_t
fname_len = bin_to_uint16(bin);
bin += sizeof(uint16_t);
*len += sizeof(uint16_t);
- fname = (char *)mrb_malloc(mrb, fname_len + 1);
- memcpy(fname, bin, fname_len);
- fname[fname_len] = '\0';
bin += fname_len;
*len += fname_len;
@@ -241,15 +237,11 @@ read_lineno_record_1(mrb_state *mrb, const uint8_t *bin, mrb_irep *irep, size_t
if (SIZE_ERROR_MUL(niseq, sizeof(uint16_t))) {
return MRB_DUMP_GENERAL_FAILURE;
}
- lines = (uint16_t *)mrb_malloc(mrb, niseq * sizeof(uint16_t));
for (i = 0; i < niseq; i++) {
- lines[i] = bin_to_uint16(bin);
bin += sizeof(uint16_t); /* niseq */
*len += sizeof(uint16_t);
}
- irep->filename = fname;
- irep->lines = lines;
return MRB_DUMP_OK;
}

MODIFY
src/state.c
src/state.c
@@ -168,10 +168,6 @@ mrb_irep_free(mrb_state *mrb, mrb_irep *irep)
}
mrb_free(mrb, irep->reps);
mrb_free(mrb, irep->lv);
- if (irep->own_filename) {
- mrb_free(mrb, (void *)irep->filename);
- }
- mrb_free(mrb, irep->lines);
mrb_debug_info_free(mrb, irep->debug_info);
mrb_free(mrb, irep);
}
@@ -273,7 +269,6 @@ mrb_add_irep(mrb_state *mrb)
irep = (mrb_irep *)mrb_malloc(mrb, sizeof(mrb_irep));
*irep = mrb_irep_zero;
irep->refcnt = 1;
- irep->own_filename = FALSE;
return irep;
}

Remove `filename`&`lines` from `mrb_irep` struct.

This patch slightly reduce memory consumption (2% for my test).