Repository navigation
[generator] Avoid repeated dictionary lookups - #13449
Merged
Merged
Conversation
Replace ContainsKey + indexer pairs with TryGetValue/TryAdd or CollectionsMarshal.GetValueRefOrAddDefault where the key is looked up again. In GenBase.ContainsMethod this also avoids building the JavaName + JniSignature key string twice. Generator output for Mono.Android API 37.0 is byte-identical. Co-authored-by: Copilot App <[email protected]>
Contributor
There was a problem hiding this comment.
🟢 Approval recommended
No active review issues are recorded for the dictionary-lookup optimizations.
No open issues 🎉
What changed in this PR
Reduces redundant dictionary lookups in .NET for Android’s binding generator, R8 remapping task, and trimmable typemap generator, continuing the allocation-reduction work from #12724.
Changes:
- Uses
TryGetValue()for method, property, generic-type, and XML importer lookups. - Uses
TryAdd()and dictionary value references for conditional insertion and updates, preserving first-mapping and ambiguity behavior. - Reports lower synthetic lookup time and allocations, byte-identical generated bindings, and passing host tests; no end-to-end speedup is established.
| File | Description |
|---|---|
| src/Microsoft.Android.Sdk.TrimmableTypeMap/Generator/ModelBuilder.cs | Inserts shared wrapper targets with one lookup. |
| src/Microsoft.Android.Build.Tasks/Tasks/GenerateR8JniRemapping.cs | Updates remapping entries through value references. |
| src/generator/Java.Interop.Tools.Generator.ObjectModel/Symbols/GenericTypeParameter.cs | Avoids repeated generic parameter lookups. |
| src/generator/Java.Interop.Tools.Generator.ObjectModel/Symbols/GenericSymbol.cs | Retrieves mapped type parameters in one lookup. |
| src/generator/Java.Interop.Tools.Generator.ObjectModel/NamespaceMapping.cs | Uses TryAdd() to preserve the first namespace mapping. |
| src/generator/Java.Interop.Tools.Generator.ObjectModel/GenBase.cs | Consolidates method and property lookups. |
| src/generator/Java.Interop.Tools.Generator.Importers/XmlApiImporter.cs | Iterates nested entries directly and simplifies ancestor lookup. |
Review effort: Balanced
jonathanpeppers
approved these changes
Nov 10, 2026
This was referenced Nov 14, 2026
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.
Context: #12724
MonoDroid.Generation.GenBase.ContainsMethod()checks a composite Java method signature withContainsKey()and then uses the indexer on a hit, constructing and hashing the same key twice. Similar repeated lookups occur in the XML importer, generic type handling, R8 remapping, and trimmable typemap generation.Use
TryGetValue()orTryAdd()where one lookup suffices. For conditional insert-or-update paths in the R8 and typemap generators, useCollectionsMarshal.GetValueRefOrAddDefault()while preserving first-mapping and ambiguous-remapping behavior. Membership-only checks remain unchanged.Measurements
Compared baseline
6b8fb8c3742b8ea4aa5f9f92e70a601a28f83360with532c8876218694cdaf979d7f6ce530d900ca78b0on macOS ARM64, .NET 11 RC2. A BenchmarkDotNet microbenchmark modeled theContainsMethod()lookup using 51,745 API 37 method signatures, probing each method in its own class and the next class. Mean time fell from 3.968 ms to 2.917 ms, and allocation from 16.91 MB to 11.14 MB per benchmark operation. This is a synthetic lookup workload, not a measurement of the production method itself.For the full generator on
api-37.0.xml, the median of 10 paired runs was 9.39 s before and 9.35 s after; the difference is within noise, so this does not establish an end-to-end speedup. The R8 and typemap changes were not benchmarked.Validation
All commands were serialized with the shared
lockflock.lockf -k /tmp/dotnet-android-modernization-lock-339a3b0c dotnet build src/generator/generator.csproj -c Release -v:q -nologo— passed for baseline and candidate.lockf -k /tmp/dotnet-android-modernization-lock-339a3b0c dotnet test tests/generator-Tests/generator-Tests.csproj -v:q -nologo— 478 passed.lockf -k /tmp/dotnet-android-modernization-lock-339a3b0c dotnet test tests/Java.Interop.Tools.Generator-Tests/Java.Interop.Tools.Generator-Tests.csproj -v:q -nologo— 18 passed.lockf -k /tmp/dotnet-android-modernization-lock-339a3b0c dotnet test src/Microsoft.Android.Build.Tasks/Tests/Microsoft.Android.Build.Tasks.Tests/Microsoft.Android.Build.Tasks.Tests.csproj -v:q -nologo— 863 passed, 12 skipped.On-device tests were not run; the changes affect build-time generators and tasks.