Skip to content

[7.x] Make LazyCollection#countBy be lazy#33801

Merged
taylorotwell merged 2 commits into
laravel:7.xfrom
JosephSilber:count-by-lazy
Aug 10, 2020
Merged

[7.x] Make LazyCollection#countBy be lazy#33801
taylorotwell merged 2 commits into
laravel:7.xfrom
JosephSilber:count-by-lazy

Conversation

@JosephSilber
Copy link
Copy Markdown
Contributor

The collection's current countBy implementation uses groupBy.

groupBy is inherently not lazy (being a form of bucket sort). This means that in order to count occurrences, we need to pull the whole dataset into memory at once.

We can do better.

The code in this PR only pulls a single item at a time, and increases the counter. The actual item is then promptly discarded, so that we never keep more than a single item in memory.

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