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

Use HashSet instead of List for temporary check in transform handling #6106

Merged
merged 4 commits into from
Jan 8, 2024

Conversation

peppy
Copy link
Member

@peppy peppy commented Jan 4, 2024

Noticed that this has no reason to be a list. Changing to HashSet avoids overheads from list capacity resizing.

@bdach
Copy link
Collaborator

bdach commented Jan 4, 2024

While the choice of HashSet<T> makes sense here based on the ops executed on the collection, I do question the "list capacity resizing" claim a little bit. Hashsets aren't magic, they are also array-based and also resize. And the initial size of a hashset created via the parameterless ctor appears to be zero, which only then gets expanded to 3 on first add and then next prime larger than twice the previous one. This should generally match the exponential expand strategy a normal list uses and result in amortised constant insertion time. And a list starts with capacity 4.

So I'm not sure I'm seeing the overheads... Are there profiling results?

@peppy
Copy link
Member Author

peppy commented Jan 4, 2024

So I'm not sure I'm seeing the overheads... Are there profiling results?

Yeah, I'll provide at some point when I have the bandwidth. During gameplay there are some scenarios where transforms get cancelled a lot, and this becomes a top 5 allocator.

Can also cache to a private to improve things.

Also I read the hashset resize and it looked to be lower overhead than list, but maybe i misread.

@pull-request-size pull-request-size bot added size/S and removed size/XS labels Jan 6, 2024
@peppy
Copy link
Member Author

peppy commented Jan 6, 2024

Here's a comparison during 5 seconds of gameplay:

Before After
2024-01-07 04 49 22@2x 2024-01-07 05 00 11@2x

I may switch this to use ArrayPool to avoid the retention overhead, not sure.

@pull-request-size pull-request-size bot added size/M and removed size/S labels Jan 7, 2024
@peppy
Copy link
Member Author

peppy commented Jan 7, 2024

Also added benchmark coverage of this.

Method Mean Error StdDev Gen0 Allocated
CreateSingleBlank 4.048 ns 0.1419 ns 0.1457 ns 0.0230 96 B
CreateSequenceThenRewind 618,818.119 ns 4,703.7909 ns 3,927.8784 ns 7.8125 36689 B
CreateSequenceThenRewindManyChildren 867,089.049 ns 16,063.5529 ns 14,239.9213 ns 19.5313 83377 B
CreateSequenceThenClearAfter 4,342.572 ns 28.3819 ns 22.1587 ns 1.1139 4688 B
Expiry 29,001.144 ns 117.0617 ns 103.7722 ns 0.2747 1208 B
CreateSequenceWithDefaultEasing 1,192.490 ns 6.2673 ns 4.8931 ns 0.2861 1200 B
ApplySequenceWithDefaultEasing 367.749 ns 3.0380 ns 2.6931 ns 0.0648 272 B
CreateSequenceWithValueEasing 1,219.856 ns 9.6691 ns 8.0741 ns 0.2861 1200 B
ApplySequenceWithValueEasing 366.232 ns 1.4552 ns 1.1361 ns 0.0648 272 B
CreateSequenceWithReferenceEasing 1,286.804 ns 25.2288 ns 49.7993 ns 0.3033 1272 B
ApplySequenceWithReferenceEasing 382.487 ns 7.2113 ns 7.0824 ns 0.0706 296 B
Method Mean Error StdDev Gen0 Gen1 Allocated
CreateSingleBlank 4.126 ns 0.1401 ns 0.1376 ns 0.0229 - 96 B
CreateSequenceThenRewind 629,838.030 ns 8,592.5113 ns 7,617.0375 ns 0.9766 - 4689 B
CreateSequenceThenRewindManyChildren 872,620.208 ns 12,992.7174 ns 12,153.3959 ns 11.7188 0.9766 51342 B
CreateSequenceThenClearAfter 4,291.789 ns 20.7813 ns 18.4220 ns 1.1139 - 4688 B
Expiry 29,159.790 ns 271.9896 ns 227.1237 ns 0.2747 - 1208 B
CreateSequenceWithDefaultEasing 1,186.912 ns 14.5979 ns 12.1899 ns 0.2861 - 1200 B
ApplySequenceWithDefaultEasing 374.882 ns 6.6652 ns 6.5462 ns 0.0648 - 272 B
CreateSequenceWithValueEasing 1,234.310 ns 8.8299 ns 7.3734 ns 0.2861 - 1200 B
ApplySequenceWithValueEasing 375.117 ns 2.4284 ns 1.8959 ns 0.0648 - 272 B
CreateSequenceWithReferenceEasing 1,230.045 ns 11.3266 ns 9.4582 ns 0.3033 - 1272 B

@bdach bdach self-requested a review January 8, 2024 08:13
@bdach bdach enabled auto-merge January 8, 2024 08:29
@bdach bdach merged commit 2a3eae1 into ppy:master Jan 8, 2024
20 of 21 checks passed
@peppy peppy deleted the transforms-less-list branch January 17, 2024 15:38
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants