Skip to content

[Perf] Skip redundant property mapper calls during handler connection #34088

Open

Description

@simonrozsival

Summary

When a .NET MAUI control is first displayed, its handler calls PropertyMapper.UpdateProperties() to push every mapped property from the virtual view to the platform view. For a typical control like Label, this means ~49 mapper calls — even though the developer may have only set 1 or 2 properties (e.g., Text). The remaining ~47 calls push values the platform view already has by default.

In an app with many controls, this "death by a thousand cuts" becomes a measurable startup cost. Each mapper call involves reading the C# property value, type conversion, and a platform interop call (JNI on Android, Obj-C message send on iOS/macCatalyst, WinRT on Windows).

Background and prior work

This issue is a continuation of the work in:

When #17303 was closed, @PureWeen noted: "The next effort here will be to assess where we could still make some additional changes to glob together mapper changes."

This issue proposes a complementary, centralized approach that goes further than the per-mapper guards in #27259 by skipping at the PropertyMapper.UpdateProperties() level itself — before the mapper delegate is even invoked.

The problem in detail

During ElementHandler.SetVirtualView(), the handler enters Connecting state and calls PropertyMapper.UpdateProperties(). This method iterates all mapper keys and calls each mapper delegate unconditionally.

Benchmark setup: A test app with 500 controls (50 each of Label, Button, Entry, Switch, Slider, CheckBox, ProgressBar, ActivityIndicator, Editor, SearchBar). Most properties are left at defaults — only Text, Placeholder, Progress, and IsRunning are explicitly set. We measured the time from app startup to the Loaded event on the main page, which captures the full handler connection and layout pipeline.

With per-key instrumentation, we observed 24,727 total mapper calls during handler connection for 500 controls (~49 per control). Of these:

  • 13,567 (55%) were for properties the developer never set (e.g., CharacterSpacing, TextColor, Rotation, Shadow)
  • ~5,500 were for ViewHandler base properties that already short-circuit via IsConnectingHandler() (e.g., TranslationX, Scale, AnchorX) — they enter the C# method, check a flag, and return, but still pay method dispatch cost
  • ~5,600 were for properties whose mapper ran, read the default value from BindableObject, and pushed it to the platform — where the platform view already had that same default

Proposed optimization

Skip mapper calls for properties that haven't been explicitly set, checked at the PropertyMapper.UpdateProperties() level:

  1. If the handler is in Connecting state (first-time setup)
  2. And the virtual view implements IPropertyDefaultChecker
  3. Then for each mapper key, call IsPropertySet(propertyName) to check if the property was set by the developer
  4. Skip the mapper call if the property is still at its BindableProperty.DefaultValue

This is a centralized, cross-platform approach — it works for all controls and all platforms automatically, without modifying individual mapper methods.

Prototype implementation (3 files, ~80 lines)

  • New internal IPropertyDefaultChecker interface with bool IsPropertySet(string propertyName)
  • BindableObject implements it using IsSet() + a per-Type static cache to resolve property names → BindableProperty instances
  • PropertyMapper.UpdateProperties() checks and skips during Connecting state

Branch with prototype: dev/simonr/skip-default-mappers

Benchmark results

Android (Samsung Galaxy, arm64, Release, 5 runs each)

Configuration Median startup-to-Loaded Mapper calls
Baseline 1,718 ms 24,727
Optimized 1,403 ms 11,160
Improvement 315 ms (18.3%) 13,567 skipped (55%)

Standard deviation across runs: ~27 ms — very consistent.

Mac Catalyst (macOS, arm64, Debug, 5 runs each)

Configuration Median startup-to-Loaded Mapper calls
Baseline 6,550 ms 24,342
Optimized 5,992 ms 10,740
Improvement 558 ms (8.5%) 13,602 skipped (56%)

The higher percentage improvement on Android (18.3% vs 8.5%) confirms that JNI interop overhead makes each skipped mapper call more impactful.

Note on the measurement: These are total app startup times (static initializer → Loaded event), not isolated SetVirtualView measurements. The mapper work is a fraction of the total, so the actual time spent in redundant mapper calls is a subset of the 315 ms / 558 ms improvements. The rest of the improvement comes from reduced GC pressure and avoided downstream work triggered by property setters.

Remaining opportunity

Instrumentation shows that even with this optimization, ~9,000 mapper calls still execute unnecessarily for 500 controls, because:

  1. Unresolved property names (~5,000 calls): Properties like Semantics, Border, Toolbar, IsInAccessibleTree don't have a matching BindableProperty field on the control type (they're computed properties, attached properties, or interface-only). The conservative fallback returns IsPropertySet = true to avoid breaking anything.

  2. Properties reported as "set" by styles (~4,000 calls): Properties like Visibility, IsEnabled, MinimumHeight, MaximumWidth have their BindableProperty.IsSet() return true because MAUI's implicit style system touches them — even though the developer never set them and the values match platform defaults.

