-
-
Notifications
You must be signed in to change notification settings - Fork 3.1k
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
Remove (disable) Perfomace/Casecmp (at least for Ruby 2.4) #4277
Comments
Here is the issue for |
If you are using Ruby 2.4 then the new |
@backus Yes, but it's only in Ruby 2.4, and it's slower than So, In Ruby < 2.4… We need to use |
I'd add a style check suggesting this use of |
Ruby >= 2.4: Ruby < 2.4: No # frozen_string_literal: true
require 'benchmark/ips'
require 'unicode'
SLUG = 'АБВГ'
def downcase
Unicode.downcase(SLUG) == 'абвг' # => true
end
def casecmp
Unicode.downcase(SLUG).casecmp('абвг').zero? # => true
end
Benchmark.ips do |x|
x.report('String#downcase + ==') { downcase }
x.report('String#casecmp') { casecmp }
x.compare!
end And that's results:
|
Style checks are not about speed, but about writing elegant code. From time to time I think the whole performance cops initiative was misguided, as the performance of certain things changes drastically between MRI revisions. Not to mention it might be completely different on JRuby and Rbx. One we finalize some extensions API, probably they would be moved out of RuboCop's core. Together with the Rails cops. |
Many people don't really deal with non-ASCII strings, so this is not as big of an issue as it might seem. If everything you deal with is in English (quite common situation) than you're golden. If not - you should probably just disable this cop. So far you're the first person to complain about this, which leads to believe it's not a big problem in practice. |
Yes, but I'm not talking about "Style checks", but about Maybe this cop must be modified and moved to another scope (
Good thoughts.
Okay, you're probably right. |
Yes. At least performance cops can not be "add and forget." The problem with language level optimisations in dynamic languages is that there are so many factors.
Agreed. It should probably be handled by someone who has the interest to set up some performance test suite and run it for each new Ruby version. |
$ rubocop -V So, String#downcase + == is good compromise between Unicode support (but only in Ruby 2.4, again) and perfomance. In Ruby < 2.4… We need to use unicode gem in any way :( And casecmp + zero? will be faster, yes. Not sure what's the best course of action here. Ruby < 2.4: No casecamp? method, and we're needing to use unicode gem (or any other external solution), which make code like this: frozen_string_literal: truerequire 'benchmark/ips' require 'unicode' SLUG = 'АБВГ' def downcase def casecmp |
Please… |
There hasn't been much activity on this ticket and our Core Team is spread too thin on so many tasks, so I'll just close it for the sake of having a cleaner lists of tasks to focus on. We'd gladly review a PR, but it's unlikely that'd we're going to tackle this ourselves in the foreseeable future. In general Performance cops are inherently linked to some MRI version, which is part of the reason we'll remove all of them from the core of RuboCop and move them to https://github.com/rubocop-hq/rubocop-performance. |
Issue still not resolved, and I think it should be opened. For cleaner lists of tasks you can use labels or GitHub Projects. Closed undone issues will be forgotten or duplicated.
I'd gladly make a PR, but it's a bit hard for me make serious multiple checks inside cops, inspecting source nodes, etc. |
Expected behavior
No warnings for:
Actual behavior
Warning: Use
casecmp
instead ofdowncase ==
.Explanation
The
String#casecmp
method does not work with Unicode (even in Ruby 2.4.1), this is written in the documentation.There is a method
String#casecmp?
, which works with Unicode, but it is slower:RuboCop version
The text was updated successfully, but these errors were encountered: