Issue #15589 has been updated by mame (Yusuke Endoh).

Assignee set to k0kubun (Takashi Kokubun)

`Numeric#zero?` is not slow, but normal.  Rather, `==` is fast because of specific VM instruction.  I don't think that `Numeric#zero?` deserves such a special handling.  I hope that MJIT will implement method inlining and fix this issue in more general way.  So, assigning to k0kubun.

(Personally I don't see any reason to use `zero?`.   `== 0` is more explicit, shorter, more consistent, and easier to understand.  Anyway.)

Just FYI: at least, the reason why `zero?` was introduced is **not** because it is for frequent patterns.  I have investigated the history of `Numeric#zero?` and `nonzero?` before.  

http://blade.nagaokaut.ac.jp/cgi-bin/scat.rb/ruby/ruby-core/58498

In short, it was introduced for very technical reason, and the motivation example is less significant now.

----------------------------------------
Bug #15589: `Numeric#zero?` is much slower than `== 0`
https://bugs.ruby-lang.org/issues/15589#change-76683

* Author: sawa (Tsuyoshi Sawada)
* Status: Open
* Priority: Normal
* Assignee: k0kubun (Takashi Kokubun)
* Target version: 
* ruby -v: 2.6.1
* Backport: 2.4: UNKNOWN, 2.5: UNKNOWN, 2.6: UNKNOWN
----------------------------------------
My understanding is that the predicate method `Numeric#zero?` is not only a shorthand for `== 0`, but is also optimized for frequent patterns. If `zero?` is not faster than `== 0`, then it loses its reason for existence.

However, According to benchmarks on my environment, `number.zero?` is around 1.23 times to 1.64 times slower than `number == 0` when `number` is an `Integer`, `Rational`, or `Complex`. It is faster only when `number` is a `Float`.

And with `number.nonzero?`, it is even worse. It is about 1.88 times to 4.35 times slower than `number != 0`.

I think there is something wrong with this, and it should be possible to optimize these methods, which has somehow been missed.



-- 
https://bugs.ruby-lang.org/

Unsubscribe: <mailto:ruby-core-request / ruby-lang.org?subject=unsubscribe>
<http://lists.ruby-lang.org/cgi-bin/mailman/options/ruby-core>