Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 5 additions & 0 deletions .jules/bolt.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,11 @@
**Learning:** `renderWorldSurface` in `pkgs/id/web/src/world-surface.ts` validated every cell of a 120x50 world frame with `Intl.Segmenter.segment()` plus the unicode-width regexes (`\p{Mn}`, `\p{Grapheme_Extend}`) β€” ~120k segmentations per second at 20 frames/s. On the ASCII path this is pure overhead: a single code unit in U+0020..U+007E is always exactly one grapheme and one column wide. Measured in Bun/JSC, per-frame validation + colour CPU for a full 6000-cell frame fell from 60–169 ms to 2–3 ms (30–55x). Full-render timings in happy-dom are useless for judging this: element creation dominates (hundreds of ms with 4x run-to-run noise) and is unchanged by the fix.
**Action:** In hot render loops, fingerprint the common value class (ASCII / single code unit) and take a constant-time fast path before calling Intl or unicode-regex machinery; keep the general path for everything else. Benchmark the CPU component with `document.createElement` stubbed, not happy-dom's DOM.

## 2026-11-15 - [Single-scalar fast path and O(log n) range lookups in world-surface]

**Learning:** The earlier ASCII fast path left three range tables (`WIDE_RANGES`, `TEXT_PRESENTATION_RANGES`, `EMOJI_PRESENTATION_SEQ_RANGES`) scanned linearly via `.some()` β€” ~1.5-2 Β΅s per lookup β€” and left every non-ASCII single scalar (box-drawing borders, accented Latin, CJK, emoji) on the Intl.Segmenter path. A full 120x50 box-drawing frame therefore took ~52 ms instead of ~2 ms. Two invariants make the fix safe: every single Unicode scalar value is exactly one grapheme cluster (verified exhaustively over U+0000..U+10FFFF against Intl.Segmenter), and all three range tables are sorted and disjoint (verified by construction). The single exception is U+200D, whose graphemeWidth shortcut returns 1 while codePointWidth returns 0, so it must stay on the general path. Flattening the tables into `Int32Array` and binary-searching removes the per-comparison array destructuring that made `.some()` so slow.
**Action:** When a range table is sorted and queried per hot cell, flatten it to `Int32Array` and binary-search it; extend single-code-unit fast paths to all single scalars (not just ASCII), excluding only the code points whose width semantics diverge between the fast and general paths. Verify equivalence exhaustively over the full code-point space before shipping.

## 2026-11-16 - [Client-keyed plain objects silently drop `__proto__`]
**Learning:** Count maps keyed by client strings (`probe.foreignSeen[r] = ...` in `pm-stack/aggregator/src/push.ts`) never record a `"__proto__"` key: assignment hits the prototype setter instead of creating an own property, so the count silently vanishes. The sibling `mergeClasses` in `alerts.ts` takes arbitrary client keys the same way and has the same latent trap.
**Action:** When bounding/capping client-keyed `Record<string, number>` maps, explicitly skip `__proto__` (and gate new-key checks on `Object.hasOwn`, not `in`); consider `Map` for new code.
Expand Down
89 changes: 77 additions & 12 deletions pkgs/id/web/src/world-surface.ts
Original file line number Diff line number Diff line change
Expand Up @@ -112,10 +112,52 @@ function isPrintableSymbol(symbol: string): boolean {
return true;
}

/**
* Flatten an ascending, non-overlapping `[low, high]` range table into
* `[low0, high0, low1, high1, ...]` so `rangeContains` can binary-search
* it without an array-of-arrays indirection per comparison.
*/
function flattenRanges(ranges: ReadonlyArray<readonly [number, number]>): Int32Array {
const flat = new Int32Array(ranges.length * 2);
for (let index = 0; index < ranges.length; index++) {
const [low, high] = ranges[index];
flat[index * 2] = low;
flat[index * 2 + 1] = high;
}
return flat;
}

/**
* Membership test over a flattened ascending range table, in O(log n).
*
* ⚑ Bolt optimization: these tables are ~80–180 entries and were scanned
* linearly (`ranges.some(...)`) once per non-ASCII cell of every rendered
* world frame. A 120x50 frame of box-drawing glyphs took ~52 ms of CPU
* instead of ~1 ms, because each cell paid the full linear scan. Binary
* search over the flat `Int32Array` cuts the lookup to a handful of
* integer comparisons with no allocation. `flattenRanges` preserves the
* table order, so callers must keep the literal tables sorted and
* disjoint (verified exhaustively over U+0000..U+10FFFF in this change).
*/
function rangeContains(flat: Int32Array, codePoint: number): boolean {
let low = 0;
let high = (flat.length >> 1) - 1;
while (low <= high) {
const mid = (low + high) >> 1;
const start = flat[mid * 2];
const end = flat[mid * 2 + 1];
if (codePoint < start) high = mid - 1;
else if (codePoint > end) low = mid + 1;
else return true;
}
return false;
}

