Skip to content

Turn any idea, plan, or codebase into a beautiful interactive diagram. An agent skill for Claude Code, Codex, and more.

perf(renderers): measure each text width and label rectangle once

goatliamia11c572886e6c
diff --git a/archify/renderers/shared/utils.mjs b/archify/renderers/shared/utils.mjs
index c14094ac..49678023 100644
--- a/archify/renderers/shared/utils.mjs
+++ b/archify/renderers/shared/utils.mjs
@@ -226,8 +226,15 @@ const VARIATION_SELECTOR_FIRST = 0xfe00;
const VARIATION_SELECTOR_LAST = 0xfe0f;
const VARIATION_SELECTOR_EMOJI = 0xfe0f;
+// Width measurement is pure and the same labels are measured many times per
+// compile, so the unit count is memoized by its input string.
+const TEXT_UNITS_CACHE = new Map();
+
export function textUnits(text) {
- const chars = Array.from(String(text ?? ''));
+ const cacheKey = String(text ?? '');
+ const cachedUnits = TEXT_UNITS_CACHE.get(cacheKey);
+ if (cachedUnits !== undefined) return cachedUnits;
+ const chars = Array.from(cacheKey);
let units = 0;
for (let i = 0; i < chars.length; i += 1) {
const codePoint = chars[i].codePointAt(0);
@@ -236,5 +243,6 @@ export function textUnits(text) {
if (next === VARIATION_SELECTOR_EMOJI) units += 2;
else units += FULLWIDTH_RE.test(chars[i]) ? 2 : 1;
}
+ TEXT_UNITS_CACHE.set(cacheKey, units);
return units;
}
diff --git a/archify/renderers/workflow/workflow-compiler.mjs b/archify/renderers/workflow/workflow-compiler.mjs
index d5c4684e..1f80ccd7 100644
--- a/archify/renderers/workflow/workflow-compiler.mjs
+++ b/archify/renderers/workflow/workflow-compiler.mjs
@@ -4379,11 +4379,18 @@ function pathFor(edge) {
return routed;
}
+// The label rectangle follows from the routed path, which is fixed once it is
+// cached; keying on that object means a re-routed edge gets a fresh rectangle.
+const LABEL_RECT_CACHE = new WeakMap();
+
function labelRectFor(edge, relationIndex) {
if (!edge.label || !nodes.has(edge.from) || !nodes.has(edge.to)) return null;
- const [lx, ly] = workflowEdgeLabelPoint(edge, pathFor(edge).points);
+ const routed = pathFor(edge);
+ const cached = LABEL_RECT_CACHE.get(routed);
+ if (cached) return cached;
+ const [lx, ly] = workflowEdgeLabelPoint(edge, routed.points);
const width = workflowLabelWidth(edge.label);
- return {
+ const rect = {
relation: edge,
relationIndex,
label: edge.label,
@@ -4394,6 +4401,8 @@ function labelRectFor(edge, relationIndex) {
lx,
ly,
};
+ LABEL_RECT_CACHE.set(routed, rect);
+ return rect;
}
function measuredContentBounds() {