ToList Usage #2692

Open
opened 2026-02-06 22:19:31 +03:00 by OVERLORD · 13 comments
Owner

Originally created by @shortspider on GitHub (Mar 3, 2021).

Describe the bug
In looking at the code I noticed that ToList is used quite liberally. Take the following as an example (but there are lots more):
image
The problem with the above is that every call to ToList creates a copy of the IEnumerable. So in the above we create a copy of _profiles.Values (this may or may not be needed depending on thread safety) and we then create a another copy after we order and filter the first copy. This second copy at least isn't needed at all since the method states that it returns an IEnumerable. This copying is death by 1000 cuts since we need to spend CPU allocating the memory for the copy, doing the copy, and then collecting the original and copy when the GC kicks in.

There are a number of usages like this in the code. I'd like to take a crack at fixing them but wanted to know how best to go about it. One big PR? A PR per class or namespace? I understand this isn't exactly a bug report but it's not a feature request either so I thought I'd post here. Please feel free to close and direct me somewhere else if that's more appropriate.

Originally created by @shortspider on GitHub (Mar 3, 2021). **Describe the bug** In looking at the code I noticed that `ToList` is used quite liberally. Take the following as an example (but there are lots more): ![image](https://user-images.githubusercontent.com/5712543/109748185-6c7f4600-7ba6-11eb-809b-8b5f9b07277b.png) The problem with the above is that every call to `ToList` creates a copy of the `IEnumerable`. So in the above we create a copy of `_profiles.Values` (this may or may not be needed depending on thread safety) and we then create a another copy after we order and filter the first copy. This second copy at least isn't needed at all since the method states that it returns an `IEnumerable`. This copying is death by 1000 cuts since we need to spend CPU allocating the memory for the copy, doing the copy, and then collecting the original and copy when the GC kicks in. There are a number of usages like this in the code. I'd like to take a crack at fixing them but wanted to know how best to go about it. One big PR? A PR per class or namespace? I understand this isn't exactly a bug report but it's not a feature request either so I thought I'd post here. Please feel free to close and direct me somewhere else if that's more appropriate.
OVERLORD added the good first issueconfirmed labels 2026-02-06 22:19:31 +03:00
Author
Owner

@crobibero commented on GitHub (Mar 3, 2021):

I would recommend a class per class/namespace, large PRs tend to stick around as they’re less likely to be reviewed

@crobibero commented on GitHub (Mar 3, 2021): I would recommend a class per class/namespace, large PRs tend to stick around as they’re less likely to be reviewed
Author
Owner

@dkanada commented on GitHub (Mar 3, 2021):

I'd say LoC is a better indicator of review turnaround time. Anything under 200 LoC is usually pretty fast here so if you can fit everything within that size it could be fine :p

@dkanada commented on GitHub (Mar 3, 2021): I'd say LoC is a better indicator of review turnaround time. Anything under 200 LoC is usually pretty fast here so if you can fit everything within that size it could be fine :p
Author
Owner

@cvium commented on GitHub (Mar 3, 2021):

Your example is not terribly great. The first .ToList() is probably superfluous given the lock on the _profiles object. But the last .ToList() is necessary. LINQ is evaluated lazily, so you have to force the enumeration inside the lock.

@cvium commented on GitHub (Mar 3, 2021): Your example is not terribly great. The first `.ToList()` is probably superfluous given the lock on the `_profiles` object. But the last `.ToList()` is necessary. LINQ is evaluated lazily, so you have to force the enumeration inside the lock.
Author
Owner

@shortspider commented on GitHub (Mar 3, 2021):

@cvium I mean, you still are pointing out that one ToList is unneeded so I think it serves as a decent example. The lock does complicate it though.
@crobibero @dkanada thanks for the feedback, I'll try and go by class/namespace while keeping the LoC down.

@shortspider commented on GitHub (Mar 3, 2021): @cvium I mean, you still are pointing out that one `ToList` is unneeded so I think it serves as a decent example. The lock does complicate it though. @crobibero @dkanada thanks for the feedback, I'll try and go by class/namespace while keeping the LoC down.
Author
Owner

@stale[bot] commented on GitHub (Jul 1, 2021):

This issue has gone 120 days without comment. To avoid abandoned issues, it will be closed in 21 days if there are no new comments.
If you're the original submitter of this issue, please comment confirming if this issue still affects you in the latest release or nightlies, or close the issue if it has been fixed. If you're another user also affected by this bug, please comment confirming so. Either action will remove the stale label.
This bot exists to prevent issues from becoming stale and forgotten. Jellyfin is always moving forward, and bugs are often fixed as side effects of other changes. We therefore ask that bug report authors remain vigilant about their issues to ensure they are closed if fixed, or re-confirmed - perhaps with fresh logs or reproduction examples - regularly. If you have any questions you can reach us on Matrix or Social Media.

@stale[bot] commented on GitHub (Jul 1, 2021): This issue has gone 120 days without comment. To avoid abandoned issues, it will be closed in 21 days if there are no new comments. If you're the original submitter of this issue, please comment confirming if this issue still affects you in the latest release or nightlies, or close the issue if it has been fixed. If you're another user also affected by this bug, please comment confirming so. Either action will remove the stale label. This bot exists to prevent issues from becoming stale and forgotten. Jellyfin is always moving forward, and bugs are often fixed as side effects of other changes. We therefore ask that bug report authors remain vigilant about their issues to ensure they are closed if fixed, or re-confirmed - perhaps with fresh logs or reproduction examples - regularly. If you have any questions you can reach us on [Matrix or Social Media](https://docs.jellyfin.org/general/getting-help.html).
Author
Owner

@Bond-009 commented on GitHub (Jul 1, 2021):

@cvium Do we want to keep this open? All the biggest allocations should already be fixed, no?

@Bond-009 commented on GitHub (Jul 1, 2021): @cvium Do we want to keep this open? All the biggest allocations should already be fixed, no?
Author
Owner

@cvium commented on GitHub (Jul 1, 2021):

The ToList usage is still a bit excessive in some places, so dunno

@cvium commented on GitHub (Jul 1, 2021): The ToList usage is still a bit excessive in some places, so dunno
Author
Owner

@jellyfin-bot commented on GitHub (Oct 24, 2023):

This issue has gone 120 days without an update and will be closed within 21 days if there is no new activity. To prevent this issue from being closed, please confirm the issue has not already been fixed by providing updated examples or logs.

If you have any questions you can use one of several ways to contact us.

@jellyfin-bot commented on GitHub (Oct 24, 2023): This issue has gone 120 days without an update and will be closed within 21 days if there is no new activity. To prevent this issue from being closed, please confirm the issue has not already been fixed by providing updated examples or logs. If you have any questions you can use one of several ways to [contact us](https://jellyfin.org/contact).
Author
Owner

@jellyfin-bot commented on GitHub (Nov 14, 2023):

This issue was closed due to inactivity.

@jellyfin-bot commented on GitHub (Nov 14, 2023): This issue was closed due to inactivity.
Author
Owner

@carmosilva-vinicius commented on GitHub (Nov 23, 2024):

I'm interested in tackling this issue as a good-first-issue. Here are my thoughts on how to approach it:

  • Replace ToList() calls with AsEnumerable() where possible to avoid unnecessary materialization.
  • Combine LINQ operations to reduce intermediate allocations.
  • Use pre-allocation for Lists when the size is known ahead of time.
  • Consider using Span for zero-allocation processing of large arrays/buffers.
  • Optimize LINQ queries to minimize intermediate results.

Would this be an appropriate good-first-issue for a newcomer to the project? Are there any specific areas of the codebase that would be good places to start?

@carmosilva-vinicius commented on GitHub (Nov 23, 2024): I'm interested in tackling this issue as a good-first-issue. Here are my thoughts on how to approach it: - Replace ToList() calls with AsEnumerable() where possible to avoid unnecessary materialization. - Combine LINQ operations to reduce intermediate allocations. - Use pre-allocation for Lists when the size is known ahead of time. - Consider using Span<T> for zero-allocation processing of large arrays/buffers. - Optimize LINQ queries to minimize intermediate results. Would this be an appropriate good-first-issue for a newcomer to the project? Are there any specific areas of the codebase that would be good places to start?
Author
Owner

@felix920506 commented on GitHub (Nov 24, 2024):

Please head to the development channel in our chat rooms to discuss the matter further. https://jellyfin.org/contact

@felix920506 commented on GitHub (Nov 24, 2024): Please head to the development channel in our chat rooms to discuss the matter further. https://jellyfin.org/contact
Author
Owner

@sarah-strawberries commented on GitHub (Nov 21, 2025):

My classmates @happystar9 and @jesus-sunuc and I are interested in helping out with this; any input on what we should get started with?

@sarah-strawberries commented on GitHub (Nov 21, 2025): My classmates @happystar9 and @jesus-sunuc and I are interested in helping out with this; any input on what we should get started with?
Author
Owner

@sjorge commented on GitHub (Nov 21, 2025):

My classmates @happystar9 and @Jesus-sunuc and I are interested in helping out with this; any input on what we should get started with?

Felix's message above is still valid, hop over to the development chat on matrix or discord and ask there. It can take bit for someone to reply with timezones and all being a thing.

@sjorge commented on GitHub (Nov 21, 2025): > My classmates [@happystar9](https://github.com/happystar9) and [@Jesus-sunuc](https://github.com/Jesus-sunuc) and I are interested in helping out with this; any input on what we should get started with? Felix's message above is still valid, hop over to the development chat on matrix or discord and ask there. It can take bit for someone to reply with timezones and all being a thing.
Sign in to join this conversation.
1 Participants
Notifications
Due Date
No due date set.
Dependencies

No dependencies set.

Reference: starred/jellyfin#2692