// Wide code-point ranges matching Rust unicode-width 0.2's wide table,
// including the astral planes the previous BMP-only regex missed (#761):
// emoji blocks (U+1F300–U+1FAF8 ranges) and CJK Ext. B and above
// (U+20000–U+2FFFD, U+30000–U+3FFFD).
// Must stay sorted ascending and non-overlapping: `rangeContains` assumes it.
const WIDE_RANGES: ReadonlyArray<readonly [number, number]> = [
[0x1100, 0x115f],
[0x17a4, 0x17a4],
Expand Down Expand Up @@ -246,8 +288,10 @@ const WIDE_RANGES: ReadonlyArray<readonly [number, number]> = [
[0x30000, 0x3fffd],
];

const WIDE_CODE_POINTS = flattenRanges(WIDE_RANGES);

function isWideCodePoint(codePoint: number): boolean {
return WIDE_RANGES.some(([low, high]) => codePoint >= low && codePoint <= high);
return rangeContains(WIDE_CODE_POINTS, codePoint);
}

// Code points that measure 2 alone but narrow to 1 when immediately
Expand Down Expand Up @@ -342,8 +386,10 @@ const TEXT_PRESENTATION_RANGES: ReadonlyArray<readonly [number, number]> = [
[0x1f6bc, 0x1f6bc],
];

const TEXT_PRESENTATION_CODE_POINTS = flattenRanges(TEXT_PRESENTATION_RANGES);

function isTextPresentationCodePoint(codePoint: number): boolean {
return TEXT_PRESENTATION_RANGES.some(([low, high]) => codePoint >= low && codePoint <= high);
return rangeContains(TEXT_PRESENTATION_CODE_POINTS, codePoint);
}

// Code points that can start an emoji presentation sequence under
Expand Down Expand Up @@ -537,9 +583,11 @@ const EMOJI_PRESENTATION_SEQ_RANGES: ReadonlyArray<readonly [number, number]> =
[0x1f6f3, 0x1f6f3],
];

const EMOJI_PRESENTATION_SEQ_CODE_POINTS = flattenRanges(EMOJI_PRESENTATION_SEQ_RANGES);

// Exported for the boundary/parity test against the unicode-width 0.2.2 fixture (#10819).
export function isEmojiPresentationSeqBase(codePoint: number): boolean {
return EMOJI_PRESENTATION_SEQ_RANGES.some(([low, high]) => codePoint >= low && codePoint <= high);
return rangeContains(EMOJI_PRESENTATION_SEQ_CODE_POINTS, codePoint);
}

// Non-spacing and enclosing marks (Mn, Me) measure 0 under unicode-width
Expand Down Expand Up @@ -705,15 +753,32 @@ export function renderWorldSurface(root: HTMLElement, surface: ScreenSurface): b
}
if (cell.columns !== 1 && cell.columns !== 2) return false;

// ⚑ Bolt optimization: a printable ASCII cell is a single code unit in
// U+0020..U+007E β€” exactly one grapheme and exactly one column wide under
// unicode-width 0.2. Terminal frames are overwhelmingly ASCII, so this
// skips Intl.Segmenter (a full grapheme segmentation per cell) and the
// unicode-width regexes on the common path. Every other cell keeps the
// existing general path unchanged, so validation semantics are identical.
const ascii = cell.symbol.length === 1 ? cell.symbol.charCodeAt(0) : 0;
if (ascii >= 0x20 && ascii <= 0x7e) {
if (cell.columns !== 1) return false;
// ⚑ Bolt optimization: a cell whose symbol is a single Unicode scalar
// value is always exactly one grapheme cluster β€” a grapheme boundary
// sits before and after every scalar value (verified exhaustively over
// U+0000..U+10FFFF against Intl.Segmenter). Terminal frames are
// overwhelmingly single-scalar cells, so this skips Intl.Segmenter
// (β‰ˆ3 Β΅s per call) and the per-cell `Array.from`/`map`/`reduce`
// allocations on that path. The width of a one-scalar cluster is the
// plain codePointWidth of that code point β€” exactly what graphemeWidth
// reduces to when there is no U+200D, U+FE0F, modifier or U+FE0E
// narrowing β€” so the general path's width check is preserved. A lone
// surrogate (not a scalar value) keeps the general path.
const firstCodeUnit = cell.symbol.length === 0 ? -1 : cell.symbol.charCodeAt(0);
const codePoint = cell.symbol.length === 0 ? 0 : cell.symbol.codePointAt(0)!;
// U+200D (ZWJ) is excluded: graphemeWidth gives it width 1 via its
// ZWJ shortcut even though codePointWidth returns 0, so it must take
// the general path to keep the verdict identical.
const isSingleScalar =
codePoint !== 0x200d &&
((cell.symbol.length === 1 && (firstCodeUnit < 0xd800 || firstCodeUnit > 0xdfff)) ||
(cell.symbol.length === 2 && firstCodeUnit >= 0xd800 && firstCodeUnit <= 0xdbff));
if (isSingleScalar) {
if (codePoint >= 0x20 && codePoint <= 0x7e) {
if (cell.columns !== 1) return false;
} else if (codePointWidth(cell.symbol, codePoint) !== cell.columns) {
return false;
}
} else {
const graphemes = segmenter ? Array.from(segmenter.segment(cell.symbol)) : Array.from(cell.symbol);
if (graphemes.length !== 1) return false;
Expand Down