-
-
Notifications
You must be signed in to change notification settings - Fork 7.8k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Extremely slow startup performance when a dynamic module provides a complex object using useValue #12738
Comments
Looks like this might be related to the fact that a I can only assume that by having to reference the schema immediately, there's an issue with serializing the schema so that it fits within the context of a |
The @jmcdo29 answer is perfect. The issue with serialization is probably caused by: nest/packages/core/injector/module-token-factory.ts Lines 24 to 31 in ef53448
If you use anything inside the module that is directly available to the object (eg: |
Let's track this here #12739 |
Unlike the startup, this was extremely fast! Thank you all! |
Just in case it helps, I just ran into this same issue, and realized that this wasn't happening with version Thanks for adding the warning, it led me here and made the troubleshooting easier. |
I can see that this is a closed ticket, but are there any plans for fixing the slow serialization or was adding the warning in #12739 the only planned action item based on this report? |
@H4ad you guys are the best, thanks so much! |
Thanks @H4ad ! And also, is I do not care about this warning (I use bull and gets a warning it takes 13ms-20ms) and I would like to disable this specific warning (or at least control what's the threshold)- any way of doing that? |
I can open a PR for this, a threshold should be fine. |
That would be amazing @H4ad !! |
Is there an existing issue for this?
Current behavior
I have encountered a peculiar app startup performance issue. When it has a provider configured a certain way, its startup performance suffers immensely - can easily get 200x, 400x (from 50ms to 20s) or even more slower than it normally is. Because it is so blatant, I decided to register it as a defect instead of a performance suggestion.
Here's the gist. I'd like to set up a provider that looks like so:
Here
schema
is relatively complex Mongoose schema (included in the repo). With the module set up like this, the app takes ~12 seconds to start. Altering the complexity of the schema changes things considerably. I can easily get the startup time to 20 or 30 seconds just by adding a few fields and validators. Mongoose schemas are really complex objects, so perhaps providing them is not okay...Things start to get interesting here
If the imported module is not dynamic, the app takes 50ms to start
If useFactory is used instead of useValue, the app takes 50ms to start
Since I need to have a dynamic module, this is what I went with for now.
These particular inconsistencies are what made me register an issue here first rather than with Mongoose. I am having trouble wrapping my head around why there's such a problem with using a dynamic module + useValue specifically.
Now onto the badly weird
For context, here's the schema:
Nothing spectacular, mostly just a bunch of empty schemas, and 5-6 levels of nesting: schema > things > a > levels > field > items > itemIds.
The "badly" weird part is that even though
specialSchema
is unused, removing it or moving it belowthingsSchema
decreases the startup time 6 times - from 12s to 2s. The symptoms seem to allude to Mongoose using some global context when registering schemas, which is worrying, and I understand that this is probably out of scope for NestJS. Nevertheless, it might be useful information when debugging.It seems that dynamic modules with useValue-based providers have trouble working with certain structures that are employed by Mongoose.
Minimum reproduction code
https://github.com/juona/nestjs-startup-perf-issue
Steps to reproduce
I used the NestJS CLI to setup a fresh project, added the Mongoose schema to it, modified the app module slightly and installed Mongoose itself - that is all.
npm ci
npm run start:dev
Expected behavior
The expectation is that NestJS is able to register such providers without such a large performance hit.
Package
@nestjs/common
@nestjs/core
@nestjs/microservices
@nestjs/platform-express
@nestjs/platform-fastify
@nestjs/platform-socket.io
@nestjs/platform-ws
@nestjs/testing
@nestjs/websockets
Other package
mongoose
NestJS version
10.2.8
Packages versions
Node.js version
16.19.1
In which operating systems have you tested?
Other
No response
The text was updated successfully, but these errors were encountered: