[前][次][番号順一覧][スレッド一覧]

ruby-changes:68522

From: Aaron <ko1@a...>
Date: Mon, 18 Oct 2021 17:41:50 +0900 (JST)
Subject: [ruby-changes:68522] 57bf354c9a (master): Eliminate some redundant checks on `num` in `newhash`

https://git.ruby-lang.org/ruby.git/commit/?id=57bf354c9a

From 57bf354c9a878bb67c294408400fd029f9b5a353 Mon Sep 17 00:00:00 2001
From: Aaron Patterson <tenderlove@r...>
Date: Sat, 16 Oct 2021 11:20:30 -0700
Subject: 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 https://github.com/ruby/ruby/blob/trunk/insns.def#L526
 {
     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)) */
-- 
cgit v1.2.1


--
ML: ruby-changes@q...
Info: http://www.atdot.net/~ko1/quickml/

[前][次][番号順一覧][スレッド一覧]