New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Style/NumericPredicate -> defaults to slower code #3633
Comments
But is the performance difference significant? It looks like they are both very fast. |
The result above has an average from about 62% slower. But also the least result (1.19x slower) means that roughly i can do 5 times a |
I would have to disagree with this. This is a style cop, not a performance cop. Robustness and readability (in this case readability is about the same) should be the guiding force for the defaults, not speculative micro-optimizations based on the current Ruby implementation. Language level optimizations are a nebulous thing, potentially leading to us having different defaults per Ruby version, because of changes to the internals. (We recently removed a performance cop because the performance difference had been nuked somewhere around MRI 2.2.) If someone does millions of comparisons to zero and, after proper, in vivo benchmarks, find that this has become a bottleneck, they can configure the cop or disable it for this block. |
I agree with @Drenmi. |
One comment I still have: @Drenmi sad "Language level optimizations" -> It is slower on ruby 2.0, 2.1, 2.2, 2.3 and 2.4 https://travis-ci.org/dennisvandehoef/rubocop-speed/builds/167869584 |
Summary: This now lints for cases beyond just a comparison to 0, and encourage the use of .zero?. Even though we can configure this cop to lint and enforce a particular style (see [rubocop docs](https://rubocop.readthedocs.io/en/latest/cops_style/#stylenumericpredicate) for detail), the additional use cases below now mean that this cannot safely autocorrect. We can keep the autocorrect disabled (disabled by default) but since many of our users are more used to the conditional style and our codebase has several instances of the predicate style, we're choosing to disable for now. It is worth noting that the conditional style is more performant, at least through ruby 2.4. rubocop/rubocop#3633 examples ``` EnforcedStyle: predicate (default) foo == 0 0 > foo bar.baz > 0 foo.zero? foo.negative? bar.baz.positive? EnforcedStyle: comparison foo.zero? foo.negative? bar.baz.positive? foo == 0 0 > foo bar.baz > 0 ``` Reviewed By: jaymzh Differential Revision: D10371724 fbshipit-source-id: 7ea7bfc3e365759e719355003448efcf167a3d9b
The default value (
predicate
) of the Style/NumericPredicate cop, produces slower code.My opinion is that rubocop should default to the faster method, with the option of having the other layout.
Tested with Benchmark-ips & ruby 2.0, 2.1, 2.2, 2.3, 2.4dev
Test code example:
The text was updated successfully, but these errors were encountered: