-
-
Notifications
You must be signed in to change notification settings - Fork 4.5k
v2: Rework Replacer loop to ignore escaped braces #3121
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
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.
Thanks for the PR!
This looks good, I like how simple it is. But one question I am wondering about that I think is kind of important at this stage, is how does this affect Replacer performance?
I learned that strings.Replace* is very inefficient: #2674
Would you mind writing a simple Benchmark...()
test function and run it before and after these changes just to verify that it doesn't slow down placeholder performance? Thanks!
No problem. I'll add a couple of benchmarks and share the results. |
Benchmark before this change:
Benchmark after this change:
Good catch, you are correct. I'll have a think about how to do this more efficiently. It is almost certainly possible to do this during the main |
Awesome, thanks. Yeah, I wonder if it's possible to just skip the escaping backslash when building the string... Edit: PS. If you want to add your benchmark to the test file that'd be cool too! For any future changes. |
Benchmark after optimisation:
Same number of allocs and bytes per operation. Still a little slower though. I'd be very interested in feedback on clarity. |
I hypothesize that it's only slower on the case of JSON input (i.e. input where escapes actually happen). You can vary your input string on the benchmark test and see if that's true. Still, allocations are the big deal -- which is fixed now -- and the speed decrease is minimal. If that speed decrease is in fact just for escaped inputs, then that's probably totally acceptable. I have a comment/question I'll inline. Thanks for working on 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.
Thanks for working on this! Left a few more comments.
Oh, and just before we merge, could you post the final benchmarks? Thanks again! |
Benchmarks for three different scenarios:
|
Performance-wise, it's looking good. If we keep it in this range for any remaining, last-minute changes, LGTM! |
This commit removes the additional check for input in which the closing brace appears before the opening brace. This check has been removed for performance reasons as it is deemed an unlikely edge case.
Benchmarks for: e385e0d
|
Benchmarks for: 681531b
|
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.
Nice work!! This is a great solution IMO. Simple, elegant, and does the job without any measurable performance hit.
Thanks for your contribution! Please feel free to participate again, would be happy to work with you any time. If you're interested, I can send you an invite to our dev Slack (just let me know the email).
Many thanks for all the support and patience as I worked through this. I'm in the process of deploying Caddy for a small site at the moment. This should give me a little more experience using the application. As I do, I'll take a look at the open issues and see if anything stands out. |
This PR fixes #3116. I have added test cases to existing tests. I've made the assumption that escaping the closing brace is optional (i.e. I'm not checking that an escaped opening brace is matched with an escaped closing brace). See test cases for specific examples.