Repository navigation
[Android] Map: Stop reloading a pin's icon on every cluster pass #37774
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -48,8 +48,8 @@ public partial class MapHandler : ViewHandler<IMap, MapView> | |
| bool _isClusteringEnabled; | ||
| List<MapCluster>? _clusters; | ||
| Dictionary<string, MapCluster>? _clusterMarkers; | ||
| const int MaxClusterIconCacheSize = 64; | ||
| readonly ClusterIconCache<BitmapDescriptor> _clusterIconCache = new(MaxClusterIconCacheSize); | ||
| const int MaxIconCacheSize = 64; | ||
| readonly IconCache<BitmapDescriptor> _iconCache = new(MaxIconCacheSize); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[moderate] Performance-Critical Path — The 64-entry LRU is now shared between cluster icons and every un-clustered pin icon, but the capacity was not revisited. On a map with more than 64 distinct pin |
||
| WeakReference<IMap>? _clusterImageOwner; | ||
| int _clusterImageVersion = int.MinValue; | ||
|
|
||
|
|
@@ -135,7 +135,7 @@ protected override void DisconnectHandler(MapView platformView) | |
| _circles = null; | ||
| _clusters?.Clear(); | ||
| _clusterMarkers?.Clear(); | ||
| _clusterIconCache.Clear(); | ||
| _iconCache.Clear(); | ||
| _clusterImageOwner = null; | ||
| _clusterImageVersion = int.MinValue; | ||
| _init = true; | ||
|
|
@@ -414,7 +414,7 @@ void UpdateClusterImageVersion(IMap map) | |
|
|
||
| _clusterImageOwner = new WeakReference<IMap>(map); | ||
| _clusterImageVersion = version; | ||
| _clusterIconCache.Clear(); | ||
| _iconCache.Clear(); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[minor] Performance-Critical Path — This |
||
| } | ||
|
|
||
| /// <summary> | ||
|
|
@@ -907,11 +907,27 @@ async Task AddPinAsync(IMapPin pin, CancellationToken ct) | |
|
|
||
| var markerOptions = iMapPinHandler.PlatformView; | ||
|
|
||
| // Load custom image if specified | ||
| if (pin.ImageSource != null) | ||
| // Captured before the await: if ImageSource changes while the load is running, the write | ||
| // below has to be dropped rather than publish a marker with the older icon. | ||
| var requestedSource = pin.ImageSource; | ||
|
|
||
| if (requestedSource is null) | ||
| { | ||
| // The MarkerOptions are reused, so a cleared ImageSource has to take the previous | ||
| // icon off them - null is Android's "use the default marker". | ||
| markerOptions.SetIcon(null); | ||
| } | ||
| else | ||
| { | ||
| var icon = await LoadPinIconAsync(pin.ImageSource, MauiContext, ct); | ||
| if (icon != null) | ||
| // Clustering re-runs AddPins on every zoom change, so without the cache the icon of | ||
| // every un-clustered pin is decoded and rescaled again on each pass. | ||
| var icon = await GetIconAsync(requestedSource, MauiContext, ct); | ||
|
|
||
| // Re-checked after the await: UpdatePinImageSourceAsync can have applied a newer | ||
| // source in the meantime, and it guards its own write the same way. Without this | ||
| // the two writers race and the marker can be published with the older icon while | ||
| // the change notification that would fix it has already been consumed. | ||
| if (icon != null && ReferenceEquals(pin.ImageSource, requestedSource)) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[moderate] Logic and Correctness — The post-await guard drops the icon write, but the comment's assumption that |
||
| markerOptions.SetIcon(icon); | ||
| } | ||
|
|
||
|
|
@@ -955,9 +971,35 @@ async Task UpdatePinImageSourceAsync(IMapPin pin, Marker marker, CancellationTok | |
| if (!ct.IsCancellationRequested && ReferenceEquals(pin.ImageSource, requestedSource) && (pin.MarkerId as string) == marker.Id) | ||
| { | ||
| marker.SetIcon(icon); | ||
|
|
||
| // The MarkerOptions are reused the next time this pin is added, so update them too - | ||
| // otherwise the next cluster pass would restore the previous icon. Reached through | ||
| // the non-throwing accessor: this resumes after an await, and the typed PlatformView | ||
| // throws once the handler has been disconnected. | ||
| if (pin.Handler is MapPinHandler mapPinHandler && | ||
| ((IElementHandler)mapPinHandler).PlatformView is MarkerOptions handlerOptions) | ||
| { | ||
| handlerOptions.SetIcon(icon); | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[minor] Complexity Reduction — This new write pushes the descriptor produced by the direct |
||
| } | ||
| } | ||
| } | ||
|
|
||
| // Shared by pin and cluster markers: one decoded descriptor backs every marker naming the same | ||
| // image, so a recluster pass reuses it instead of decoding per marker. GetIconCacheKey returns | ||
| // null for a source that can't be keyed stably, or that opted out of caching - those load fresh | ||
| // every time, which is also why they keep this caller's ct: nobody else is waiting on them. | ||
| // A keyable load is shared, so it must not be cancelled by whichever caller happened to start it. | ||
| Task<BitmapDescriptor?> GetIconAsync(IImageSource imageSource, IMauiContext mauiContext, CancellationToken ct) | ||
|
Collaborator
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
[minor] Async and Threading Safety — For any keyable source the load is started with |
||
| { | ||
| var cacheKey = GetIconCacheKey(imageSource); | ||
| var loadToken = cacheKey is null ? ct : CancellationToken.None; | ||
|
|
||
| return _iconCache.GetOrCreateAsync( | ||
| cacheKey, | ||
| () => LoadPinIconAsync(imageSource, mauiContext, loadToken), | ||
| () => GetIconCacheExpiry(imageSource)); | ||
| } | ||
|
|
||
| // Loads imageSource into a BitmapDescriptor | ||
| // Returns null on cancel/failure/no drawable. | ||
| static async Task<BitmapDescriptor?> LoadPinIconAsync(IImageSource imageSource, IMauiContext mauiContext, CancellationToken ct) | ||
|
|
@@ -1009,14 +1051,7 @@ async Task AddClusterMarkerAsync(MapCluster cluster, CancellationToken ct) | |
|
|
||
| BitmapDescriptor? icon = null; | ||
| if (image != null) | ||
| { | ||
| var cacheKey = GetClusterIconCacheKey(image); | ||
| var loadToken = cacheKey is null ? ct : CancellationToken.None; | ||
| icon = await _clusterIconCache.GetOrCreateAsync( | ||
| cacheKey, | ||
| () => LoadPinIconAsync(image, mauiContext, loadToken), | ||
| () => GetClusterIconCacheExpiry(image)); | ||
| } | ||
| icon = await GetIconAsync(image, mauiContext, ct); | ||
|
|
||
| if (ct.IsCancellationRequested || !ReferenceEquals(Map, map)) | ||
| return; | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
[moderate] Regression Prevention and Test Coverage — The two added tests exercise only the
IconCache<T>primitive (keyed reuse, null-key bypass), which already existed and is not where the reported bug lives. None of the actual behavior changes in this PR are covered: the reusedMarkerOptionsbeing cleared whenImageSourcebecomes null,UpdatePinImageSourceAsyncwriting back to the handler'sMarkerOptionsso a later cluster pass does not restore the previous icon, and the post-await stale-source guard inAddPinAsync. Those are the changes that could regress #37773, and they only fail-without-fix here because of the type rename, not because of behavior. A device/UI test on Android that sets a pinImageSource, forces a zoom-driven recluster, and asserts the icon survives (and that clearingImageSourcereverts to the default marker) would give this fix real regression protection.