Skip to content
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

Use fastify plugin metadata or an explicit passed in name option for a plugin's name #162

Merged
merged 1 commit into from
Nov 7, 2021

Conversation

airhorns
Copy link
Member

@airhorns airhorns commented Nov 5, 2021

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 the getName 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:

  --- expected
  +++ actual
  @@ -1,1 +1,1 @@
  -ERR_AVVIO_READY_TIMEOUT
  +AVV_ERR_READY_TIMEOUT

which I think is from other changes to avvio master, not this change.

Checklist

@airhorns airhorns requested a review from mcollina November 5, 2021 16:02
@mcollina
Copy link
Member

mcollina commented Nov 5, 2021

This targets the new avvio release that will be part of Fastify@v4, don't worry about the broken test.

…a plugin's name

`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 the getName 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.
@airhorns airhorns force-pushed the no-slow-function-name branch from a8a0ddb to 90f8af2 Compare November 5, 2021 21:16
Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

Copy link
Member

@mcollina mcollina left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lgtm

@mcollina
Copy link
Member

mcollina commented Nov 6, 2021

cc @jsumners you ok now?

@mcollina mcollina merged commit 0996aa6 into master Nov 7, 2021
@mcollina mcollina deleted the no-slow-function-name branch November 8, 2021 08:09
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants