From e6b348aa6ba363360a441372e4f9610ec803e923 Mon Sep 17 00:00:00 2001 From: Alex Sexton Date: Thu, 9 Apr 2026 21:48:18 -0500 Subject: [PATCH] Fast-path simple segment sort-key comparisons MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Teach compareSegmentSortKeys() to compare the common single-string, no-numeric segment keys directly on their cached lowercase values instead of walking token arrays. This trims remaining initializeExpandedPaths comparison overhead. Experiments: #31 Metric: visible_rows_ready_ms 197.5 → 192.9 (-2.3%) --- packages/path-store/src/sort.ts | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/packages/path-store/src/sort.ts b/packages/path-store/src/sort.ts index b38faddeb..bb029bf7e 100644 --- a/packages/path-store/src/sort.ts +++ b/packages/path-store/src/sort.ts @@ -90,6 +90,22 @@ export function compareSegmentSortKeys( leftKey: SegmentSortKey, rightKey: SegmentSortKey ): number { + // Segments without numeric runs produce a single string token, so they can + // compare directly on the cached lower-case string without walking the token + // arrays or coercing tokens back through String(). + if ( + leftKey.tokens.length === 1 && + rightKey.tokens.length === 1 && + typeof leftKey.tokens[0] === 'string' && + typeof rightKey.tokens[0] === 'string' + ) { + if (leftKey.lowerValue === rightKey.lowerValue) { + return 0; + } + + return leftKey.lowerValue < rightKey.lowerValue ? -1 : 1; + } + const tokenComparison = compareNaturalTokens(leftKey.tokens, rightKey.tokens); if (tokenComparison !== 0) { return tokenComparison;