perf: avoid cloning args on every pre/post#45
Conversation
There was a problem hiding this comment.
💡 Codex Review
Here are some automated review suggestions for this pull request.
Reviewed commit: 4407e9e035
ℹ️ About Codex in GitHub
Your team has set up Codex to review pull requests in this repo. Reviews are triggered when you
- Open a pull request for review
- Mark a draft as ready
- Comment "@codex review".
If Codex has suggestions, it will comment; otherwise it will react with 👍.
Codex can also answer questions or update the PR. Try commenting "@codex address that feedback".
index.js
Outdated
| args = error.args; | ||
| newArgs = args.slice(); | ||
| numArgs = args.length; | ||
| if (options?.numCallbackParams != null) { | ||
| numArgs = options.numCallbackParams; |
There was a problem hiding this comment.
Rebuild
errorArgs after overwrite in error-handler flow
When an error-handling post middleware throws/callbacks Kareem.overwriteResult, this branch updates args and newArgs but then continues without refreshing errorArgs. That means the next error handler is called with stale pre-overwrite arguments, whereas the previous implementation recomputed args per middleware iteration. This can corrupt chained error-handling behavior whenever one handler overwrites the result and a later handler depends on the updated args.
Useful? React with 👍 / 👎.
There was a problem hiding this comment.
Pull request overview
Optimizes Kareem hook execution performance by reducing per-hook argument cloning in execPre() / execPost(), and adds benchmark scripts to measure the impact.
Changes:
- Removes per-pre-hook argument cloning in
execPre()and calls each pre hook with the current args array. - Refactors
execPost()to reuse a singlenewArgsarray and callback wrapper across post hooks, rebuilding only when needed (e.g.overwriteResult). - Adds
benchmarks/execPre.jsandbenchmarks/execPost.jsto measure pre/post hook execution cost.
Reviewed changes
Copilot reviewed 3 out of 3 changed files in this pull request and generated 4 comments.
| File | Description |
|---|---|
| index.js | Removes per-hook arg cloning and refactors post-hook arg/callback handling for performance. |
| benchmarks/execPre.js | Adds a benchmark runner for execPre() scenarios. |
| benchmarks/execPost.js | Adds a benchmark runner for execPost() scenarios. |
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
|
@hasezoey @AbdelrahmanHafez can you please take a look? |
hasezoey
left a comment
There was a problem hiding this comment.
Looks good to me, though consider resolving copilot's suggestions..
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
Cloning args on every pre/post was a big drain on performance, especially for
pre()where we don't have to add callbacks anymore.Before:
After:
With post, the difference is less pronounced because we still have lingering callbacks. But still non-trival, before:
After:
This difference is significant enough to show up in Mongoose's
saveSimple()benchmark, which we have been striving to optimize.