Repository navigation
perf(do): strip redundant reads on the metric-history hot path - #184
Conversation
recordMetricHistory runs per ctx.metrics.* call. Its durable upsert is the irreducible cost, but each call also re-ran CREATE TABLE IF NOT EXISTS and a bucket-existence SELECT. Memoize both per SQL handle: - ensureMetricHistoryTable no-ops after the first call per handle (WeakSet). - A per-handle known-bucket set lets an in-minute repeat skip the existence read (and the cap-scan/trim) and go straight to the upsert. Membership only, never values, so a stored aggregate can never be made wrong; the set is bounded (cleared past a cap) and a fresh isolate re-reads from the DB. No durability change — every call still upserts synchronously; only the reads around it are removed. Bench (repeated same-series measurement): ~17.4us → ~12.2us (~1.4x). Cutting the upsert itself would need deferred coalescing, which is a durability-semantics change left for a separate change. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NjFm7WWd7aqnKduj2thnNU
✅ Deploy Preview for lunorash ready!
To edit notification comments on pull requests, go to your Netlify project configuration. |
|
Important Review skippedReview was skipped as selected files did not have any reviewable changes. 💤 Files selected but had no reviewable changes (1)
⛔ Files ignored due to path filters (1)
⚙️ Run configurationConfiguration used: Path: .coderabbit.yaml Review profile: CHILL Plan: Pro Plus Run ID: ⛔ Files ignored due to path filters (1)
📒 Files selected for processing (1)
You can disable this status message by setting the Use the checkbox below for a quick retry:
WalkthroughThe metric history write path now memoizes table initialization and ChangesMetric history caching
Estimated code review effort: 2 (Simple) | ~15 minutes Possibly related PRs
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✨ Finishing Touches🧪 Generate unit tests (beta)
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 |
|
Thank you for following the naming conventions! 🙏 |
|
Thank you for confirming the Contributor License Agreement! 🙏 |
There was a problem hiding this comment.
Actionable comments posted: 2
🤖 Prompt for all review comments with AI agents
Verify each finding against current code. Fix only still-valid issues, skip the
rest with a brief reason, keep changes minimal, and validate.
Inline comments:
In `@packages/do/src/metric-history.ts`:
- Line 119: Capture the stable SQL handle once per request before
instrumentation can re-proxy this.sql, then pass that captured handle to
recordMetricHistory(). Update the request flow in shard-do.ts around
currentStmtSamples so the same handle is reused for metric-history recording and
both caches remain keyed consistently.
- Around line 274-282: Update the bucket-recording flow around the
cache.add(cacheKey) call so cacheKey is added only after confirming the bucket
still exists following insertion and retention. Do not mark retention-deleted
buckets as known; their next write must perform the existence and retention
checks instead of being skipped by the cache.
🪄 Autofix (Beta)
Fix all unresolved CodeRabbit comments on this PR:
- Push a commit to this branch (recommended)
- Create a new PR with the fixes
ℹ️ Review info
⚙️ Run configuration
Configuration used: Path: .coderabbit.yaml
Review profile: CHILL
Plan: Pro Plus
Run ID: 13b1c1da-366a-4054-a387-0abcf2bdbbb5
⛔ Files ignored due to path filters (1)
packages/do/__tests__/metric-history.test.tsis excluded by!**/__tests__/**,!**/*.test.tsand included bypackages/**
📒 Files selected for processing (2)
packages/do/__bench__/metric-history-record.bench.tspackages/do/src/metric-history.ts
Merging this PR will improve performance by 36.61%
|
| Benchmark | BASE |
HEAD |
Efficiency | |
|---|---|---|---|---|
| ⚡ | broadcastWhisper to 1024 members |
1,281.2 µs | 937.8 µs | +36.61% |
| 🆕 | repeated same-(series, bucket) measurement |
N/A | 423.9 µs | N/A |
Tip
Curious why this is faster? Comment @codspeedbot explain why this is faster on this PR, or directly use the CodSpeed MCP with your agent.
Comparing perf/metric-history-coalesce (096ba21) with alpha (7acf42b)2
Footnotes
-
60 benchmarks were skipped, so the baseline results were used instead. If they were deleted from the codebase, click here and archive them to remove them from the performance reports. ↩
-
No successful run was found on
alpha(a07c091) during the generation of this report, so 7acf42b was used instead as the comparison base. There might be some changes unrelated to this pull request in this report. ↩
A late, out-of-window sample can be inserted then immediately removed by the retention trim in the same call; caching it as known let its next write skip both the existence check and the trim, resurrecting the expired row until a cold bucket re-triggered trimming (CodeRabbit review, PR #184). Cache only a bucket confirmed to already exist (bucketExists), so a freshly-created bucket is re-checked on its next write and cached only once durable. Adds a regression test: an old bucket trimmed by a far-ahead bucket stays gone when re-recorded. Bench unchanged (~1.4x); steady-state repeats are still the cached hot path. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_01NjFm7WWd7aqnKduj2thnNU
Sync the cloud observability + security-hardening branch with alpha (OTLP resource attributes #182, notify observability #183, do perf #184, workerd custom-spans test #180, ai-gateway correlation #181, and release bumps). Clean auto-merge — no conflicts. Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com> Claude-Session: https://claude.ai/code/session_0197KjhmBDB3PXAc6TXiPqZ9
|
This pull request has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs. |
What
recordMetricHistoryruns on everyctx.metrics.*call across every package. A trace + bench (from the notify observability work) showed it costs ~19 µs/call, dominated by a synchronous SQLite write — but each call also re-ran two avoidable statements:CREATE TABLE IF NOT EXISTSand abucketExistsSELECT.This memoizes both per SQL handle, with no change to durability semantics — every call still upserts synchronously, only the reads around it are removed.
ensureMetricHistoryTableno-ops after the first call per handle (WeakSet).WeakMap, so a fresh isolate (post-hibernation) starts cold and re-consults the DB.Impact
Bench (
metric-history-record.bench.ts, repeated same-(series, minute)measurement — the hot path a metrics-heavy handler or a per-recipient fan-out hits):~1.4× (≈30% off the hot path), entirely from removing the two redundant statements.
Not in scope (deliberately)
The remaining ~12 µs is the durable upsert itself. Cutting that needs deferred coalescing — folding N same-series counts in memory and flushing once per dispatch — which changes durability timing (a mid-dispatch crash would lose the buffered window) and touches the RPC/alarm/subscription flush lifecycle. That's a semantics decision for its own change, not this one.
Verification
metric-history,metric-buffer,function-metrics, telemetry,shard-do.admin), incl. a new test asserting 50 repeats of one series fold tocount: 50(the cache-hit path stays exact).lint:types+ eslint + prettier clean.Complements (independent of) the notify delivery-observability PR #183, which reduces how often notify hits this path; this makes the path itself cheaper for every caller.
🤖 Generated with Claude Code
Summary by CodeRabbit
Performance Improvements
Tests