Skip to content
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

Do not use ImmutableList for Events collections #113

Merged
merged 1 commit into from
May 24, 2023

Conversation

bill-long
Copy link
Member

I noticed in my lab, which is a VM with limited CPUs, that if I open a large event log (~500,000 events) which is also very busy adding new events, choosing Load New Events from the view menu can make the app hang for several seconds. After doing some testing, I believe this is due to the use of ImmutableList.

Our Fluxor Store needs to produce immutable state, so ImmutableList seems like a natural choice for our event collections. We can't use IEnumerable, because we need to be able to index into the collection, and anyway, IEnumerable leaves it possible to modify the underlying collection.

ImmutableList would be fine for 100 or 1000 items, but when we have 500,000 events and we now need to concatenate two ImmutableLists to produce a new one for the state, it gets very expensive and very slow.

I compared Concat() performance between List<>, ImmutableList<>, and ReadOnlyCollection<>. This is starting with a collection of 50,000 items and then calling Concat 10,000 times to add 10,000 new items:

❯ dotnet run
ReadOnlyCollection: 94ms
ImmutableList: 4758ms
List: 99ms

This is too high of a performance price to pay in a critical UI path, even if it might save us from accidentally modifying the underlying collection. We can use ReadOnlyCollection, which at least limits mutability somewhat, and as long as we are disciplined about making sure the Reducers always produce a new collection and don't mutate the old one, we should be fine.

Therefore, this PR drops ImmutableList for ReadOnlyCollection in the EventLogState.

@bill-long bill-long requested a review from a team as a code owner May 24, 2023 06:20
@jschick04 jschick04 merged commit 96afeb8 into main May 24, 2023
@jschick04 jschick04 deleted the bilong-immutableperf branch May 24, 2023 15:22
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