v2: Performance improvements to Replacer implementation (placeholders) #2674
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.
1. What does this change do, exactly?
Improves performance of placeholders in Caddy 2. Placeholders are the equivalent of variables in other web servers (but better). They allow you to refer to dynamic values in static configuration.
I was lazy with the initial implementation because I wanted to get it done quickly.
The old implementation pre-generated all possible values and then lazily called
strings.ReplaceAll()
in a loop. Of course this produces LOTS of unnecessary allocations. Now, we simply use functions which return the requested value, or false if the value is not recognized by that function; and instead of a blanketstrings.ReplaceAll()
for every possible placeholder, we parse the input string and look for the specific placeholders we need. By usingstrings.Builder
, the reconstruction of the string is very efficient.Note that this optimization affects ALL usage of placeholders, but the tests I performed were using them in a response body.
Using this sample config:
Here are some high-level load tests on my 8-core Macbook Pro:
2. Please link to the relevant issues.
Closes #2673
3. Which documentation changes (if any) need to be made because of this PR?
None.