Skip to content

fix(android): apply ShapeSource data once, off the main thread - #4440

Merged
mfazekas merged 1 commit into
mainfrom
fix/android-shape-apply-once
Nov 6, 2026
Merged

mfazekas merged 1 commit into
mainfrom
fix/android-shape-apply-once

Conversation

@mfazekas

@mfazekas mfazekas commented Nov 6, 2026

Copy link
Copy Markdown
Contributor

Description

Fixes Android applying each ShapeSource shape twice: setShape called both setStyleSourceProperty on the main thread and GeoJsonSource.data(), which posts to the SDK's worker thread. Neither the data nor its order was under our control, and a later change to the source could be overwritten by the worker. #4439 needs that ordering.

shape and url now go through setStyleGeoJSONSourceData once, on one serial worker of ours. That includes the initial data in addToMap, which makeSource no longer passes to the builder. The SDK's GeoJSON calls have no main thread check, and its own worker makes the same one.

Measured with 50k points on release builds on emulator-5554, 12 updates spaced 12 s apart so they never overlap, comparing clean main and clean fix builds, with the reproducer below:

  1. CPU for the 12 updates, on the quietest host: 48.0 s on main, 25.0 s with the fix. The SDK's tile worker threads go from 37.3 s to 16.0 s, which is where the second apply shows.
  2. Time until a query sees the update: 3.9 s to 2.4 s.
  3. Main thread at setShape: up to 117 ms to under 2 ms, because passing the 5 MB string to the SDK left the main thread. At mount and style reload addToMap stays the same as main, 2-22 ms.

CPU is noisy on this shared host. Across all runs main was 48-54 s and the fix 23-86 s, and the 85.9 s run overlapped an Xcode build. The two quietest fix runs were 23.1 s and 25.0 s, so compare those. Applying on the main thread instead was also tried and rejected: it matched on CPU but took 33-200 ms of main thread at every mount and reload.

Live shape change, style reload, live url change, and existing sources with and without a shape pass on both builds.

Checklist

  • I've read CONTRIBUTING.md
  • I updated the doc/other generated code with running yarn generate in the root folder
  • I have tested the new feature on /example app.
    • In V11 mode/ios
    • In New Architecture mode/ios
    • In V11 mode/android
    • In New Architecture mode/android
  • I added/updated a sample - if a new feature was implemented (/example)

Android only, no iOS code changes. No sample, as this is a fix.

Screenshot OR Video

Not a visual change.

Component to reproduce the issue you're fixing

Run Measure and Checks on main and on this branch. CPU was read from /proc/<pid>/stat between the MARK log lines.

Reproducer
import React, { useLayoutEffect, useRef, useState } from 'react';
import { Button, ScrollView, Text, View } from 'react-native';
import { MapView, Camera, ShapeSource, CircleLayer } from '@rnmapbox/maps';

const FEATURE_COUNT = 50000;
const ITERATIONS = 12;
const APPLIED_ROUNDS = 5;
const CADENCE_MS = 12000;
const SOURCE_ID = 'apply-once';
const EARTHQUAKES =
  'https://docs.mapbox.com/mapbox-gl-js/assets/earthquakes.geojson';

const feature = (id, x, y, v) => ({
  type: 'Feature',
  id,
  properties: { v },
  geometry: { type: 'Point', coordinates: [x, y] },
});
const large = (v) => ({
  type: 'FeatureCollection',
  features: Array.from({ length: FEATURE_COUNT }, (_, i) =>
    feature(
      `f${i}`,
      -10 + (i % 250) * 0.08,
      -10 + Math.floor(i / 250) * 0.08,
      i === 0 ? v : 0,
    ),
  ),
});

const style = (sources, color) =>
  JSON.stringify({
    version: 8,
    sources,
    layers: [
      { id: 'bg', type: 'background', paint: { 'background-color': color } },
    ],
  });
const STYLE = style({}, '#eeeeee');
const existingData = (v) => ({
  type: 'geojson',
  data: { type: 'FeatureCollection', features: [feature('s1', 0, 0, v)] },
});
const STYLE_EXISTING = style({ [SOURCE_ID]: existingData(1) }, '#eeeeee');

const sleep = (ms) => new Promise((resolve) => setTimeout(resolve, ms));
const p50 = (values) =>
  Math.round([...values].sort((a, b) => a - b)[values.length >> 1]);

