Repository navigation
Skip clearing reference-free OrderedDictionary entries - #137596
Merged
Merged
Conversation
|
Azure Pipelines: Successfully started running 3 pipeline(s). 12 pipeline(s) were filtered out due to trigger conditions. There may be pipelines that require an authorized user to comment /azp run to run. |
Contributor
|
Tagging subscribers to this area: @dotnet/area-system-collections |
Member
|
/ba-g failures look unrelated (timeouts and iOS installation) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
For batches of 16 collections with 65,536 items each, mean
Clear()time fell by 68.25-78.89% forintand 91.90-93.31% for a 64-byte value type in two BenchmarkDotNet comparisons.This follows #136605.
Clear()now sets the count to zero and resets the buckets without clearing the entry array when neither keys nor values contain object references.RemoveAt()also skips clearing the unused last entry. New items overwrite old entries before use. Types that contain references, including structs, are still cleared.The optimization uses
#if NET. OlderSystem.Text.Jsontargets still clear the entries.Performance
Selected
ClearBench<T>.ClearOnlyresults for 16 collections per batch. Setup and validation are outside timing. Times are microseconds (us) per batch, shown as mean +/- sample standard deviation for each process.int/intint/intint/Wideint/WideBenchmarkDotNet
0.16.0-preview.2,InProcessEmitToolchain, verified High process priority. Each version ran in two fresh processes. Clear-only used 10 warmups and 100 measurements. Clear-and-refill and removal used 8 warmups and 30 measurements targeting 500 ms each.Windows 11 Pro x64 (build 26200), Ryzen 9 5950X, Release
.NET 12.0.0-dev. ReadyToRun and tiered compilation were disabled; concurrent workstation GC was enabled.Clear-only iterations were shorter than BenchmarkDotNet recommends, and some results had multiple timing clusters. All samples are included. These comparisons do not establish statistical significance. The
int/RefValueClear-only runs were slower at both sizes, with large variation. At 65,536 items, clear-and-refill means differed by less than 0.3%.Validation
System.Collections.Testsbuilt from420ee500343passed 33,975 tests. Tests ran on the isolated benchmark runtime image with the matching collection implementation. No failures.Ordering_AddInsertRemoveClear_ExpectedOrderResultschecks keys, values, and entry order after removal, insertion, clear, and refill with different values.RemoveAt_DoesNotExposeRemovedEntrieschecks collision lookups after removing and reusing the first, middle, or last position. It verifies every surviving value and that the removed key remains absent.git diff --checkpassed.System.Text.Jsonbuilds were not run.Full benchmark results and setup
Widecontains eightlongfields (64 bytes), compares all fields, and hashes the first.RefValuecontains alongand an object reference. Input values are created once and reused.Benchmark baseline
8ac60f59b95, with this PR's collection implementation applied for the patched runs. OnlySystem.Collections.dlldiffers between the baseline and patched runtime images. Native runtime files are identical.Baseline/patched order was randomized with seed 136605. No outliers were removed. Clear-only uses one invocation per measurement and checks that all collections are empty afterward.
CycleBench<T>.ClearAndRefillclears and refills one collection per invocation. Refill is timed; existing storage and input values are reused. Each case recorded at least 10 seconds of measured work. Cleanup checks the final count.RemoveBench<T>.RemoveLastAndRefillremoves every item from the end, then refills the collection. Both steps are timed. Removal cases recorded 14.86 to 16.54 seconds of measured work per case in each process.Removal results, in microseconds per complete remove-and-refill cycle:
int/intint/intint/Wideint/Wideint/RefValueint/RefValueint/intint/intint/Wideint/Wideint/RefValueint/RefValueClear-only batches of sixteen collections, in microseconds.
int/intint/intint/Wideint/Wideint/RefValueint/RefValueint/intint/intint/Wideint/Wideint/RefValueint/RefValueSustained clear-and-refill cycles, in microseconds.
int/intint/intint/Wideint/Wideint/RefValueint/RefValueint/intint/intint/Wideint/Wideint/RefValueint/RefValueHigher patched means in at least one process:
int/RefValueat 1,024 items (Clear-only, up to 666.3%),int/RefValueat 65,536 items (Clear-only, up to 81.1%),int/RefValueat 1,024 items (clear-and-refill, up to 6.3%),int/RefValueat 65,536 items (clear-and-refill, up to 0.2%),int/intat 65,536 items (remove-last-and-refill, up to 1.3%),int/RefValueat 65,536 items (remove-last-and-refill, up to 1.7%).Timed methods
Note
The implementation, tests, and pull request description were prepared with OpenAI Codex (Fable). I used Tedd.DotNetLab for the performance investigation. I have manually reviewed and checked everything.