Skip to content

Conversation

@notEthan
Copy link
Contributor

Since #count iterates the enumerable, it can be quite slow. #size is not always available but when it is, it can be much faster than #count.
For some large enumerables I work with, iterating the whole thing takes minutes, so doing it once for count and once for the actual iteration is terribly slow. These use an Enumerator with a block specifying size, and that is computed in relatively insignificant time, so using than when it is available is much better.

@paul
Copy link
Owner

paul commented Dec 16, 2024

I think this used count over size originally because when it was written, ActiveRecord::Relation#size would load the entire query, but #count would execute the COUNT SQL query. It doesn't look like that's the case any more: ActiveRecord::Relation#size

Is there any reason to use #count at all? Should it just be switched it #size in every case?

@notEthan
Copy link
Contributor Author

#size is not always defined - the Enumerable module defines #count but not #size; most classes that include Enumerable do define #size, but not all. And the Enumerator class does define #size but it returns nil unless the Enumerator is initialized with a block indicating the size.

@notEthan
Copy link
Contributor Author

Think this could make it in? I'm working with large enumerables again and having this in a release would be convenient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants