[Mono.Android] Avoid lock for activation constructor cache - #12409
Open
jonathanpeppers wants to merge 2 commits into
Open
[Mono.Android] Avoid lock for activation constructor cache#12409jonathanpeppers wants to merge 2 commits into
jonathanpeppers wants to merge 2 commits into
Conversation
Use a ConcurrentDictionary stateful factory so cached proxy activation lookups no longer enter a monitor while preserving constructor trim annotations. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: bafa7c78-cb0c-45a5-9ec0-65cecea94620
jonathanpeppers
force-pushed
the
jonathanpeppers-audit-android-lock-caches
branch
from
August 17, 2026 18:30
2e90263 to
6fb9a22
Compare
jonathanpeppers
changed the base branch from
jonathanpeppers-cache-jni-method-ids
to
main
August 17, 2026 18:31
Contributor
There was a problem hiding this comment.
Pull request overview
This PR updates the Mono.Android activation path to remove monitor-based locking around the activation-constructor cache by switching it to a small ConcurrentDictionary, while preserving trimming annotations. It also adds an Android in-process BenchmarkDotNet benchmark intended to measure the cached TypeManager.CreateProxy path.
Changes:
- Replaced the lock-protected
Dictionary<Type, ActivationConstructor>cache with a smallConcurrentDictionaryand a single statefulGetOrAddfactory. - Added an
AnnotatedTypewrapper to preserveDynamicallyAccessedMembersflow through theGetOrAddstate parameter. - Added an Android BenchmarkDotNet benchmark targeting the cached
CreateProxyactivation path.
Reviewed changes
Copilot reviewed 2 out of 2 changed files in this pull request and generated 1 comment.
| File | Description |
|---|---|
| tests/Android.Benchmarks/ActivationConstructorCacheBenchmarks.cs | Adds a new on-device benchmark for the cached TypeManager.CreateProxy path. |
| src/Mono.Android/Java.Interop/TypeManager.cs | Reworks activation-constructor caching to be lock-free for reads via ConcurrentDictionary.GetOrAdd while keeping trimming annotations. |
Use an unmanaged jstring global reference so the CreateProxy benchmark exercises normal managed activation instead of duplicate-peer detection. Co-authored-by: Copilot App <223556219+Copilot@users.noreply.github.com> Copilot-Session: bafa7c78-cb0c-45a5-9ec0-65cecea94620
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.
Summary
TypeManageractivation-constructor cache with a deliberately smallConcurrentDictionaryGetOrAddcall while preserving constructor trimming annotations without suppressionsCreateProxypathFollow-up to #12377.
Results
Measured on a Pixel 10 running Android 16 with CoreCLR:
CreateProxybenchmarkThe benchmark uses an unregistered JNI
jstringglobal reference so each iteration exercises normal managed activation without duplicate-peer detection. The cold-start paired changed-minus-base 95% confidence interval is [-7.842, +4.192] ms, indicating no measurable startup regression.apkdiffreports only a 32-byte uncompressed increase inlibassembly-store.so; this does not change final APK size. ExtractedMono.Android.dllremains 1,913,344 bytes.Validation
ReflectionCreateProxydevice fixture: 3 passed, 0 failed