-
Notifications
You must be signed in to change notification settings - Fork 240
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
chore: Replace positive? with comparison operator #1586
Conversation
Opentelemtry used in many projects and micro performance could impact a lot. The default rubocop style was not designed in performance overview. [subjective] Benchmarks shows the `Numeric#positive?` is 1.32x slower, than 'number > 0'. ``` ruby 3.3.0 (2023-12-25 revision 5124f9ac75) +YJIT [arm64-darwin23] Warming up -------------------------------------- compare with 0 1.000 i/100ms positive? 1.000 i/100ms Calculating ------------------------------------- compare with 0 3.153 (± 0.0%) i/s - 95.000 in 30.132600s positive? 2.397 (± 0.0%) i/s - 72.000 in 30.042688s Comparison: compare with 0: 3.2 i/s positive?: 2.4 i/s - 1.32x slower ``` The change disables Style/NumericPredicate for rubocop as well. Signed-off-by: Michael Nikitochkin <michael.nikitochkin@gmx.net>
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thank you for this catch! We use rubocop-performance in the contrib repo, but haven't implemented it here.
It looks like this particular fix might not be part of rubocop-performance yet (cops list), so you could consider contributing a new cop with this swap to that repo as well! 😄
@kaylareopelle Thank you for the hint. |
@kaylareopelle The problem with this cop is that it is hard to know the type of variable. The change is good when I know for sure there is a Number and not another object or Anyway, I sent a PR to introduce a new cop: rubocop/rubocop-performance#440. I will wait for a response, but I think it may not be merged. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Curious where you spotted this one, did it show up on a profile in a production load?
I lack access to high-load applications (not anymore). However, it would be beneficial to seek input from a Ruby group to validate my theory. |
Opentelemetry is utilized across numerous projects, and even minor performance variations can have a significant impact.
It's worth noting that the default rubocop style may not have been specifically crafted with performance considerations in mind.
According to benchmarks on my machine M1, it appears that
Numeric#positive?
is approximately 1.32 times slower than using the comparison operator (number > 0
).benchmark script
The change disables Style/NumericPredicate for rubocop as well for sdk.
Propose to change for sdk gem in
BatchSpanProcessor
.The number suppose not be a
nil
or BigNumber.References