`Set#count` performance issues
Set#size is O(1), but
Set#count is O(N).
I would like to add
alias count size to
Is it okay?
Updated by ioquatix (Samuel Williams) 10 months ago
I was okay with changing my implementation to use
#size - which is what I've done. However, I don't think there is anything wrong with optimising this use case if it doesn't add any overhead to existing use case. Because
Array does it too, it makes me think it's not a completely stupid idea.
If that's the case, maybe we should consider whether this logic belongs in
Enumerable#size is implemented by calling
#each by default. However, maybe that introduce bad behaviour/expectations about behaviour. Not sure.
Enumerable#size makes less sense to me, but it would be consistent across how it's used if we implemented as above.
Updated by Hanmac (Hans Mackowiak) 10 months ago
it might not be able to be generic in Enumerable
for example an Widget might be Enumerable with the child widgets, but its size would be
class Set def count(*args) return size if !block_given? && args.empty? super end end
Updated by ioquatix (Samuel Williams) 9 months ago
- Status changed from Open to Rejected
Given the discussion, I'm okay if we just reject this feature. knu (Akinori MUSHA) it's up to you if you want to implement it or not. Even thought it can make sense from one POV, the correct solution for most users is just to use
#size when it's available, rather than making
#count more complicated implementation (and potentially slower).
Ultimately, it was my fault for not understanding