Making a Fuel-Price Map Fast on Android and iOS

Image of the author

Germán Bustamante @germanbustamante

Fuelvo's price map on Android, with stations shown as price pins coloured by how cheap they are for the area

Making a Fuel-Price Map Fast on Android and iOS

The map is the screen of Fuelvo that people judge first. It shows up to 200 stations at a time, each one a pin with its price, coloured by how cheap it is for the area, and grouped into clusters when they would cover each other. It worked from the first release, but it didn’t feel fast: panning was heavy on Android, and on both platforms the map did more work than the user could see.

Three tickets in October fixed it: FUEA-110 for how the map loads data, FUEA-111 for how Android draws pins and FUEA-112 for how iOS does. This post covers what the problem was, what Google and Apple’s tools expect you to do, what I changed and what the measurements can and cannot show.

The shape of the problem

Every time the camera stopped moving, three things happened:

  1. A request. The shared MapViewModel asked the backend for the stations in the visible bounding box, wrote the answer to the local cache and replaced the markers. That was true even after a 50-metre pan inside an area it had just loaded.
  2. New pictures on Android. Each pin and each cluster was a Composable. The maps library built a view for each one on the main thread and captured it to a bitmap. A single zoom out and back in created between 156 and 267 of those views.
  3. A diff on every state change on iOS. MapKit’s updateUIView runs on every change of the screen’s state, including the loading bar, the freshness label and the preview card, and each time it rebuilt and compared all 200 annotations.

The first problem lives in shared Kotlin code, so one fix covers both apps. The other two are platform-specific, and each needed a different fix.

1. Load more than you show, and ask again only when needed

Google’s optimisation guide for Maps is written for the JavaScript API, but one rule applies everywhere: only add or remove content once the user stops panning or zooming. Fuelvo already did that (a 350 ms debounce, and a newer viewport cancels the older request). The problem was what happened after the camera stopped: every stop was treated as new.

The fix is in MapViewModel, in :core:presentation, so Android and iOS got it in the same commit:

The loaded area is the view plus half its span on each side; a pan inside it needs no request

One detail is easy to miss. The pin with the cheapest price on screen gets a star, and that has to follow the camera, not the loaded area. So withCheapestIn(viewport) recomputes it over what’s visible, and the price colours still come from the backend’s scale for the area (the app never ranks prices against whatever happens to be on screen).

MapViewModelTest covers the sequences: ten pans after a margin load make no request, and another fuel, a cut answer or an old answer each trigger a new one.

2. Android: a picture per content, not a view per pin

What was slow

Fuelvo’s Android map uses android-maps-compose and its Clustering composable. Its clusterItemContent and clusterContent slots are convenient: you write a Composable and it becomes the marker. But for each item the library creates a view, measures it, lays it out, draws it and captures the result as a BitmapDescriptor, on the main thread, even when many pins look exactly the same.

The library’s maintainers know about this. Issue #272, open since February 2023, asks for a cache for those bitmaps. The reporter had 3,000 points but only about 20 different markers. It is still open, and the maintainers would rather find an optimisation that doesn’t need a cache key.

There was a second problem. The selected station was part of each cluster item, so tapping a pin gave the library a new list, and it clustered the whole map again.

What Google’s own tooling expects

Under Compose, the clustering is the Maps SDK Utility Library: a ClusterManager, an algorithm and a DefaultClusterRenderer, which the documentation describes as “a good base to start from” for custom markers by subclassing it. So the fix stays within what Google documents: use the library’s renderer and give it bitmaps instead of views. The JavaScript optimisation guide gives the same advice: plain raster images for markers, and clustering when there are many.

What I built

Before: cluster item, Compose slot, a new view per pin, a bitmap captured on the main thread. After: the content is a key into a cache, and a Canvas painter draws only on a miss

MapPinScreenshotTest is the golden for the pins (Roborazzi), because no screen-level screenshot shows them.

The numbers, and what they can’t show

I measured with a Macrobenchmark (MapBenchmark) on the release build against production: panning at city zoom, zooming out to the widest view and back, and panning out there. The figures below are for the zoom route, on an emulator:

BeforeAfter
Compose views created for pins156–2670
Main-thread time making pins289–533 ms134–216 ms
Frames over 16.7 msabout the sameabout the same

The first two rows are the work the app no longer does: no views, and roughly half the main-thread time spent producing pins. The third row looks the same before and after, and the reason matters. On a 2-core emulator the slow frames come from the emulator’s GPU pipe (eglSwapBuffers), not from the app, so frame times can’t show this improvement. A run on a real low-end phone is still on my list, and I won’t quote a frame-time gain until I’ve done it.

3. iOS: MapKit was right, the code around it wasn’t

MapKit already does the two things a map with many markers needs: annotation views are reused (dequeueReusableAnnotationView), and clustering is built in through clusteringIdentifier. The waste was in my code around it:

On every updateUIView, MapAnnotationDiff returns nil when the markers are the same list; otherwise it splits them into added, removed and updated, and updated pins only get a new image

On iOS I haven’t measured frame times yet either: the simulator has the same limits as the emulator. The changes are counted by tests instead (MapPinTests), and the next step is an Instruments run on a physical iPhone with the Animation Hitches template.

What carried over between the platforms

The same idea solved the problem on both platforms: a pin is a picture of its content, and the content is the cache key. Everything else follows from that. Clustering libraries can keep their markers, selection changes two pictures instead of the whole data set, and the work scales with the number of different pins, not the number of stations.

What I’d tell another team doing this: