Skip to content

Commit

Permalink
proxy: speedup stats proxy
Browse files Browse the repository at this point in the history
do some sad snprintf unrolling to reduce CPU usage when dumping high
numbers of user stats.
  • Loading branch information
dormando committed Jun 21, 2024
1 parent 5300153 commit 32bc17d
Showing 1 changed file with 20 additions and 2 deletions.
22 changes: 20 additions & 2 deletions proto_proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -214,11 +214,29 @@ void process_proxy_stats(void *arg, ADD_STAT add_stats, void *c) {

// return all of the user generated stats
if (ctx->user_stats_namebuf) {
char vbuf[INCR_MAX_STORAGE_LEN];
char *e = NULL; // ptr into vbuf
const char *pfx = "user_";
const size_t pfxlen = strlen(pfx);
for (int x = 0; x < stats_num; x++) {
if (us[x].cname) {
char *name = ctx->user_stats_namebuf + us[x].cname;
snprintf(key_str, STAT_KEY_LEN-1, "user_%s", name);
APPEND_STAT(key_str, "%llu", (unsigned long long)counters[x]);
size_t nlen = strlen(name);
if (nlen > STAT_KEY_LEN-6) {
// impossible, but for paranoia.
nlen = STAT_KEY_LEN;
}
// avoiding an snprintf call for some performance ("user_%s")
memcpy(key_str, pfx, pfxlen);
memcpy(key_str+pfxlen, name, nlen);
key_str[pfxlen+nlen] = '\0';

// APPEND_STAT() calls another snprintf, which calls our
// add_stats argument. Lets skip yet another snprintf with
// some unrolling.
e = itoa_u64(counters[x], vbuf);
*(e+1) = '\0';
add_stats(key_str, pfxlen+nlen, vbuf, e-vbuf, c);
}
}
}
Expand Down

0 comments on commit 32bc17d

Please sign in to comment.