dumping ground for random patches and texts
 help / color / mirror / Atom feed
* [PATCH] micro-optimize case dispatch even harder
@ 2015-12-08  3:28 Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2015-12-08  3:28 UTC (permalink / raw)
  To: spew

While working on r52931, I noticed increasing the size of the
opt_case_dispatch instruction was not worth it and could trigger
tiny regressions on code that does NOT use optimized case dispatch.

By using a bare hash table, we avoid the overhead of rb_hash_*
functions as well as the cost of translating FIX2INT for jump
labels.

This also reduces GC overhead, as the iseq mark array no longer
carries redundant objects nor the Hash object.

2015-12-08 03:26:19 +0000
target 0: a (ruby 2.3.0dev (2015-12-08 trunk 52932) [x86_64-linux]) at "/home/ew/rrrr/b/ruby"
target 1: b (ruby 2.3.0dev (2015-12-08 master 52932) [x86_64-linux]
last_commit=micro-optimize case dispatch even harder) at "/home/ew/ruby/b/ruby"

-----------------------------------------------------------
loop_whileloop2

i = 0
while i< 6_000_000 # benchmark loop 2
  i += 1
end

a	0.10256672604009509
a	0.10279557690955698
a	0.10286601004190743
a	0.10278227413073182
a	0.10278794192709029
b	0.10288732498884201
b	0.10327051300555468
b	0.1026718900538981
b	0.10263488302007318
b	0.10256404406391084

-----------------------------------------------------------
vm2_case

i = 0
while i<6_000_000 # while loop 2
  case :foo
  when :bar
    raise
  when :baz
    raise
  when :boo
    raise
  when :foo
    i += 1
  end
end

a	0.18099936190992594
a	0.1836838680319488
a	0.1825075231026858
a	0.18087006197310984
a	0.18375545786693692
b	0.16652655508369207
b	0.16673082998022437
b	0.16754083498381078
b	0.16704767406918108
b	0.16648077592253685

-----------------------------------------------------------
vm2_case_lit

i = 0
@ret = [ "foo", true, false, :sym, 6, nil, 0.1, 0xffffffffffffffff ]
def foo(i)
  @ret[i % @ret.size]
end

while i<6_000_000 # while loop 2
  case foo(i)
  when "foo" then :foo
  when true then true
  when false then false
  when :sym then :sym
  when 6 then :fix
  when nil then nil
  when 0.1 then :float
  when 0xffffffffffffffff then :big
  end
  i += 1
end

a	0.670805990928784
a	0.6559841448906809
a	0.6560367799829692
a	0.655022048857063
a	0.671947613125667
b	0.659776204964146
b	0.6549702121410519
b	0.6788892599288374
b	0.6793588208965957
b	0.6878500080201775

-----------------------------------------------------------
vm2_case_small

i = 0
while i<6_000_000 # while loop 2
  case :foo
  when :foo
    i += 1
  else
    raise
  end
end

a	0.1667630800511688
a	0.16669297800399363
a	0.1665362489875406
a	0.16644068900495768
a	0.16646360605955124
b	0.15790433110669255
b	0.15774213010445237
b	0.15753646893426776
b	0.15781028987839818
b	0.15771835204213858

Elapsed time: 11.069307098 (sec)
-----------------------------------------------------------
benchmark results:
minimum results in each 5 measurements.
Execution time (sec)
name	a	b
loop_whileloop2	0.103	0.103
vm2_case*	0.078	0.064
vm2_case_lit*	0.552	0.552
vm2_case_small*	0.064	0.055

