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

ruby-changes:58843

From: Kazuki <ko1@a...>
Date: Wed, 20 Nov 2019 00:07:27 +0900 (JST)
Subject: [ruby-changes:58843] 2439948bcc (master): Avoid needless object allocation

https://git.ruby-lang.org/ruby.git/commit/?id=2439948bcc

From 2439948bcc0ec9daf91cf79301195e59bad49aff Mon Sep 17 00:00:00 2001
From: Kazuki Tsujimoto <kazuki@c...>
Date: Tue, 19 Nov 2019 08:53:01 -0600
Subject: Avoid needless object allocation


diff --git a/struct.c b/struct.c
index bf1ff6f..d1aa7c2 100644
--- a/struct.c
+++ b/struct.c
@@ -957,12 +957,15 @@ rb_struct_deconstruct_keys(VALUE s, VALUE keys) https://github.com/ruby/ruby/blob/trunk/struct.c#L957
                  rb_obj_class(keys));
 
     }
+    if (RSTRUCT_LEN(s) < RARRAY_LEN(keys)) {
+        return rb_hash_new_with_size(0);
+    }
     h = rb_hash_new_with_size(RARRAY_LEN(keys));
     for (i=0; i<RARRAY_LEN(keys); i++) {
         VALUE key = RARRAY_AREF(keys, i);
         int i = rb_struct_pos(s, &key);
         if (i < 0) {
-            return rb_hash_new_with_size(0);
+            return h;
         }
         rb_hash_aset(h, key, RSTRUCT_GET(s, i));
     }
diff --git a/test/ruby/test_pattern_matching.rb b/test/ruby/test_pattern_matching.rb
index 69524cb..889f8dd 100644
--- a/test/ruby/test_pattern_matching.rb
+++ b/test/ruby/test_pattern_matching.rb
@@ -1250,6 +1250,8 @@ END https://github.com/ruby/ruby/blob/trunk/test/ruby/test_pattern_matching.rb#L1250
       case s[a: 0, b: 1]
       in a:, c:
         flunk
+      in a:, b:, c:
+        flunk
       in b:
         b == 1
       end
diff --git a/test/ruby/test_struct.rb b/test/ruby/test_struct.rb
index 2c725f1..2f5ef80 100644
--- a/test/ruby/test_struct.rb
+++ b/test/ruby/test_struct.rb
@@ -437,7 +437,7 @@ module TestStruct https://github.com/ruby/ruby/blob/trunk/test/ruby/test_struct.rb#L437
     assert_equal({a: 1, b: 2}, o.deconstruct_keys(nil))
     assert_equal({a: 1, b: 2}, o.deconstruct_keys([:b, :a]))
     assert_equal({a: 1}, o.deconstruct_keys([:a]))
-    assert_equal({}, o.deconstruct_keys([:a, :c]))
+    assert_not_send([o.deconstruct_keys([:a, :c]), :key?, :c])
     assert_raise(TypeError) {
       o.deconstruct_keys(0)
     }
-- 
cgit v0.10.2


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

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