feat(cache): warn when recompute is cheaper than cache retrieval#4401
feat(cache): warn when recompute is cheaper than cache retrieval#4401raminjafary wants to merge 1 commit into
Conversation
|
Someone is attempting to deploy a commit to the Nitro Team on Vercel. A member of the Team first needs to authorize it. |
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: Organization UI Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (3)
👮 Files not reviewed due to content moderation or server errors (3)
📝 Walkthrough
✨ 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 |
|
Do you think we can port this to https://github.com/unjs/ocache ? |
|
Yeah, I will port it to |
Enable ocache's `warnWhenSlower` option globally in dev (via import.meta.dev) for cached functions and handlers, so ocache measures recompute vs. retrieval directly. Overridable per cache; stripped from production builds. Requires the ocache `warnWhenSlower` option — bump the ocache dependency once that lands before merging.
3983d1b to
5d35a94
Compare
|
Moved the detection logic into ocache as an opt-in Nitro just enables it globally in dev via import.meta.dev (stripped in prod, overridable per cache). Blocked on the ocache side: ocache PR must be merged and its version bumped in nitro. |
🔗 Linked issue
Closes #2559
❓ Type of change
📚 Description
cachedEventHandler/cachedFunctioncan be slower than the code they cache when the value is cheap to compute — the store/retrieve round-trip (and JSON (de)serialization) costs more than just recomputing it (see #2558).This adds a dev-only warning to surface that. At the storage layer we measure, per cache:
getItem, andsetItem.When median recompute is cheaper than median retrieval, we log a single warning suggesting the cache may be adding overhead.
Notes:
import.meta.dev, so the whole path is tree-shaken out of production builds.Tested with unit tests covering the warn / no-warn / threshold / warn-once cases, plus a regression guard that the function reaches ocache unwrapped. Full suite passes on both the rollup and rolldown builders.
📝 Checklist