Speedup ratio: compare with the result of `a' (greater is better)
name	b
loop_whileloop2	1.000
vm2_case*	1.225
vm2_case_lit*	1.000
vm2_case_small*	1.162
---
 benchmark/bm_vm2_case_small.rb |  9 ++++++++
 compile.c                      | 50 ++++++++++++++++++++----------------------
 insns.def                      |  6 ++---
 iseq.c                         |  6 ++---
 vm_core.h                      |  2 +-
 5 files changed, 40 insertions(+), 33 deletions(-)
 create mode 100644 benchmark/bm_vm2_case_small.rb

diff --git a/benchmark/bm_vm2_case_small.rb b/benchmark/bm_vm2_case_small.rb
new file mode 100644
index 0000000..b009a3e
--- /dev/null
+++ b/benchmark/bm_vm2_case_small.rb
@@ -0,0 +1,9 @@
+i = 0
+while i<6_000_000 # while loop 2
+  case :foo
+  when :foo
+    i += 1
+  else
+    raise
+  end
+end
diff --git a/compile.c b/compile.c
index abb5306..462354e 100644
--- a/compile.c
+++ b/compile.c
@@ -1492,17 +1492,17 @@ static const struct st_hash_type cdhash_type = {
 };
 
 struct cdhash_set_label_struct {
-    VALUE hash;
+    st_table *cdh;
     int pos;
     int len;
 };
 
 static int
-cdhash_set_label_i(VALUE key, VALUE val, void *ptr)
+cdhash_set_label_i(VALUE key, long val, void *ptr)
 {
-    struct cdhash_set_label_struct *data = (struct cdhash_set_label_struct *)ptr;
+    struct cdhash_set_label_struct *d = (struct cdhash_set_label_struct *)ptr;
     LABEL *lobj = (LABEL *)(val & ~1);
-    rb_hash_aset(data->hash, key, INT2FIX(lobj->position - (data->pos+data->len)));
+    st_insert(d->cdh, key, lobj->position - (d->pos + d->len));
     return ST_CONTINUE;
 }
 
@@ -1630,15 +1630,14 @@ iseq_set_sequence(rb_iseq_t *iseq, LINK_ANCHOR *anchor)
 			}
 		      case TS_CDHASH:
 			{
-			    VALUE map = operands[j];
-			    struct cdhash_set_label_struct data;
-                            data.hash = map;
-                            data.pos = code_index;
-                            data.len = len;
-			    rb_hash_foreach(map, cdhash_set_label_i, (VALUE)&data);
-
-			    freeze_hide_obj(map);
-			    generated_iseq[code_index + 1 + j] = map;
+			    st_table *cdh = (st_table *)operands[j];
+			    struct cdhash_set_label_struct d;
+			    d.cdh = cdh;
+			    d.pos = code_index;
+			    d.len = len;
+
+			    st_foreach(cdh, cdhash_set_label_i, (st_data_t)&d);
+			    generated_iseq[code_index + 1 + j] = (VALUE)cdh;
 			    break;
 			}
 		      case TS_LINDEX:
@@ -2932,7 +2931,8 @@ case_when_optimizable_literal(NODE * node)
 }
 
 static int
-when_vals(rb_iseq_t *iseq, LINK_ANCHOR *cond_seq, NODE *vals, LABEL *l1, int only_special_literals, VALUE literals)
+when_vals(rb_iseq_t *iseq, LINK_ANCHOR *cond_seq, NODE *vals, LABEL *l1,
+	  int only_special_literals, st_table *literals)
 {
     while (vals) {
 	NODE* val = vals->nd_head;
@@ -2942,11 +2942,11 @@ when_vals(rb_iseq_t *iseq, LINK_ANCHOR *cond_seq, NODE *vals, LABEL *l1, int onl
 	    only_special_literals = 0;
 	}
 	else {
-	    if (rb_hash_lookup(literals, lit) != Qnil) {
+	    if (st_lookup(literals, lit, 0)) {
 		rb_compile_warning(ruby_sourcefile, nd_line(val), "duplicated when clause is ignored");
 	    }
 	    else {
-		rb_hash_aset(literals, lit, (VALUE)(l1) | 1);
+		st_add_direct(literals, lit, (VALUE)(l1) | 1);
 	    }
 	}
 
@@ -3703,14 +3703,12 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
 	DECL_ANCHOR(body_seq);
 	DECL_ANCHOR(cond_seq);
 	int only_special_literals = 1;
-	VALUE literals = rb_hash_new();
+	st_table *literals = st_init_table(&cdhash_type);
 
 	INIT_ANCHOR(head);
 	INIT_ANCHOR(body_seq);
 	INIT_ANCHOR(cond_seq);
 
-	rb_hash_tbl_raw(literals)->type = &cdhash_type;
-
 	if (node->nd_head == 0) {
 	    COMPILE_(ret, "when", node->nd_body, poped);
 	    break;
@@ -3789,12 +3787,13 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
 	}
 
 	if (only_special_literals) {
-	    iseq_add_mark_object(iseq, literals);
-
 	    ADD_INSN(ret, nd_line(tempnode), dup);
 	    ADD_INSN2(ret, nd_line(tempnode), opt_case_dispatch, literals, elselabel);
 	    LABEL_REF(elselabel);
 	}
+	else {
+	    st_free_table(literals);
+	}
 
 	ADD_SEQ(ret, cond_seq);
 	ADD_SEQ(ret, body_seq);
@@ -6403,9 +6402,8 @@ iseq_build_from_ary_body(rb_iseq_t *iseq, LINK_ANCHOR *anchor,
 		      case TS_CDHASH:
 			{
 			    int i;
-			    VALUE map = rb_hash_new();
+			    st_table *literals = st_init_table(&cdhash_type);
 
-			    rb_hash_tbl_raw(map)->type = &cdhash_type;
 			    op = rb_convert_type(op, T_ARRAY, "Array", "to_ary");
 			    op = rb_ary_dup(op);
 			    for (i=0; i<RARRAY_LEN(op); i+=2) {
@@ -6413,11 +6411,11 @@ iseq_build_from_ary_body(rb_iseq_t *iseq, LINK_ANCHOR *anchor,
 				VALUE sym = RARRAY_AREF(op, i+1);
 				LABEL *label =
 				  register_label(iseq, labels_table, sym);
-				rb_hash_aset(map, key, (VALUE)label | 1);
+
+				st_add_direct(literals, key, (VALUE)label | 1);
 			    }
 			    RB_GC_GUARD(op);
-			    argv[j] = map;
-			    rb_iseq_add_mark_object(iseq, map);
+			    argv[j] = (VALUE)literals;
 			}
 			break;
 		      case TS_FUNCPTR:
diff --git a/insns.def b/insns.def
index 3c4d980..4de3a6d 100644
--- a/insns.def
+++ b/insns.def
@@ -1253,7 +1253,7 @@ once
  */
 DEFINE_INSN
 opt_case_dispatch
-(CDHASH hash, OFFSET else_offset)
+(CDHASH cdh, OFFSET else_offset)
 (..., VALUE key)
 () // inc += -1;
 {
@@ -1281,8 +1281,8 @@ opt_case_dispatch
 				   FALSE_REDEFINED_OP_FLAG  |
 				   STRING_REDEFINED_OP_FLAG)) {
 	    st_data_t val;
-	    if (st_lookup(RHASH_TBL_RAW(hash), key, &val)) {
-		JUMP(FIX2INT((VALUE)val));
+	    if (st_lookup(cdh, key, &val)) {
+		JUMP(val);
 	    }
 	    else {
 		JUMP(else_offset);
diff --git a/iseq.c b/iseq.c
index 17bfee4..de08beb 100644
--- a/iseq.c
+++ b/iseq.c
@@ -1754,7 +1754,7 @@ static int
 cdhash_each(VALUE key, VALUE value, VALUE ary)
 {
     rb_ary_push(ary, obj_resurrect(key));
-    rb_ary_push(ary, value);
+    rb_ary_push(ary, INT2FIX(value));
     return ST_CONTINUE;
 }
 
@@ -1961,11 +1961,11 @@ iseq_data_to_ary(const rb_iseq_t *iseq)
 		break;
 	      case TS_CDHASH:
 		{
-		    VALUE hash = *seq;
+		    st_table *cdh = (st_table *)*seq;
 		    VALUE val = rb_ary_new();
 		    int i;
 
-		    rb_hash_foreach(hash, cdhash_each, val);
+		    st_foreach(cdh, cdhash_each, val);
 
 		    for (i=0; i<RARRAY_LEN(val); i+=2) {
 			VALUE pos = FIX2INT(rb_ary_entry(val, i+1));
diff --git a/vm_core.h b/vm_core.h
index aecbe61..c700382 100644
--- a/vm_core.h
+++ b/vm_core.h
@@ -917,7 +917,7 @@ typedef struct rb_call_cache *CALL_CACHE;
 
 void rb_vm_change_state(void);
 
-typedef VALUE CDHASH;
+typedef st_table * CDHASH;
 
 #ifndef FUNC_FASTCALL
 #define FUNC_FASTCALL(x) x
-- 
EW


^ permalink raw reply related	[flat|nested] 2+ messages in thread

* [PATCH] micro-optimize case dispatch even harder
@ 2015-12-08  4:59 Eric Wong
  0 siblings, 0 replies; 2+ messages in thread
From: Eric Wong @ 2015-12-08  4:59 UTC (permalink / raw)
  To: spew

I noticed these optimizations while working on r52931.

By using a bare hash table, we avoid the overhead of rb_hash_*
functions as well as the cost of translating FIX2INT for jump
labels.

This also reduces GC overhead, as the iseq mark array no longer
carries redundant objects nor the Hash object.

* vm_core.h (CDHASH): change to st_table *
* benchmark/bm_vm2_case_small.rb: new benchmark
* compile.c (struct cdhash_set_label_struct): change to st_table *
  (cdhash_set_label_i): adjust to store int in cdhash
  (iseq_set_sequence): adjust for type change
  (when_vals): ditto
  (iseq_compile_each): ditto
  (iseq_build_from_ary_body): ditto
  (cdhash_from_ary): extract from iseq_build_from_ary_body
* insns.def (opt_case_dispatch): ditto
* iseq.c (cdhash_each): ditto
  (iseq_data_to_ary): ditto
* test/ruby/test_iseq.rb (test_memleak): new test
  [ruby-core:71931] [Feature #11786]

2015-12-08 03:26:19 +0000
target 0: a (ruby 2.3.0dev (2015-12-08 trunk 52932) [x86_64-linux]) at "/home/ew/rrrr/b/ruby"
target 1: b (ruby 2.3.0dev (2015-12-08 master 52932) [x86_64-linux]
last_commit=micro-optimize case dispatch even harder) at "/home/ew/ruby/b/ruby"

-----------------------------------------------------------
loop_whileloop2

i = 0
while i< 6_000_000 # benchmark loop 2
  i += 1
end

a	0.10256672604009509
a	0.10279557690955698
a	0.10286601004190743
a	0.10278227413073182
a	0.10278794192709029
b	0.10288732498884201
b	0.10327051300555468
b	0.1026718900538981
b	0.10263488302007318
b	0.10256404406391084

-----------------------------------------------------------
vm2_case

i = 0
while i<6_000_000 # while loop 2
  case :foo
  when :bar
    raise
  when :baz
    raise
  when :boo
    raise
  when :foo
    i += 1
  end
end

a	0.18099936190992594
a	0.1836838680319488
a	0.1825075231026858
a	0.18087006197310984
a	0.18375545786693692
b	0.16652655508369207
b	0.16673082998022437
b	0.16754083498381078
b	0.16704767406918108
b	0.16648077592253685

-----------------------------------------------------------
vm2_case_lit

i = 0
@ret = [ "foo", true, false, :sym, 6, nil, 0.1, 0xffffffffffffffff ]
def foo(i)
  @ret[i % @ret.size]
end

while i<6_000_000 # while loop 2
  case foo(i)
  when "foo" then :foo
  when true then true
  when false then false
  when :sym then :sym
  when 6 then :fix
  when nil then nil
  when 0.1 then :float
  when 0xffffffffffffffff then :big
  end
  i += 1
end

a	0.670805990928784
a	0.6559841448906809
a	0.6560367799829692
a	0.655022048857063
a	0.671947613125667
b	0.659776204964146
b	0.6549702121410519
b	0.6788892599288374
b	0.6793588208965957
b	0.6878500080201775

-----------------------------------------------------------
vm2_case_small

i = 0
while i<6_000_000 # while loop 2
  case :foo
  when :foo
    i += 1
  else
    raise
  end
end

a	0.1667630800511688
a	0.16669297800399363
a	0.1665362489875406
a	0.16644068900495768
a	0.16646360605955124
b	0.15790433110669255
b	0.15774213010445237
b	0.15753646893426776
b	0.15781028987839818
b	0.15771835204213858

Elapsed time: 11.069307098 (sec)
-----------------------------------------------------------
benchmark results:
minimum results in each 5 measurements.
Execution time (sec)
name	a	b
loop_whileloop2	0.103	0.103
vm2_case*	0.078	0.064
vm2_case_lit*	0.552	0.552
vm2_case_small*	0.064	0.055

Speedup ratio: compare with the result of `a' (greater is better)
name	b
loop_whileloop2	1.000
vm2_case*	1.225
vm2_case_lit*	1.000
vm2_case_small*	1.162
---
 benchmark/bm_vm2_case_small.rb |  9 +++++
 compile.c                      | 79 ++++++++++++++++++++++--------------------
 insns.def                      |  6 ++--
 iseq.c                         |  6 ++--
 test/ruby/test_iseq.rb         | 22 ++++++++++++
 vm_core.h                      |  2 +-
 6 files changed, 79 insertions(+), 45 deletions(-)
 create mode 100644 benchmark/bm_vm2_case_small.rb

