Skip to content

Commit 388ffae

Browse files
committed
perf: replace COMMANDS struct format strings with precompiled packers.
Each entry in the COMMANDS table now carries a precompiled struct.Struct for the fixed-size prefix of its wire format (HEADER_STRUCT plus any leading "extras" bytes); variable-length tails (key, value, auth payloads) are concatenated as bytes after packer.pack(...). Previously each call site built a fresh format string from HEADER_STRUCT + the per-command 'struct' suffix and substituted in the per-call lengths via "%". This costs a string concat plus a % format on every call, and -- because the resulting format string embeds the per-call lengths ('17s', '23s', ...) -- defeats the LRU cache that the struct module keeps over compiled formats. Once the working set of distinct lengths exceeds that cache (100 entries by default in CPython), every call recompiles its format from scratch. The hot build loops in get_multi, set_multi, and set_multi_cas paid this on every key; additionally bind packer.pack and the relevant COMMANDS / MAGIC / STATUS lookups to locals. Microbench (500-key get_multi request build, no network): 1181us -> 441us, ~2.7x.
1 parent be3d1dd commit 388ffae

2 files changed

Lines changed: 166 additions & 124 deletions

File tree

0 commit comments

Comments
 (0)