diff options
author | Daniel Colson <[email protected]> | 2022-11-22 21:16:11 -0500 |
---|---|---|
committer | John Hawthorn <[email protected]> | 2022-12-06 12:37:23 -0800 |
commit | e69b91fae4602b69c5ef45fcf82932adde8b31d8 (patch) | |
tree | 7b3466a4199eadf6bb00773145895697b1455748 /internal/compar.h | |
parent | c43951e60eed0b01f464cd25441b81751d2d5087 (diff) |
Introduce BOP_CMP for optimized comparison
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=<master@5958c305> ITEM=array_min
$ make benchmark COMPARE_RUBY=<master@5958c305> ITEM=array_max
```
The benchmarks added in this commit also look generally improved.
Co-authored-by: John Hawthorn <[email protected]>
Diffstat (limited to 'internal/compar.h')
-rw-r--r-- | internal/compar.h | 32 |
1 files changed, 6 insertions, 26 deletions
diff --git a/internal/compar.h b/internal/compar.h index 5e336adafa..9115e4bd63 100644 --- a/internal/compar.h +++ b/internal/compar.h @@ -8,38 +8,18 @@ * file COPYING are met. Consult the file for details. * @brief Internal header for Comparable. */ -#include "internal/vm.h" /* for rb_method_basic_definition_p */ +#include "internal/basic_operators.h" #define STRING_P(s) (RB_TYPE_P((s), T_STRING) && CLASS_OF(s) == rb_cString) -enum { - cmp_opt_Integer, - cmp_opt_String, - cmp_opt_Float, - cmp_optimizable_count -}; +#define CMP_OPTIMIZABLE(type) BASIC_OP_UNREDEFINED_P(BOP_CMP, type##_REDEFINED_OP_FLAG) -struct cmp_opt_data { - unsigned int opt_methods; - unsigned int opt_inited; -}; - -#define NEW_CMP_OPT_MEMO(type, value) \ - NEW_PARTIAL_MEMO_FOR(type, value, cmp_opt) -#define CMP_OPTIMIZABLE_BIT(type) (1U << TOKEN_PASTE(cmp_opt_,type)) -#define CMP_OPTIMIZABLE(data, type) \ - (((data).opt_inited & CMP_OPTIMIZABLE_BIT(type)) ? \ - ((data).opt_methods & CMP_OPTIMIZABLE_BIT(type)) : \ - (((data).opt_inited |= CMP_OPTIMIZABLE_BIT(type)), \ - rb_method_basic_definition_p(TOKEN_PASTE(rb_c,type), id_cmp) && \ - ((data).opt_methods |= CMP_OPTIMIZABLE_BIT(type)))) - -#define OPTIMIZED_CMP(a, b, data) \ - ((FIXNUM_P(a) && FIXNUM_P(b) && CMP_OPTIMIZABLE(data, Integer)) ? \ +#define OPTIMIZED_CMP(a, b) \ + ((FIXNUM_P(a) && FIXNUM_P(b) && CMP_OPTIMIZABLE(INTEGER)) ? \ (((long)a > (long)b) ? 1 : ((long)a < (long)b) ? -1 : 0) : \ - (STRING_P(a) && STRING_P(b) && CMP_OPTIMIZABLE(data, String)) ? \ + (STRING_P(a) && STRING_P(b) && CMP_OPTIMIZABLE(STRING)) ? \ rb_str_cmp(a, b) : \ - (RB_FLOAT_TYPE_P(a) && RB_FLOAT_TYPE_P(b) && CMP_OPTIMIZABLE(data, Float)) ? \ + (RB_FLOAT_TYPE_P(a) && RB_FLOAT_TYPE_P(b) && CMP_OPTIMIZABLE(FLOAT)) ? \ rb_float_cmp(a, b) : \ rb_cmpint(rb_funcallv(a, id_cmp, 1, &b), a, b)) |