-
-
Notifications
You must be signed in to change notification settings - Fork 20
perf: improve instantation speed by dynamic formatter generation #103
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
Conversation
lib/formatFn.js
Outdated
const amountOfPlaceholders = countSpecifiers(message) | ||
|
||
if (amountOfPlaceholders === 0) { | ||
return () => message |
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.
maybe just use string variable in FastifyError
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.
instantiate Error x 1,895,804 ops/sec ±1.62% (184 runs sampled)
instantiate FastifyError x 173,772,171 ops/sec ±2.15% (179 runs sampled)
instantiate FastifySpecialError x 102,105,886 ops/sec ±2.52% (179 runs sampled)
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.
More likely to build FastifyError dynamically.
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 didn't get it
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.
nevermind.
Anyhow. this is just a poc. If there is interest, I could improve it.
@ivan-tymoshenko |
@Uzlopak sorry, I don't understand. Can you explain, please? |
new benchmarks: after: Like I wrote above, i disabled stack tracing to get a more comparable result. Generating the Stack is horrible slow. |
I use now the message string as pattern to create the format function once instead of calling format(message, args) every time. |
With default stackTraceLimit before: after: In conclusio: It is signigificant faster if we set stackTraceLimit to 0. If we use default stackTraceLimit, we are not definetly not faster. |
@@ -3,14 +3,19 @@ | |||
const benchmark = require('benchmark') | |||
const createError = require('..') | |||
|
|||
Error.stackTraceLimit = 0 |
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.
there is already benchmarks/no-stack.js
for this?
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 don't think benchmarking without stacktraces is worthwhile. Stacktraces are incredibly useful to determine where a problem originated.
What is the goal of the change? What are are you trying to achieve?
I think we already had a discussion and determined that we do not want to omit stack traces. They are the purpose of the error. |
improve string formatting perf. disabling stack trace is would make it easier to focus on the perf of string formatting. |
I think, that in production Environment disabling stacktraces is a viable decision. In development using stacktraces make obviously sense. |
Sure, it's a viable decision. But I would recommend against it. Without the stack trace you are left with very little information to determine the cause of issues. |
As I see that @trim21 is active in this repo, I wanted to suggest a change, which has maybe more impact than #100
This is just a poc. It is not complete regarding the serialization.
To have some useful performance benchmark, i disabled stacktrace generation.
before:
uzlopak@Battlestation:~/workspace/fastify-org/fastify-error$ node benchmarks/instantiate.js instantiate Error x 5,245,780 ops/sec ±0.37% (192 runs sampled) instantiate FastifyError x 238,523,493 ops/sec ±0.07% (196 runs sampled) instantiate FastifySpecialError x 20,584,535 ops/sec ±0.54% (194 runs sampled)
after:
uzlopak@Battlestation:~/workspace/fastify-org/fastify-error$ node benchmarks/instantiate.js instantiate Error x 5,208,812 ops/sec ±0.22% (192 runs sampled) instantiate FastifyError x 238,148,483 ops/sec ±0.08% (195 runs sampled) instantiate FastifySpecialError x 144,541,760 ops/sec ±0.12% (194 runs sampled)
Obviously it gets slower if type casting is feature complete. But I assume, that it is still faster than calling util.format every time.
Maybe it is useless, as stacktrace is always the biggest perf bottleneck. But in the case, somebody disables stacktrace on production services (which is i think anyway best practice) it shoiuld have an impact on perf.
Checklist
npm run test
andnpm run benchmark
and the Code of conduct