Skip to content

TypeManager.CreateProxy does not cache constructor reflection #7479

Description

@jeromelaban

Android application type

Android for .NET (net6.0-android, etc.)

Affected platform version

.NET 6 and 7

Description

The following piece of code is very expensive when manipulating native objects:

https://github.com/xamarin/xamarin-android/blob/91669a4e489104668fdd07382e40cf070aa559ac/src/Mono.Android/Java.Interop/TypeManager.cs#L327-L348

As there is no caching for getting constructors, in the worst case, twice:

image

Steps to Reproduce

Get ShapeDrawable.Paint in a loop.

Did you find any workaround?

None.

Relevant log output

No response

Activity

  1. added and removed
    needs-triageIssues that need to be assigned.
    on Oct 30, 2024
  2. jonathanpeppers commented on Oct 30, 2024

    @jonathanpeppers
    Member

    I can still see this in a recent .NET MAUI app:

    Image

    It looks like any API that returns a new instance of a Java.Lang.Object subclass would look up the constructor again.

    1. First call, Java object A: looks up constructor
    2. Second call, Java object A: already has a C# object, reuses object, doesn't call constructor at all
    3. Third call, Java object B: looks up constructor again

    I think this could happen for commonly called APIs like View.Background that return unique Drawable instances. MAUI calls these a lot.

  3. added this to the .NET 10 milestone on Oct 30, 2024
  4. added
    enhancementProposed change to current functionality.
    on Nov 1, 2024
  5. added theissue type on Apr 3, 2025
  6. removed their assignment
    on Mar 26, 2026
  7. added a commit that references this issue on Aug 12, 2026
    feeba90
  8. locked and limited conversation to collaborators on Sep 12, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Labels

Area: App RuntimeIssues in `libmonodroid.so`.Area: PerformanceIssues with performance.enhancementProposed change to current functionality.

Projects

No projects

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions