From 15cbfc222cb009b00c490657cafc89fceb0c03f1 Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Mon, 10 Aug 2026 16:39:49 +0000 Subject: [PATCH] perf: Batch overlay removal to improve performance Replaced the O(N*M) loop removal of overlays with an O(M) batched removal approach using `removeOverlays`. - Collects all removed overlays into a single array. - Uses MKMapView.removeOverlays to efficiently bulk remove the collected overlays. Co-authored-by: esenmx <43244505+esenmx@users.noreply.github.com> --- .../mapkit_flutter/MapView/MapKitViewHost.swift | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/darwin/mapkit_flutter/Sources/mapkit_flutter/MapView/MapKitViewHost.swift b/darwin/mapkit_flutter/Sources/mapkit_flutter/MapView/MapKitViewHost.swift index f016099..cd07a43 100644 --- a/darwin/mapkit_flutter/Sources/mapkit_flutter/MapView/MapKitViewHost.swift +++ b/darwin/mapkit_flutter/Sources/mapkit_flutter/MapView/MapKitViewHost.swift @@ -213,15 +213,23 @@ public class MapKitViewHost: NSObject, @preconcurrency MapKitHostApi { } private func applyOverlayUpdate(adding: [any FlutterOverlay], changing: [any FlutterOverlay], removing: [String]) { + var overlaysToRemove: [MKOverlay] = [] for id in removing { if let overlay = self.overlaysById.removeValue(forKey: id) { - self.mapView.removeOverlay(overlay) + overlaysToRemove.append(overlay) } } for new in changing { if let old = self.overlaysById[new.id] { - self.mapView.removeOverlay(old) + overlaysToRemove.append(old) } + } + + if !overlaysToRemove.isEmpty { + self.mapView.removeOverlays(overlaysToRemove) + } + + for new in changing { self.addOverlay(new) } for new in adding {