diff --git a/benchmark/bm_vm2_case_small.rb b/benchmark/bm_vm2_case_small.rb
new file mode 100644
index 0000000..b009a3e
--- /dev/null
+++ b/benchmark/bm_vm2_case_small.rb
@@ -0,0 +1,9 @@
+i = 0
+while i<6_000_000 # while loop 2
+  case :foo
+  when :foo
+    i += 1
+  else
+    raise
+  end
+end
diff --git a/compile.c b/compile.c
index bb0a07c..896e70c 100644
--- a/compile.c
+++ b/compile.c
@@ -1492,17 +1492,17 @@ static const struct st_hash_type cdhash_type = {
 };
 
 struct cdhash_set_label_struct {
-    VALUE hash;
+    st_table *cdh;
     int pos;
     int len;
 };
 
 static int
-cdhash_set_label_i(VALUE key, VALUE val, void *ptr)
+cdhash_set_label_i(VALUE key, long val, void *ptr)
 {
-    struct cdhash_set_label_struct *data = (struct cdhash_set_label_struct *)ptr;
+    struct cdhash_set_label_struct *d = (struct cdhash_set_label_struct *)ptr;
     LABEL *lobj = (LABEL *)(val & ~1);
-    rb_hash_aset(data->hash, key, INT2FIX(lobj->position - (data->pos+data->len)));
+    st_insert(d->cdh, key, lobj->position - (d->pos + d->len));
     return ST_CONTINUE;
 }
 
