Use fastify plugin metadata or an explicit passed in name option for a plugin's name #162
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
This is a second approach to the same thing in #140.
getName
in avvio gets a string name for each plugin registered to use when reporting debug info. Doing some profiling of a big fastify application, I noticed that a lot of time was spent getting the name of avvio plugins in thegetName
function. For me,require.cache
was very big, and the number of registered plugins was very big, so the O(n^2) search for each file in the require cache was actually taking a good long while. Also,getName
relying on the require cache is undesirable as ESM becomes more and more common -- we should try to move to an approach that doesn't need that.Fortunately, a lot of plugins registered with avvio already report their names! We were somewhat accidentally using
getName
to get the name of a lot of plugins that already have fastify metadata set, or pass an explicit name in with their options. We can avoid the nasty performance pitfall by just accepting the incoming names inside avvio, and then falling back on one of the much faster last-resort strategies if plugin names aren't set.It is kind of unfortunate that we can't use the plugin's file path as a nice, human friendly name for a plugin that isn't otherwise named, but I think the performance issue merits its removal, and I will add documentation to fastify to indicate that naming your own plugins is a good idea.
I tried running the fastify test suite with this version of this library in place, and the only things that failed were like this:
which I think is from other changes to
avvio
master, not this change.Checklist
npm run test
andnpm run benchmark
and the Code of conduct