perf: cache bundled turn lane manifest

This commit is contained in:
que01 committed 2026-09-14 13:48:27 +08:00
1 parent 78c357e445
commit 82846cdebc
3 files changed
+25 -1

No files matched your search

+16 -1
View File
@@ -5,6 +5,7 @@ const path = require('path');
const { laneCenterline } = require('../geometry/lane-geometry');
const ASSET_MANIFEST = path.resolve(__dirname, '..', '..', 'assets', 'lane-icons', 'manifest.json');
let defaultManifestCache = null;
const LANE_WIDTH_METERS = 3.2;
const PLACEMENT_DISTANCE_METERS = 9;
const SPATIAL_MATCH_MAX_DISTANCE_METERS = 18;
@@ -15,7 +16,21 @@ const SPATIAL_MATCH_MIN_ALIGNMENT = Math.cos(Math.PI / 6);
const SVG_METERS_PER_UNIT = 0.1;
function loadManifest(file = ASSET_MANIFEST) {
const manifest = JSON.parse(fs.readFileSync(file, 'utf8'));
// The bundled manifest is immutable during normal compilation, so avoid
// reparsing it for every road preview. Keep custom paths uncached because
// callers may use them as a live test/config input.
let manifest;
if (file === ASSET_MANIFEST) {
const stat = fs.statSync(file);
if (
defaultManifestCache &&
defaultManifestCache.mtimeMs === stat.mtimeMs &&
defaultManifestCache.size === stat.size
)
return defaultManifestCache.manifest;
manifest = JSON.parse(fs.readFileSync(file, 'utf8'));
defaultManifestCache = { manifest, mtimeMs: stat.mtimeMs, size: stat.size };
} else manifest = JSON.parse(fs.readFileSync(file, 'utf8'));
if (!Array.isArray(manifest.assets)) throw new Error('turn-lane asset manifest has no assets array');
return manifest;
}