Repository navigation
Conversation
BuildKeyToInfoLookup performed two hash lookups per keyed item for both the old tree (ContainsKey + indexer set) and the new tree (TryGetValue + indexer set). Use TryAdd and CollectionsMarshal.GetValueRefOrAddDefault so each key is hashed once per tree. Adds KeyedListDiffBenchmark to the Components perf project. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
There was a problem hiding this comment.
🔵 Needs a closer look
The required frozen head/base source and existing-feedback bundle was not supplied, preventing independent provenance and prior-feedback deduplication checks.
No open issues 🎉
What changed in this PR
This PR reduces dictionary lookups when Blazor builds the keyed-item lookup during render-tree diffing, while preserving duplicate-key detection.
Changes:
- Uses
TryAddfor old-tree keys andCollectionsMarshal.GetValueRefOrAddDefaultfor new-tree keys. - Adds benchmarks for 500-item keyed lists in unchanged order and with reordering, insertion, and removal.
| File | Description |
|---|---|
src/Components/Components/src/RenderTree/RenderTreeDiffBuilder.cs |
Removes redundant dictionary lookups when processing keyed siblings. |
src/Components/Components/perf/KeyedListDiffBenchmark.cs |
Adds same-order and reordered keyed-list diff benchmarks. |
Review effort: Balanced
|
The only CI failure is 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
/azp run aspnetcore-ci 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
No pipelines are associated with this pull request. |
|
/azp run aspnetcore-ci Rerun reason: the failures don't touch the code changed here (
🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
|
Azure Pipelines: Successfully started running 1 pipeline(s). |
|
No pipelines are associated with this pull request. |
|
Compliance check: this is an internal-only change to 🤖 Posted by ASP.NET Core Perf 2 (AI Agent) |
Keep only the single-lookup TryAdd for the old-tree pass; the new-tree pass returns to TryGetValue + indexer to comply with the no-CollectionsMarshal rule. Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
/review |
Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
|
Looks like this PR hasn't been active for some time and the codebase could have been changed in the meantime. |
|
Small difference between |
Avoid redundant dictionary lookups when diffing keyed items
Hash each old-tree
@keyonce when building the keyed-item diff lookupDescription
When a sibling range contains
@keyed elements or components,RenderTreeDiffBuilder.BuildKeyToInfoLookupbuilds aDictionary<object, KeyedItemInfo>. For each old-tree key it calledContainsKeyand then the indexer setter, which is two hash lookups. Keys areobject, so each lookup costs a virtualGetHashCode/Equals.This change uses a single
TryAddfor old-tree keys. The new-tree pass is unchanged and still usesTryGetValuefollowed by the indexer. Duplicate-key detection, the exceptions it throws, and the diff output all stay the same. There are no public API or behavior changes.Benchmark
The PR adds
KeyedListDiffBenchmarkinsrc/Components/Components/perf. It diffs a<ul>with 500@keyed<li>items, once in the same order and once with a swap, an insert and a removal. Setup: BenchmarkDotNet--config perflab(InProcess) on a 2 vCPU Linux VM, 6 interleaved A/B rounds. Both versions allocate 0 B.Tests
Microsoft.AspNetCore.Components.Tests: 1303 passed, 8 skipped. This includes theRenderTreeDiffBuilderTestkeyed and duplicate-key cases.Benchmark (3 interleaved rounds)
KeyedListDiffBenchmark(added in this PR), 500@keyed items.Result: within noise.
SameOrdermeasured −5.8%, +1.9% and −2.2%, andReorderedmeasured −3.3%, −3.9% and +8.4%. Neither version allocates. In this 3-round A/B I can't separate the remaining change from noise.Setup: BenchmarkDotNet v0.13.0
--config perflab(InProcess, MemoryDiagnoser, 3 warmup + 15 measured iterations); AMD EPYC 9V74, 2 vCPU, Ubuntu 24.04 container; .NET SDK 12.0.100-alpha.1.26521.103, .NET 12.0.0 runtime (X64 RyuJIT).mainis the PR's merge base4f556f73a6.PRis the same tree with only this PR'ssrc/**/srcchanges applied. Both builds use the same benchmark source. Run order: main, PR, main, PR, main, PR.🤖 Posted by ASP.NET Core Perf 2 (AI Agent)