-
-
Notifications
You must be signed in to change notification settings - Fork 55
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
[fixes #22] Use Sets instead of Arrays #26
Conversation
I like this suggestion. In general I've often been frustrated with the Ruby community's dislike of sets. I've always attributed this to the fact they are not part of the core language and don't have a convenient literal syntax. |
So this breaks one cop (at parse time); I tweaked it in
|
Off the bat I can only think of another set of constants with the array versions derived from the set version, but that's going to be painful. Or we can use some custom adapter class that would behave more like array? |
Answer to #2 is simple actually... semantic versionning. If we break previous Rubocop, that means a major version (of the -ast) gem. And the core rubocop should always require |
So I'd say we should tweak the current gemspec from Then we can merge this PR for |
Sounds like a plan. Let's do it! |
The downside to this move is increased memory consumption. # frozen_string_literal: true
require 'set'
require 'memory_profiler'
def profile
puts ''
MemoryProfiler.report { yield }.pretty_print(scale_bytes: true)
puts ''
end
profile { COMPARISON_OPERATORS = %i[== === != <= >= > <].freeze }
profile { COMPARISON_OPERATORS_SET = %i[== === != <= >= > <].to_set.freeze } |
@ashmaroli you are correct that Sets take more memory than arrays. You need to realize these are constants. It's a one time memory-cost you can calculate in bytes. To avoid status quo bias, imagine they had been written as Sets all along and someone was proposing a PR to save those bytes in memory, at the cost of performance (and expressivity)... |
Yes, I understand that they're constants and won't matter much during runtime. Just saying that Sets are inherently heavier and when they are converted into arrays (perhaps in an extension outside the Core's control), it'll result in allocating more objects (one for the array, one each for constituent item). Disclaimer: I've not yet checked if a frozen Set constituent of frozen strings get converted into an array of frozen strings. |
FYI, |
Thanks for letting me know ✌️ |
FWIW, I'm quite frustrated by the state of affairs with I'm preparing a series of feature requests for ruby-core (comments welcome). One issue in particular is that most of our
Actually I think that might be best. I think it should be easy to define |
@marcandre We can close this PR, right? |
Yes :-) |
No description provided.