Skip to content

Conversation

Copy link
Contributor

Copilot AI commented Nov 7, 2025

Note

Are you waiting for the changes in this PR to be merged?
It would be very helpful if you could test the resulting artifacts from this PR and let us know in a comment if this change resolves your issue. Thank you!

Description of Change

Addresses performance concern from #32278 (comment).

The FindListenerForView method had O(n*m) complexity due to nested iteration:

  • Outer loop: walk view hierarchy (n levels, typically 10-20+)
  • Inner loop: linear search through registered views (m entries)
  • Runs on every layout operation

Changes:

  • Replace List<ViewEntry> with ConditionalWeakTable<AView, MauiWindowInsetListener>
  • Remove ViewEntry record and manual weak reference management
  • Simplify RegisterView, UnregisterView, and FindListenerForView to use dictionary operations

Performance impact:

  • Complexity: O(n*m) → O(n)
  • Dictionary lookup is O(1) vs linear search O(m)
  • Automatic GC-based cleanup vs manual cleanup during lookups
  • Thread-safe by default

Code change:

// Before: nested loops with manual cleanup
for (int i = _registeredViews.Count - 1; i >= 0; i--)
{
    var entry = _registeredViews[i];
    if (!entry.View.TryGetTarget(out var registeredView))
        _registeredViews.RemoveAt(i);
    else if (ReferenceEquals(registeredView, parentView))
        return entry.Listener;
}

// After: direct lookup
if (_registeredViews.TryGetValue(parentView, out var listener))
    return listener;

Net: -36 lines, eliminates hot path overhead in deep view hierarchies.

Issues Fixed

Implements optimization suggested in #32278 (comment 3504288138)

Warning

Firewall rules blocked me from connecting to one or more addresses (expand for details)

I tried to connect to the following addresses, but was blocked by firewall rules:

  • https://api.github.com/repos/dotnet/maui/issues/32278/comments
    • Triggering command: curl -s REDACTED (http block)
  • https://api.github.com/repos/dotnet/maui/issues/comments/3504288138
    • Triggering command: curl -s REDACTED (http block)

If you need me to access, download, or install something from one of these locations, you can either:

Original prompt

Can you create a PR based on the comments made here #32278 (comment)

Thoroughly analyze these comments and everywhere you agree with them please apply changes in a new PR


💬 We'd love your input! Share your thoughts on Copilot coding agent in our 2 minute survey.

Copilot AI changed the title [WIP] Update based on review comments from PR 32278 [Android] Optimize MauiWindowInsetListener registry with ConditionalWeakTable Nov 7, 2025
Copilot finished work on behalf of PureWeen November 7, 2025 20:19
Copilot AI requested a review from PureWeen November 7, 2025 20:20
@PureWeen
Copy link
Member

PureWeen commented Nov 7, 2025

/azp run

@azure-pipelines
Copy link

Azure Pipelines successfully started running 3 pipeline(s).

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

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants