fix(core): cache the Model class in isModelStatic to avoid repeated require()#18234
fix(core): cache the Model class in isModelStatic to avoid repeated require()#18234Svehla wants to merge 3 commits into
Conversation
…equire() Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
|
No actionable comments were generated in the recent review. 🎉 ℹ️ Recent review info⚙️ Run configurationConfiguration used: defaults Review profile: CHILL Plan: Pro Run ID: 📒 Files selected for processing (1)
🚧 Files skipped from review as they are similar to previous changes (1)
📝 WalkthroughWalkthrough
ChangesisModelStatic Module Cache Optimization
🎯 2 (Simple) | ⏱️ ~10 minutes
🚥 Pre-merge checks | ✅ 5✅ Passed checks (5 passed)
✏️ Tip: You can configure your own custom pre-merge checks in the settings. ✨ Finishing Touches🧪 Generate unit tests (beta)
Warning There were issues while running some tools. Please review the errors and either fix the tool's configuration or disable the tool if it's a critical failure. 🔧 ESLint
ESLint install failed. For unrecoverable errors, disable the tool in CodeRabbit configuration. Warning Review ran into problems🔥 ProblemsStopped waiting for pipeline failures after 30000ms. One of your pipelines takes longer than our 30000ms fetch window to run, so review may not consider pipeline-failure results for inline comments if any failures occurred after the fetch window. Increase the timeout if you want to wait longer or run a 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 |
There was a problem hiding this comment.
Actionable comments posted: 1
🤖 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/core/test/unit/utils/model-utils.test.ts`:
- Around line 14-37: This test mutates Sequelize model state but the file lacks
per-test lifecycle hooks; add a beforeEach that calls sequelize.sync({ force:
true }) (or equivalent reset) and an afterEach that closes the Sequelize
instance (sequelize.close()) to ensure isolation between tests, and update any
tests referencing MyModel or calling isModelStatic to rely on those hooks so
Sequelize instances and models are reset between tests.
🪄 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: defaults
Review profile: CHILL
Plan: Pro
Run ID: d67f9ece-e460-46b8-9dc8-197179d3c1cb
📒 Files selected for processing (2)
packages/core/src/utils/model-utils.tspackages/core/test/unit/utils/model-utils.test.ts
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Co-Authored-By: Claude Opus 4.8 (1M context) <noreply@anthropic.com>
Pull Request Checklist
Closes #18233
Description of Changes
The problem we hit
isModelStatic()callsrequire('../model')on every invocation:isModelStaticis on a hot path — it runs (directly, and viaisSameInitialModel/extractModelDefinition) once per association while building queries. Although Node caches the loaded module, therequire()call itself still funnels throughModule._loadand re-resolves the request every time.We ran into this in production: under our Node module-resolution setup, that per-call
require('../model')repeatedly walkedtrySelf → findParentPackageJSON → fs.stat, and on include-heavy queries (many associations) the resolution overhead showed up at the top of our CPU profiles. We were running this fix as apatch-packagepatch against@sequelize/core@7.0.0-alpha.47and wanted to upstream it.Even on a plain Node setup where each resolution is cheaper, the work is pure waste: the resolved
Modelclass never changes once the module graph is initialized.The fix
Cache the resolved
Modelclass in a module-level variable and reuse it on subsequent calls. The lazyrequire(the cyclic-import workaround) is preserved for the first call, so module-load ordering is unchanged; only the redundant re-resolution on later calls is removed.Tests
Added a unit test that spies on
Module._loadand asserts theModelmodule is not re-resolved across 50isModelStaticcalls after warm-up.I verified locally that the test passes with the fix and fails without it (the uncached version resolves the module once per call — 50 times). Behavior is unchanged and remains covered by the existing
isModelStatic/isSameInitialModeltests;yarn eslintandyarn lerna run test-typings --scope=@sequelize/coreboth pass.List of Breaking Changes
None. This is an internal, behavior-preserving performance optimization.
Summary by CodeRabbit
Refactor
Tests