From e69b91fae4602b69c5ef45fcf82932adde8b31d8 Mon Sep 17 00:00:00 2001 From: Daniel Colson Date: Tue, 22 Nov 2022 21:16:11 -0500 Subject: Introduce BOP_CMP for optimized comparison MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Prior to this commit the `OPTIMIZED_CMP` macro relied on a method lookup to determine whether `<=>` was overridden. The result of the lookup was cached, but only for the duration of the specific method that initialized the cmp_opt_data cache structure. With this method lookup, `[x,y].max` is slower than doing `x > y ? x : y` even though there's an optimized instruction for "new array max". (John noticed somebody a proposed micro-optimization based on this fact in https://github.com/mastodon/mastodon/pull/19903.) ```rb a, b = 1, 2 Benchmark.ips do |bm| bm.report('conditional') { a > b ? a : b } bm.report('method') { [a, b].max } bm.compare! end ``` Before: ``` Comparison: conditional: 22603733.2 i/s method: 19820412.7 i/s - 1.14x (± 0.00) slower ``` This commit replaces the method lookup with a new CMP basic op, which gives the examples above equivalent performance. After: ``` Comparison: method: 24022466.5 i/s conditional: 23851094.2 i/s - same-ish: difference falls within error ``` Relevant benchmarks show an improvement to Array#max and Array#min when not using the optimized newarray_max instruction as well. They are noticeably faster for small arrays with the relevant types, and the same or maybe a touch faster on larger arrays. ``` $ make benchmark COMPARE_RUBY= ITEM=array_min $ make benchmark COMPARE_RUBY= ITEM=array_max ``` The benchmarks added in this commit also look generally improved. Co-authored-by: John Hawthorn --- internal/basic_operators.h | 1 + 1 file changed, 1 insertion(+) (limited to 'internal/basic_operators.h') diff --git a/internal/basic_operators.h b/internal/basic_operators.h index b978421610..8f4458a68b 100644 --- a/internal/basic_operators.h +++ b/internal/basic_operators.h @@ -34,6 +34,7 @@ enum ruby_basic_operators { BOP_CALL, BOP_AND, BOP_OR, + BOP_CMP, BOP_LAST_ }; -- cgit v1.2.1