Repository navigation
perf(usage): skip Unicode folding for plain ASCII metadata - #7886
Conversation
|
No actionable comments were generated in the recent review.
|
| Layer / File(s) | Summary |
|---|---|
ASCII folding fast path src/lib/redact-folding.ts |
foldForMatching returns eligible ASCII input unchanged with an identity offset map. |
Folding and scanner validation tests/lib/redact.test.ts, tests/usage/usage-ledger-scanner.test.ts, structure/transports/byte-accounting.md |
Tests compare folded text and offsets across paths, verify scanner behavior for ASCII metadata and persisted-row shapes, and document the fast-path conditions. |
Priority:
Estimated code review effort: 2 (Simple) | ~10 minutes
Change: Refactor · Severity of issue fixed: Medium
Suggested reviewers: paosder, luvs01
Merge Risk: 
Minimal · up to 4d60b
-
ASCII metadata can be processed faster without an established behavior change. No merge-blocking risk is identified.
Pre-merge checks: 
5
-
Full details: Linked Issues check
-
Explanation
Issue
#7846permits a normalization fast path as one solution.src/lib/redact-folding.ts:64-71returns the input and an identity map for ASCII strings without\\,%, or&. Other inputs retain the existing folding path.tests/usage/usage-ledger-scanner.test.ts:51-65verifies zeroString.prototype.normalizecalls for ASCII metadata during a 100-row ledger scan. The equivalence test in the same file covers legacy, redacted, nested, malformed, and rejected row shapes. The reported synthetic scan also shows a material reduction. The issue does not require a specific index or aggregate design.
Full details: Out of Scope Changes check
-
Explanation
The changes remain within issue
#7846. The source change optimizesfoldForMatching. The scanner and redaction tests verify performance behavior and unchanged masking and normalization behavior. Thestructure/transports/byte-accounting.mdupdate documents the same fast path. No unrelated product behavior or cleanup is identified in the supplied change summary.
Full details: Docstring Coverage
-
Explanation
Docstring coverage is 100.00% which is sufficient. The required threshold is 80.00%. Docstring coverage is scoped to functions touched by this diff with cyclomatic complexity of at least 2. Documentation-only edits and deletions within surviving functions are included; unchanged diff context and fully deleted functions are excluded. Analyzed 1 functions across 3 files. (1 skipped: 1 unsupported.)
-
Finishing Touches
-
📝 Generate docstrings
-
- Commit to this branch
- Create a new PR
🧪 Generate unit tests (beta)
-
- Commit to this branch
- Create a new PR
-
- Autofix · Keep fixing CodeRabbit findings and required CI, and resolving merge conflicts
Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.
Comment @coderabbitai help to get the list of available commands.
|
✅ Deterministic PR hygiene checks passed. |
Codex Review SummaryThis comment shows the latest Codex review activity on this pull request.
ℹ️ About Codex in GitHubYour team has set up Codex to review pull requests in this repo. Reviews are triggered when you
Codex reacts with 👀 while any review is running, comments if it has suggestions, and reacts with 👍 once all reviews finish with no findings. |
Summary
Closes #7846.
A filtered
GET /api/usage(provider,model,apiKeyId,since/until) re-reads the whole usage ledger, and almost all of that time went intonormalizePersistedUsageRow. The cost was in metadata redaction:foldForMatchinginsrc/lib/redact-folding.tsran escape detection and Unicode normalization (NFKD) on every character of every metadata string, for every row.For pure ASCII input with no escape introducer (
\,%,&), the folded matching view is the input itself with an identity offset map: ASCII has no confusables, no invisible format characters, and no NFKD change. This PR returns that result directly. Every credential masking pass still runs on the folded view, and any string with non-ASCII characters or an escape introducer takes the existing path unchanged. Persisted format, filter semantics, cache invalidation and singleflight behavior are untouched.Synthetic ledger, 100,000 rows / 105 MB, Bun 1.4.4, local Windows machine:
The benchmark script is local scratch and is not committed; the reporter's 279 MB ledger was not available.
Verification
Focused files ran with
OCX_TEST_NO_QUEUE=1because the host's shared test queue was saturated by other lanes.tests/usage/usage-ledger-scanner.test.ts(counts Unicode normalization calls, no timing threshold) without the fixtests/lib/redact.test.ts,tests/usage/usage-ledger-scanner.test.ts,tests/usage/usage-log.test.ts,tests/usage/usage-aggregate-cache.test.ts,tests/server/api-usage.test.tsfoldForMatchingover 100,000 rows, all 1,114,112 code points, and 32,776 ASCII cases (folded text and offset map)bun run typecheck,bun run structure:check,bun run privacy:scan,git diff --checksrc/lib/redact.ts)Hosted PR CI on head
4d60bf9b4passed, includinggatesandtest changed.Not run locally: full
bun run test, a completed localbun run test:changed(it selected 2,250 files and hit its 900 s limit on the saturated host), and a live HTTP latency benchmark.Checklist
structure/transports/byte-accounting.mddocuments the fast path.)Summary by CodeRabbit