Skip to content

[generator] Avoid repeated dictionary lookups - #13449

Merged
jonathanpeppers merged 1 commit into
mainfrom
simonrozsival-dictionary-lookups
Nov 10, 2026
Merged

jonathanpeppers merged 1 commit into
mainfrom
simonrozsival-dictionary-lookups

Conversation

@simonrozsival

Copy link
Copy Markdown
Member

Context: #12724

MonoDroid.Generation.GenBase.ContainsMethod() checks a composite Java method signature with ContainsKey() 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() or TryAdd() where one lookup suffices. For conditional insert-or-update paths in the R8 and typemap generators, use CollectionsMarshal.GetValueRefOrAddDefault() while preserving first-mapping and ambiguous-remapping behavior. Membership-only checks remain unchanged.

Measurements

Compared baseline 6b8fb8c3742b8ea4aa5f9f92e70a601a28f83360 with 532c8876218694cdaf979d7f6ce530d900ca78b0 on macOS ARM64, .NET 11 RC2. A BenchmarkDotNet microbenchmark modeled the ContainsMethod() 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 lockf lock.

  • lockf -k /tmp/dotnet-android-modernization-lock-339a3b0c dotnet build src/generator/generator.csproj -c Release -v:q -nologo — passed for baseline and candidate.
  • Generated Mono.Android API 37 bindings with both generator builds — all 6,362 output files were byte-identical; logs matched after normalizing output paths.
  • 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.

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]>
Copilot AI balanced review requested due to automatic review settings November 10, 2026 12:13

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 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

@simonrozsival simonrozsival added the ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable). label Nov 10, 2026
@jonathanpeppers
jonathanpeppers merged commit ff7a06d into main Nov 10, 2026
41 checks passed
@jonathanpeppers
jonathanpeppers deleted the simonrozsival-dictionary-lookups branch November 10, 2026 19:39
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

ready-to-review This PR is ready to review/merge, I think any CI failures are just flaky (ignorable).

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants