From 57bf354c9a878bb67c294408400fd029f9b5a353 Mon Sep 17 00:00:00 2001 From: Aaron Patterson Date: Sat, 16 Oct 2021 11:20:30 -0700 Subject: [PATCH] Eliminate some redundant checks on `num` in `newhash` The `newhash` instruction was checking if `num` is greater than 0, but so is [`rb_hash_new_with_size`](https://github.com/ruby/ruby/blob/82e2443d8b1e3edd2607c78dddf5aac79a13492d/hash.c#L1564) as well as [`rb_hash_bulk_insert`](https://github.com/ruby/ruby/blob/82e2443d8b1e3edd2607c78dddf5aac79a13492d/hash.c#L4764). If we know the size is 0 in the instruction, we can just directly call `rb_hash_new` and only check the size once. Unfortunately, when num is greater than 0, it's still checked 3 times. --- insns.def | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/insns.def b/insns.def index 43690be52e..7dfeed202b 100644 --- a/insns.def +++ b/insns.def @@ -526,11 +526,13 @@ newhash { RUBY_DTRACE_CREATE_HOOK(HASH, num); - val = rb_hash_new_with_size(num / 2); - if (num) { + val = rb_hash_new_with_size(num / 2); rb_hash_bulk_insert(num, STACK_ADDR_FROM_TOP(num), val); } + else { + val = rb_hash_new(); + } } /* put new Range object.(Range.new(low, high, flag)) */