-
-
Notifications
You must be signed in to change notification settings - Fork 7.7k
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
perf: prefer use factory instead of use value when possible #12753
perf: prefer use factory instead of use value when possible #12753
Conversation
Very interesting, but nice to see a solution for this. Great job! |
Let's see what Kamil thinks, but ha me I really like this modification. 😉👍 |
I'd suggest to add some comments on why we are using factory over value providers in those places to prevent on future refactoring on them |
useValue: ExternalContextCreator.fromContainer(container), | ||
useFactory: () => ExternalContextCreator.fromContainer(container), | ||
}, | ||
{ | ||
provide: ModulesContainer, | ||
useValue: container.getModules(), | ||
useFactory: () => container.getModules(), | ||
}, | ||
{ | ||
provide: HttpAdapterHost, | ||
useValue: httpAdapterHost, | ||
useFactory: () => httpAdapterHost, | ||
}, | ||
{ | ||
provide: LazyModuleLoader, | ||
useFactory: lazyModuleLoaderFactory, | ||
}, | ||
{ | ||
provide: SerializedGraph, | ||
useValue: container.serializedGraph, | ||
useFactory: () => container.serializedGraph, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I notice another good side-effect on changing ExternalContextCreator
and SerializedGraph
providers to factory:
SerializedGraph#toJSON
was called 6x in a very simple nestjs app before these changes, which I think it was useless because it was only invoked due to the name toJSON
being known as a special method for JSON.stringify
(used by stringify
from fast-safe-stringify
)
🥳
Pull Request Test Coverage Report for Build 349dfb92-7ead-48ea-8cfa-4a0a9202a4d3
💛 - Coveralls |
{ provide: MULTER_MODULE_OPTIONS, useValue: options }, | ||
// useFactory is for performance reasons | ||
// see more: https://github.com/nestjs/nest/issues/12738#issuecomment-1810987001 | ||
{ provide: MULTER_MODULE_OPTIONS, useFactory: () => options }, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
With this change, you won't be able to use multiple multer modules in your project (due to equal hash tokens).
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Yeah, but if the user uses https://docs.nestjs.com/techniques/file-upload#async-configuration, they already have this effect.
Anyone that does not use useValue
will have this same issue, so I think we can consider this a minor
if we want to release a safer version.
Maybe we can study emit warnings when we detect two modules duplicated (with same hash), so the users can be aware of this behavior.
But this could be better in a follow-up PR, what do you think?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actually, the https://github.com/nestjs/nest/pull/12753/files/8a2d19db5a2bf3c9026139cef9f884bb95b0100c#diff-cfbc60d072c116c16132ec78536eae4514513069d6e2020ab1444e41175ad5f1R25 already makes the module be unique.
Long term we should circle back on something we discussed a while ago - so basically replacing the Eventually, we'll leave that hash for the purpose of diff algorithms (devtools) and just use weak references, dropping the entire "modules redundancy optimization" technique in future major versions. |
@kamilmysliwiec I will create an issue to compile all the viable options to solve this issue based on that previous comment I already made and include your suggestion then we can validate which one is possible to build and which has the best DX. But as soon I have more time, I can create a PR for this. |
Was not possible to defer |
lgtm |
Improbable but not impossible, if you have some reproduction, I can investigate. |
PR Checklist
Please check if your PR fulfills the following requirements:
PR Type
What kind of change does this PR introduce?
What is the current behavior?
Because of the nature of the
ModuleTokenFactory
, heavy objects will slow down the initialization, especially when using.proto
files.Issue Number: #12719
Closes #12719
What is the new behavior?
Now, we defer the initialization of the
.proto
files by usinguseFactory
, this will fix any initialization issue.I also updated almost all the references for
useValue
, so this can help avoid the same issue in other places.Does this PR introduce a breaking change?