summaryrefslogtreecommitdiff
path: root/test/ruby/test_array.rb
diff options
context:
space:
mode:
authorRandy Stauner <randy.stauner@shopify.com>2024-11-26 12:31:08 -0700
committerGitHub <noreply@github.com>2024-11-26 14:31:08 -0500
commit1dd40ec18a55ff46f52d0ba44ff5d7923f57c08f (patch)
treeeac4304c90dfa9df36a657af57eccd6bf3e8543c /test/ruby/test_array.rb
parentc1dcd1d4965100292e8f649042c74e10d58e6c0f (diff)
Optimize instructions when creating an array just to call `include?` (#12123)
* Add opt_duparray_send insn to skip the allocation on `#include?` If the method isn't going to modify the array we don't need to copy it. This avoids the allocation / array copy for things like `[:a, :b].include?(x)`. This adds a BOP for include? and tracks redefinition for it on Array. Co-authored-by: Andrew Novoselac <andrew.novoselac@shopify.com> * YJIT: Implement opt_duparray_send include_p Co-authored-by: Andrew Novoselac <andrew.novoselac@shopify.com> * Update opt_newarray_send to support simple forms of include?(arg) Similar to opt_duparray_send but for non-static arrays. * YJIT: Implement opt_newarray_send include_p --------- Co-authored-by: Andrew Novoselac <andrew.novoselac@shopify.com>
Notes
Notes: Merged-By: maximecb <maximecb@ruby-lang.org>
Diffstat (limited to 'test/ruby/test_array.rb')
-rw-r--r--test/ruby/test_array.rb27
1 files changed, 27 insertions, 0 deletions
diff --git a/test/ruby/test_array.rb b/test/ruby/test_array.rb
index 66251b9fb0..797ae95e97 100644
--- a/test/ruby/test_array.rb
+++ b/test/ruby/test_array.rb
@@ -1114,6 +1114,33 @@ class TestArray < Test::Unit::TestCase
assert_not_include(a, [1,2])
end
+ def test_monkey_patch_include?
+ assert_separately([], "#{<<~"begin;"}\n#{<<~'end;'}", timeout: 30)
+ begin;
+ $-w = false
+ class Array
+ alias :old_include? :include?
+ def include? x
+ return true if x == :always
+ old_include?(x)
+ end
+ end
+ def test
+ a, c, always = :a, :c, :always
+ [
+ [:a, :b].include?(a),
+ [:a, :b].include?(c),
+ [:a, :b].include?(always),
+ ]
+ end
+ v = test
+ class Array
+ alias :include? :old_include?
+ end
+ assert_equal [true, false, true], v
+ end;
+ end
+
def test_intersect?
a = @cls[ 1, 2, 3]
assert_send([a, :intersect?, [3]])