diff options
| author | Yukihiro "Matz" Matsumoto <[email protected]> | 2019-08-16 16:36:13 +0900 |
|---|---|---|
| committer | Yukihiro "Matz" Matsumoto <[email protected]> | 2019-08-16 16:42:40 +0900 |
| commit | 06c7ff06662440a900c06cda5646b7090ab9d8c9 (patch) | |
| tree | 8ebaeb4292095a8a5df9def16fd021e3e07e45c2 | |
| parent | fa8515bc2c22ad1b1c7688cf0201022702f6a22f (diff) | |
| download | mruby-06c7ff06662440a900c06cda5646b7090ab9d8c9.tar.gz mruby-06c7ff06662440a900c06cda5646b7090ab9d8c9.zip | |
Avoid creating unnecessary empty arrays on splat.
But this changes requires `OP_ARYCAT` and `OP_ARYPUSH` to accept `nil`
as their first operand. Alternative VMs (e.g. `mruby/c`) that understand
mruby bytecode need to be updated.
| -rw-r--r-- | mrbgems/mruby-compiler/core/codegen.c | 7 | ||||
| -rw-r--r-- | src/vm.c | 7 |
2 files changed, 12 insertions, 2 deletions
diff --git a/mrbgems/mruby-compiler/core/codegen.c b/mrbgems/mruby-compiler/core/codegen.c index 5cade970c..d4031f6fd 100644 --- a/mrbgems/mruby-compiler/core/codegen.c +++ b/mrbgems/mruby-compiler/core/codegen.c @@ -956,7 +956,12 @@ gen_values(codegen_scope *s, node *t, int val, int extra) } else { pop_n(n); - genop_2(s, OP_ARRAY, cursp(), n); + if (n == 0 && is_splat) { + genop_1(s, OP_LOADNIL, cursp()); + } + else { + genop_2(s, OP_ARRAY, cursp(), n); + } push(); codegen(s, t->car, VAL); pop(); pop(); @@ -2441,7 +2441,12 @@ RETRY_TRY_BLOCK: CASE(OP_ARYCAT, B) { mrb_value splat = mrb_ary_splat(mrb, regs[a+1]); - mrb_ary_concat(mrb, regs[a], splat); + if (mrb_nil_p(regs[a])) { + regs[a] = splat; + } + else { + mrb_ary_concat(mrb, regs[a], splat); + } mrb_gc_arena_restore(mrb, ai); NEXT; } |
