-
Notifications
You must be signed in to change notification settings - Fork 376
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: improve getMetricAsString for long strings + less allocations #587
base: next
Are you sure you want to change the base?
Conversation
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.
exciting! mind adding a changelog entry as well?
Sure, I'll have a look at the changelog. |
Here's an example of a benchmark vs. |
Cool, let's hold off a tiny bit and see if @shappir is able to provide some feedback here 🙂 |
Thank you - reviewing |
Thank you. |
Have you had time to review this yet, @shappir? 🙂 |
Sorry for the delay. Reviewing right now. |
I measured just the string escape implementation outside the context of My conclusion is that using a single pass will be faster when replacing 4 or more chars, but not for less. As a result, it seems not to be appropriate for this use-case. |
Thanks @shappir |
That seems reasonable to me at least 🙂 |
Perhaps I wasn't clear enough or I'm not understanding you: Based on my tests (using your implementation but external to The new escape function is faster only when the length of the replace chain gets longer. But since currently the length of the chain is capped at 3, this has no impact. But on this, my current recommendation is to not merge this PR. Note: I did not measure GC impact specifically. The existing method may indeed create more garbage which could impact execution speed, even externally to |
@shappir I understand, and yes, I misunderstood your initial comment, thanks for the clarification. @SimenB Let me know what you want to do. Feel free to close the PR or whatever you think. |
Final note: interestingly this person conducted a similar comparison, and the results match my findings |
Do all replacements for escaping in a single pass (less large string copies due to chained replace calls).
prom-client@v15.0.0-1
(which was a really good improvement) is sometimes on-par or a bit better than this, but not by much, and I think in terms of memory this is a bit more efficient (for long strings) so perhaps there's a place for a different strategy for different inputs, but I didn't want to get into that complexity here.@shappir you improvement was really good, I was trying to also address allocations here (chained
replace
), would actually love your take here as well, since it's possible the best solution is some combination.