From 82846cdebc5ee7d212e27ba5af1dbb5def7eb7fe Mon Sep 17 00:00:00 2001 From: que01 Date: Mon, 14 Sep 2026 13:48:27 +0800 Subject: [PATCH] perf: cache bundled turn lane manifest --- .../implement.md | 4 ++++ src/compile/turn-lane-arrows.js | 17 ++++++++++++++++- test/index.js | 5 +++++ 3 files changed, 25 insertions(+), 1 deletion(-) diff --git a/.trellis/tasks/09-08-junction-reference-fidelity/implement.md b/.trellis/tasks/09-08-junction-reference-fidelity/implement.md index 3888067..00334ae 100644 --- a/.trellis/tasks/09-08-junction-reference-fidelity/implement.md +++ b/.trellis/tasks/09-08-junction-reference-fidelity/implement.md @@ -192,3 +192,7 @@ npm test 中代码、投影、边界反例、设施、编辑与工作台导入 ## 2026-09-14 现有设施联动收口 现有设施范围的 A6 已由 `test/index.js`、`test/workbench-edit-api.js` 和 `test/junction-facility-composition.js` 覆盖:无 crossing 来源时复杂斑马线省略并发布 `complex-junction-crossing-source-missing`,停止线保留 `unknown-no-crossing-source`;匹配 OSM crossing 节点时发布带来源证据的斑马线;已知交通岛作为排除区域保留并纳入结构投影;角部设施缺少有效道路/步行连接时不伪造。导流岛自由编辑、新右转通道和全套过街设施编辑器仍按 PRD 留在后续任务。 + +## 2026-09-14 内置箭头资源清单缓存 + +预览编译重复读取固定的 `assets/lane-icons/manifest.json`。`loadManifest()` 现在只缓存内置清单,并以文件 `mtimeMs` 和大小失效;自定义清单路径保持每次读取,避免改变调用方的动态配置语义。默认清单缓存身份回归已加入 `test/index.js`。真实预览输出未改变;本轮样本总耗时受几何计算抖动影响,不能单独宣称可测的额外百分比收益。 diff --git a/src/compile/turn-lane-arrows.js b/src/compile/turn-lane-arrows.js index ab021f5..f6c8cc7 100644 --- a/src/compile/turn-lane-arrows.js +++ b/src/compile/turn-lane-arrows.js @@ -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; } diff --git a/test/index.js b/test/index.js index 0df76a0..b6a7816 100644 --- a/test/index.js +++ b/test/index.js @@ -8,6 +8,11 @@ const compiler = require('../src'); assert.equal(typeof compiler.laneGeometry.laneCenterline, 'function'); assert.equal(typeof compiler.gaodeReference.convertGeoJson, 'function'); assert.equal(typeof compiler.turnLaneArrows.buildCustomTurnLaneArrows, 'function'); +assert.strictEqual( + compiler.turnLaneArrows.loadManifest(), + compiler.turnLaneArrows.loadManifest(), + 'the bundled turn-lane manifest is reused between compiles', +); assert.equal(typeof compiler.complexJunction.buildComplexJunctionGeometry, 'function'); assert.equal(typeof compiler.complexJunction.classifyJunctionShape, 'function'); assert.equal(typeof compiler.junctionStructure.buildJunctionStructure, 'function');