@@ -1630,15 +1630,14 @@ iseq_set_sequence(rb_iseq_t *iseq, LINK_ANCHOR *anchor)
 			}
 		      case TS_CDHASH:
 			{
-			    VALUE map = operands[j];
-			    struct cdhash_set_label_struct data;
-                            data.hash = map;
-                            data.pos = code_index;
-                            data.len = len;
-			    rb_hash_foreach(map, cdhash_set_label_i, (VALUE)&data);
-
-			    freeze_hide_obj(map);
-			    generated_iseq[code_index + 1 + j] = map;
+			    st_table *cdh = (st_table *)operands[j];
+			    struct cdhash_set_label_struct d;
+			    d.cdh = cdh;
+			    d.pos = code_index;
+			    d.len = len;
+
+			    st_foreach(cdh, cdhash_set_label_i, (st_data_t)&d);
+			    generated_iseq[code_index + 1 + j] = (VALUE)cdh;
 			    break;
 			}
 		      case TS_LINDEX:
@@ -2932,7 +2931,8 @@ case_when_optimizable_literal(NODE * node)
 }
 
 static int
-when_vals(rb_iseq_t *iseq, LINK_ANCHOR *cond_seq, NODE *vals, LABEL *l1, int only_special_literals, VALUE literals)
+when_vals(rb_iseq_t *iseq, LINK_ANCHOR *cond_seq, NODE *vals, LABEL *l1,
+	  int only_special_literals, st_table *literals)
 {
     while (vals) {
 	NODE* val = vals->nd_head;
@@ -2942,11 +2942,11 @@ when_vals(rb_iseq_t *iseq, LINK_ANCHOR *cond_seq, NODE *vals, LABEL *l1, int onl
 	    only_special_literals = 0;
 	}
 	else {
-	    if (rb_hash_lookup(literals, lit) != Qnil) {
+	    if (st_lookup(literals, lit, 0)) {
 		rb_compile_warning(ruby_sourcefile, nd_line(val), "duplicated when clause is ignored");
 	    }
 	    else {
-		rb_hash_aset(literals, lit, (VALUE)(l1) | 1);
+		st_add_direct(literals, lit, (VALUE)(l1) | 1);
 	    }
 	}
 
@@ -3703,14 +3703,13 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
 	DECL_ANCHOR(body_seq);
 	DECL_ANCHOR(cond_seq);
 	int only_special_literals = 1;
-	VALUE literals = rb_hash_new();
+	st_table *literals = st_init_table(&cdhash_type);
+	VALUE lit_wrapper = Data_Wrap_Struct(0, 0, st_free_table, literals);
 
 	INIT_ANCHOR(head);
 	INIT_ANCHOR(body_seq);
 	INIT_ANCHOR(cond_seq);
 
-	rb_hash_tbl_raw(literals)->type = &cdhash_type;
-
 	if (node->nd_head == 0) {
 	    COMPILE_(ret, "when", node->nd_body, poped);
 	    break;
@@ -3789,8 +3788,7 @@ iseq_compile_each(rb_iseq_t *iseq, LINK_ANCHOR *ret, NODE * node, int poped)
 	}
 
 	if (only_special_literals) {
-	    iseq_add_mark_object(iseq, literals);
-
+	    iseq_add_mark_object(iseq, lit_wrapper);
 	    ADD_INSN(ret, nd_line(tempnode), dup);
 	    ADD_INSN2(ret, nd_line(tempnode), opt_case_dispatch, literals, elselabel);
 	    LABEL_REF(elselabel);
@@ -6298,6 +6296,27 @@ iseq_build_callinfo_from_hash(rb_iseq_t *iseq, VALUE op)
     return (VALUE)new_callinfo(iseq, mid, orig_argc, flag, kw_arg, (flag & VM_CALL_ARGS_SIMPLE) == 0);
 }
 
+static VALUE
+cdhash_from_ary(rb_iseq_t *iseq, st_table *labels_table, VALUE op)
+{
+    int i;
+    st_table *literals = st_init_table(&cdhash_type);
+    VALUE lit_wrap = Data_Wrap_Struct(0, 0, st_free_table, literals);
+
+    iseq_add_mark_object(iseq, lit_wrap);
+    op = rb_convert_type(op, T_ARRAY, "Array", "to_ary");
+    for (i = 0; i < RARRAY_LEN(op); i += 2) {
+	VALUE key = RARRAY_AREF(op, i);
+	VALUE sym = RARRAY_AREF(op, i + 1);
+	LABEL *label = register_label(iseq, labels_table, sym);
+
+	st_add_direct(literals, key, (VALUE)label | 1);
+    }
+    RB_GC_GUARD(op);
+
+    return (VALUE)literals;
+}
+
 static int
 iseq_build_from_ary_body(rb_iseq_t *iseq, LINK_ANCHOR *anchor,
 			 VALUE body, VALUE labels_wrapper)
@@ -6401,23 +6420,7 @@ iseq_build_from_ary_body(rb_iseq_t *iseq, LINK_ANCHOR *anchor,
 						  "Symbol", "to_sym");
 			break;
 		      case TS_CDHASH:
-			{
-			    int i;
-			    VALUE map = rb_hash_new();
-
-			    rb_hash_tbl_raw(map)->type = &cdhash_type;
-			    op = rb_convert_type(op, T_ARRAY, "Array", "to_ary");
-			    for (i=0; i<RARRAY_LEN(op); i+=2) {
-				VALUE key = RARRAY_AREF(op, i);
-				VALUE sym = RARRAY_AREF(op, i+1);
-				LABEL *label =
-				  register_label(iseq, labels_table, sym);
-				rb_hash_aset(map, key, (VALUE)label | 1);
-			    }
-			    RB_GC_GUARD(op);
-			    argv[j] = map;
-			    rb_iseq_add_mark_object(iseq, map);
-			}
+			argv[j] = cdhash_from_ary(iseq, labels_table, op);
 			break;
 		      case TS_FUNCPTR:
 			{
diff --git a/insns.def b/insns.def
index 3c4d980..4de3a6d 100644
--- a/insns.def
+++ b/insns.def
@@ -1253,7 +1253,7 @@ once
  */
 DEFINE_INSN
 opt_case_dispatch
-(CDHASH hash, OFFSET else_offset)
+(CDHASH cdh, OFFSET else_offset)
 (..., VALUE key)
 () // inc += -1;
 {
@@ -1281,8 +1281,8 @@ opt_case_dispatch
 				   FALSE_REDEFINED_OP_FLAG  |
 				   STRING_REDEFINED_OP_FLAG)) {
 	    st_data_t val;
-	    if (st_lookup(RHASH_TBL_RAW(hash), key, &val)) {
-		JUMP(FIX2INT((VALUE)val));
+	    if (st_lookup(cdh, key, &val)) {
+		JUMP(val);
 	    }
 	    else {
 		JUMP(else_offset);
diff --git a/iseq.c b/iseq.c
index 17bfee4..de08beb 100644
--- a/iseq.c
+++ b/iseq.c
@@ -1754,7 +1754,7 @@ static int
 cdhash_each(VALUE key, VALUE value, VALUE ary)
 {
     rb_ary_push(ary, obj_resurrect(key));
-    rb_ary_push(ary, value);
+    rb_ary_push(ary, INT2FIX(value));
     return ST_CONTINUE;
 }
 
@@ -1961,11 +1961,11 @@ iseq_data_to_ary(const rb_iseq_t *iseq)
 		break;
 	      case TS_CDHASH:
 		{
-		    VALUE hash = *seq;
+		    st_table *cdh = (st_table *)*seq;
 		    VALUE val = rb_ary_new();
 		    int i;
 
-		    rb_hash_foreach(hash, cdhash_each, val);
+		    st_foreach(cdh, cdhash_each, val);
 
 		    for (i=0; i<RARRAY_LEN(val); i+=2) {
 			VALUE pos = FIX2INT(rb_ary_entry(val, i+1));
diff --git a/test/ruby/test_iseq.rb b/test/ruby/test_iseq.rb
index 4f7616a..2855872 100644
--- a/test/ruby/test_iseq.rb
+++ b/test/ruby/test_iseq.rb
@@ -185,4 +185,26 @@ def test_safe_call_chain
     labels = body.select {|op, arg| op == :branchnil}.map {|op, arg| arg}
     assert_equal(1, labels.uniq.size)
   end
+
+  def test_memleak
+    assert_no_memory_leak([], <<-'end;', '35_000.times(&doit)')
+      doit = proc { RubyVM::InstructionSequence.compile('
+        case $foo
+        when "0" then 0
+        when "1" then 1
+        when "2" then 2
+        when "3" then 3
+        when "4" then 4
+        when "5" then 5
+        when "6" then 6
+        when "7" then 7
+        when "8" then 8
+        when "9" then 9
+        else
+          -1
+        end
+      ') }
+      10.times(&doit)
+    end;
+  end
 end
diff --git a/vm_core.h b/vm_core.h
index aecbe61..c700382 100644
--- a/vm_core.h
+++ b/vm_core.h
@@ -917,7 +917,7 @@ typedef struct rb_call_cache *CALL_CACHE;
 
 void rb_vm_change_state(void);
 
-typedef VALUE CDHASH;
+typedef st_table * CDHASH;
 
 #ifndef FUNC_FASTCALL
 #define FUNC_FASTCALL(x) x
-- 
EW


^ permalink raw reply related	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2015-12-08  4:59 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2015-12-08  3:28 [PATCH] micro-optimize case dispatch even harder Eric Wong
  -- strict thread matches above, loose matches on Subject: below --
2015-12-08  4:59 Eric Wong

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).