Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
38 changes: 38 additions & 0 deletions benchmark.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
import Foundation

struct FlutterAnnotation {
var id: String
}

var annotations: [FlutterAnnotation] = []
for i in 0..<10000 {
annotations.append(FlutterAnnotation(id: "id_\(i)"))
}

// Ensure the random seed is reproducible for a fair comparison, though time based is ok
let startArray = Date()
var count = 0
for i in 0..<1000 {
let id = "id_\(i * 10)"
let found = annotations.filter { $0.id == id }.first
if found != nil { count += 1 }
}
let endArray = Date()

print("Array filter O(N) time: \(endArray.timeIntervalSince(startArray)) seconds for 1000 lookups in 10000 items")

var annotationsMap: [String: FlutterAnnotation] = [:]
for a in annotations {
annotationsMap[a.id] = a
}

let startDict = Date()
count = 0
for i in 0..<1000 {
let id = "id_\(i * 10)"
let found = annotationsMap[id]
if found != nil { count += 1 }
}
let endDict = Date()

print("Dict lookup O(1) time: \(endDict.timeIntervalSince(startDict)) seconds for 1000 lookups in 10000 items")
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,8 @@ extension MapKitViewHost {
}

func annotationsToChange(_ annotations: [PlatformAnnotation]) {
let oldAnnotations: [MKAnnotation] = self.mapView.annotations
for annotationData in annotations {
if let annotationToChange = oldAnnotations.first(where: { ($0 as? FlutterAnnotation)?.id == annotationData.id }) as? FlutterAnnotation {
if let annotationToChange = self.getAnnotation(with: annotationData.id) {
let newAnnotation = FlutterAnnotation(fromPlatform: annotationData)
if annotationToChange != newAnnotation {
if !annotationToChange.wasDragged {
Expand Down Expand Up @@ -137,6 +136,7 @@ extension MapKitViewHost {

private func removeAnnotation(id: String) {
if let flutterAnnotation: FlutterAnnotation = self.getAnnotation(with: id) {
self.annotationsById.removeValue(forKey: id)
self.mapView.removeAnnotation(flutterAnnotation)
}
}
Expand Down Expand Up @@ -178,7 +178,7 @@ extension MapKitViewHost {
#endif

private func getAnnotation(with id: String) -> FlutterAnnotation? {
return self.mapView.annotations.filter { annotation in return (annotation as? FlutterAnnotation)?.id == id }.first as? FlutterAnnotation
return self.annotationsById[id]
}

private func annotationExists(with id: String) -> Bool {
Expand All @@ -193,6 +193,7 @@ extension MapKitViewHost {
if self.annotationExists(with: annotation.id) {
self.removeAnnotation(id: annotation.id)
}
self.annotationsById[annotation.id] = annotation
self.mapView.addAnnotation(annotation)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ public class MapKitViewHost: NSObject, @preconcurrency MapKitHostApi {
var flutterApi: MapKitFlutterApi
var currentlySelectedAnnotation: String?
var tileOverlays: [String: FlutterTileOverlay] = [:]
var annotationsById: [String: FlutterAnnotation] = [:]

public init(withFrame frame: CGRect, withRegistrar registrar: FlutterPluginRegistrar, withId id: Int64) {
let suffix = "\(id)"
Expand Down
Loading