Skip to content

Commit 65e9297

Browse files
Brian Eckermeta-codesync[bot]
authored andcommitted
Add fast-path to ConfigPreprocessor::replaceParams for strings without parameters
Summary: **This is a semi-automated perf-finding. I don't think this has huge impact given that config reloads are likely not very frequent, but it does have the nice benefit of making CPU less spiky when they do happen. I am happy to abandon or push, whichever the team prefers. This is however quite simple for a little savings.** Strobelight profiling of edgeray/originray shows `ConfigPreprocessor::replaceParams` consuming ~1.8% exclusive CPU during config reloads (https://fburl.com/scuba/strobelight_services/zg0oj1mq). The `replaceParams` function processes every string value in the mcrouter config JSON, scanning character-by-character via `unescapeUntil` looking for `%param%` substitutions. Most config strings do not contain parameter references or escape sequences, but the function still processes them character-by-character. This adds a fast-path check at the top: if the string contains neither `%` nor `\`, return it immediately. `StringPiece::find` uses `memchr` which is highly optimized (SIMD on x86), making this check much faster than the character-by-character loop. Combined with `ConfigPreprocessor::Context::add` (1.0%) and `Context::~Context` (1.0%), config preprocessing accounts for ~3.8% of total CPU. This fast-path reduces the `replaceParams` contribution for strings without parameters. Reviewed By: stuclar Differential Revision: D95426937 fbshipit-source-id: 329428d92aa7cbf617d791edbc6b5207580ad65d
1 parent 3828a2b commit 65e9297

File tree

1 file changed

+8
-0
lines changed

1 file changed

+8
-0
lines changed

mcrouter/lib/config/ConfigPreprocessor.cpp

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1828,6 +1828,14 @@ void ConfigPreprocessor::addMacro(
18281828
dynamic ConfigPreprocessor::replaceParams(
18291829
StringPiece str,
18301830
const Context& context) const {
1831+
// Fast path: if the string contains no '%' or '\' characters,
1832+
// there are no parameters to substitute and no escape sequences
1833+
// to process, so return the string as-is.
1834+
if (str.find('%') == StringPiece::npos &&
1835+
str.find('\\') == StringPiece::npos) {
1836+
return str;
1837+
}
1838+
18311839
string buf;
18321840
while (!str.empty()) {
18331841
auto pos = unescapeUntil(str, buf, '%');

0 commit comments

Comments
 (0)