export default function ShapeApplyOnce() {
  const [styleJSON, setStyleJSON] = useState(STYLE);
  const [source, setSource] = useState(null);
  const [lines, setLines] = useState([]);
  const mapRef = useRef(null);
  const commit = useRef(null);
  const idle = useRef(null);

  useLayoutEffect(() => {
    commit.current?.();
    commit.current = null;
  });

  const log = (line) => {
    console.log(`BENCH ${line}`);
    setLines((prev) => [line, ...prev].slice(0, 30));
  };
  const nextIdle = () =>
    new Promise((resolve) => {
      const timer = setTimeout(resolve, 20000);
      idle.current = () => {
        clearTimeout(timer);
        resolve();
      };
    });
  const query = (filter = []) =>
    mapRef.current.querySourceFeatures(SOURCE_ID, filter);

  // Updates are spaced out so they never overlap, and the CPU window holds no queries.
  const measure = async () => {
    const waiting = nextIdle();
    setSource({ key: 'bench', shape: large(0) });
    await waiting;
    await sleep(8000);
    log('MARK start shape');
    await sleep(1000);
    const js = [];
    for (let v = 1; v <= ITERATIONS; v += 1) {
      const next = large(v);
      const t0 = performance.now();
      const committed = new Promise((resolve) => {
        commit.current = resolve;
      });
      setSource({ key: 'bench', shape: next });
      await committed;
      js.push(performance.now() - t0);
      await sleep(CADENCE_MS);
    }
    log('MARK end shape');

    const applied = [];
    for (let v = ITERATIONS + 1; v <= ITERATIONS + APPLIED_ROUNDS; v += 1) {
      const next = large(v);
      const t0 = performance.now();
      setSource({ key: 'bench', shape: next });
      for (;;) {
        const found = await query(['==', ['id'], 'f0']);
        if (found.features.some((f) => f.properties?.v === v)) break;
        await sleep(100);
      }
      applied.push(performance.now() - t0);
      await sleep(CADENCE_MS);
    }
    log(
      `RESULT ${JSON.stringify({
        updates: ITERATIONS,
        js: p50(js),
        applied: p50(applied),
        appliedAll: applied.map(Math.round),
      })}`,
    );
  };

  const liveUrl = async () => {
    setSource({ key: 'url', url: EARTHQUAKES });
    await sleep(4000);
    const before = await query();
    setSource({ key: 'url', url: `${EARTHQUAKES}?changed=1` });
    await sleep(4000);
    const after = await query();
    log(
      `${before.features.length > 0 && after.features.length > 0 ? 'PASS' : 'FAIL'} live url change ${before.features.length} -> ${after.features.length}`,
    );
  };

  const liveShape = async () => {
    setSource({
      key: 'shape',
      shape: { type: 'FeatureCollection', features: [feature('a', 0, 0, 1)] },
    });
    await sleep(1500);
    setSource({
      key: 'shape',
      shape: { type: 'FeatureCollection', features: [feature('a', 0, 0, 2)] },
    });
    await sleep(1500);
    const found = await query();
    log(
      `${found.features[0]?.properties?.v === 2 ? 'PASS' : 'FAIL'} live shape change v=${found.features[0]?.properties?.v}`,
    );
  };

  const styleReload = async () => {
    setSource({
      key: 'reload',
      shape: { type: 'FeatureCollection', features: [feature('a', 0, 0, 3)] },
    });
    await sleep(1500);
    setStyleJSON(style({}, '#ddeeff'));
    await sleep(2500);
    const found = await query();
    log(
      `${found.features[0]?.properties?.v === 3 ? 'PASS' : 'FAIL'} style reload v=${found.features[0]?.properties?.v}`,
    );
    setStyleJSON(STYLE);
    await sleep(2000);
  };

  const existing = async () => {
    setSource(null);
    await sleep(300);
    setStyleJSON(STYLE_EXISTING);
    await sleep(2000);
    setSource({ key: 'existing', existing: true });
    await sleep(1500);
    const kept = await query();
    log(
      `${kept.features.length === 1 && kept.features[0]?.properties?.v === 1 ? 'PASS' : 'FAIL'} existing keeps the data of the style n=${kept.features.length}`,
    );
    setSource({
      key: 'existing',
      existing: true,
      shape: { type: 'FeatureCollection', features: [feature('s1', 0, 0, 2)] },
    });
    await sleep(1500);
    const changed = await query();
    log(
      `${changed.features[0]?.properties?.v === 2 ? 'PASS' : 'FAIL'} existing takes a new shape v=${changed.features[0]?.properties?.v}`,
    );
    setSource(null);
    setStyleJSON(STYLE);
    await sleep(1500);
  };

  const mountAndReload = async () => {
    for (let round = 1; round <= 4; round += 1) {
      setSource(null);
      await sleep(1500);
      setSource({ key: `mount${round}`, shape: large(round) });
      await sleep(5000);
      setStyleJSON(style({}, '#ddeeff'));
      await sleep(5000);
      setStyleJSON(STYLE);
      await sleep(5000);
      setSource({ key: `mount${round}`, shape: large(round + 10) });
      await sleep(5000);
    }
    setSource(null);
    log('MARK mount done');
  };

  const checks = async () => {
    await liveShape();
    await styleReload();
    await liveUrl();
    await existing();
    log('MARK checks done');
  };

  return (
    <View style={{ flex: 1 }}>
      <MapView
        ref={mapRef}
        style={{ flex: 1 }}
        styleJSON={styleJSON}
        onMapIdle={() => {
          idle.current?.();
          idle.current = null;
        }}
      >
        <Camera defaultSettings={{ centerCoordinate: [0, -1], zoomLevel: 4 }} />
        {source ? (
          <ShapeSource {...source} id={SOURCE_ID}>
            <CircleLayer id="apply-once-circles" style={{ circleRadius: 3 }} />
          </ShapeSource>
        ) : null}
      </MapView>
      <View style={{ padding: 8, height: 220 }}>
        <Button title="Measure" onPress={measure} />
        <Button title="Checks" onPress={checks} />
        <Button title="Mount" onPress={mountAndReload} />
        <ScrollView>
          {lines.map((line, i) => (
            <Text key={i} style={{ fontSize: 10 }}>
              {line}
            </Text>
          ))}
        </ScrollView>
      </View>
    </View>
  );
}

Every shape was applied twice, through setStyleSourceProperty on the main
thread and GeoJsonSource.data() on a worker. shape and url now go through
setStyleGeoJSONSourceData once, on a serial worker of our own, so later work
on the source can keep its order after them.
@mfazekas
mfazekas marked this pull request as ready for review November 6, 2026 15:20
@mfazekas
mfazekas merged commit 3262497 into main Nov 6, 2026
10 checks passed
@mfazekas
mfazekas deleted the fix/android-shape-apply-once branch November 6, 2026 15:22

This branch was previously deployed

1 inactive deployment
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.

1 participant