Addressing these two categories could skip an additional ~9,000 calls per 500 controls, potentially doubling the improvement.

Next steps

This issue is filed to track the opportunity and share the data. The prototype is not immediately mergeable — it needs:

  1. More thorough testing — especially around controls with non-standard defaults and custom handlers
  2. Correctness validation — the assumption that freshly-created platform views have matching defaults must be verified across all controls and platforms
  3. Better property resolution — replace the reflection-based BindableProperty lookup with something that handles attached properties and computed properties
  4. Consideration of source generators — to eliminate the reflection cost entirely

cc @PureWeen @jonathanpeppers @albyrock87 @MartyIX

Activity

  1. added
    perf/generalThe issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf)
    on Feb 17, 2026
  2. albyrock87 commented on Feb 17, 2026

    @albyrock87
    Contributor

    @simonrozsival the approach seems good but I'm seeing a few critical points:

    • this may become a breaking change: a developer may have added (append mapper) a custom map function which is expected to run even when the property has the default value (this is the reason for which I acted at mapper level in Skip useless handler mappings calls while connecting #27259
    • some properties, like Visibility needs to be mapped even on the default value because they may have to set some additional flag on the native view
  3. simonrozsival commented on Feb 17, 2026

    @simonrozsival
    MemberAuthor

    I agree. I will need to spend more time investigating this. One direction we could explore is to allow skipping for some known "safe" properties. Custom mappers should not be affected.

  4. simonrozsival commented on Feb 17, 2026

    @simonrozsival
    MemberAuthor

    @albyrock87 Thanks for the feedback — both points are valid and important.

    On custom mappers (AppendToMapping/ModifyMapping):

    You're right that this prototype would silently skip user-customized mappers. If a developer does:

    LabelHandler.Mapper.AppendToMapping("Opacity", (handler, view) => {
        // custom initialization code
    });

    ...and the Label has default opacity, our optimization skips the entire "Opacity" mapper — including their appended code. There's no way to distinguish "original framework mapper" from "framework + user-appended" at the key level today.

    A possible solution: track which keys have been modified via AppendToMapping/PrependToMapping/ModifyMapping and never skip those. This could be a HashSet<string> on the PropertyMapper populated by ModifyMapping(). That way, the optimization only affects framework-defined mappers with no user customization.

    On Visibility and other "needs mapping at default" properties:

    Good point. MapVisibility on Android does more than just set View.Visibility — it may also set additional flags depending on the control type (e.g., ActivityIndicator).

    In the prototype, I started with a basic IsPropertySet() check that resolved mapper key names to BindableProperty instances via reflection. That version skipped ~55% of mapper calls. However, it missed many properties because the mapper key name often differs from the BindableProperty.PropertyName (e.g., mapper key "Visibility" vs property "IsVisible", or "MinimumHeight" vs "MinimumHeightRequest"), and attached properties (like SemanticProperties.Description) aren't on the control's type hierarchy. I then expanded the resolution with name aliases and attached-property scanning, which brought the skip rate to ~85% — but as you point out, some of those properties (like Visibility) genuinely need their mapper to run even at the default value.

    The safest approach may be the one you chose in #27259 — let individual mapper methods decide whether to skip, rather than making a blanket decision at the PropertyMapper level. The data from this experiment (which mappers are no-ops at defaults, how many calls there are, etc.) could help identify which additional mappers could benefit from IsConnectingHandler() guards beyond the ones you already added.

    I'll keep this issue open to track the investigation and data. The prototype is not meant to be merged as-is — it's an experiment to quantify the opportunity and find the right approach.

  5. albyrock87 commented on Feb 18, 2026

    @albyrock87
    Contributor

    @simonrozsival yes I believe my approach is the only one that fits all the needs.

    I also remembered another scenario.
    It seems it's not legitimate, but the code kind-of allows it, which is the possibility to switch virtual view on the same platform view.
    This is a scenario where setting the default value is actually needed to reset an eventual custom value.
    See https://github.com/dotnet/maui/blob/43db9d77f2ff59999dea36ab8befb8541e919013/src/Core/src/Core/Extensions/InternalElementExtensions.cs

    I think that those extension methods should be made public, and the property checker should be available at mapper level.
    This way even appended or overridden mappers could check the handler connection state and whether the property was set.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    perf/generalThe issue affects performance (runtime speed, memory usage, startup time, etc.) (sub: perf)

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions