Skip to content

perf: use CompactString for keys (SSO ≤24 bytes)#228

Merged
kacy merged 3 commits intomainfrom
perf/compact-string-keys
Feb 20, 2026
Merged

perf: use CompactString for keys (SSO ≤24 bytes)#228
kacy merged 3 commits intomainfrom
perf/compact-string-keys

Conversation

@kacy
Copy link
Owner

@kacy kacy commented Feb 20, 2026

summary

  • replaces Box<str> with compact_str::CompactString for all keyspace keys across keyspace, dropper, concurrent keyspace, and memory tracking
  • CompactString inlines strings up to 24 bytes on the stack, eliminating heap allocation for short keys — the vast majority of real cache keys
  • ENTRY_OVERHEAD bumped from 120 → 128 to reflect the larger key struct (24 vs 16 bytes)

what was tested

  • cargo test -p emberkv-core --lib — 345 tests pass
  • cargo check -p emberkv-core — clean build
  • cargo clippy -p emberkv-core --no-deps — no new warnings (8 pre-existing doc_lazy_continuation in sorted_set.rs)

design considerations

the 8-byte increase per key struct is more than offset by eliminating heap allocator overhead (16-32 bytes per allocation) for keys under 24 bytes. lookups work unchanged since CompactString implements Borrow<str>, Hash, and Eq. the compact_str crate is well-maintained and widely used (~36M downloads).

kacy added 3 commits February 19, 2026 23:45
add auth, election, and raft_transport to the modules table and features
list. fix emberkv-core typo → ember-core in the related crates table.
hot-path quick wins from performance audit (phase A):

- fuse double hash probe on GET/GET_STRING into single lookup
  that checks expiry inline, eliminating ~2M redundant probes/sec
- cache value_size in Entry struct so memory accounting is O(1)
  instead of walking entire collections on every mutation
- shrink last_access from u64 ms to u32 secs, saving 4 bytes/entry
  and improving cache-line packing for the hot Entry struct
- replace allocating peek_command_name (3 heap allocs per MULTI/EXEC
  command) with zero-allocation eq_ignore_ascii_case comparisons
replace Box<str> with compact_str::CompactString for all keyspace keys.
CompactString inlines strings up to 24 bytes on the stack, eliminating
heap allocation for the vast majority of real cache keys (e.g. "user:123",
"session:abc", "rate:ip:1.2.3.4").

- Box<str>: 16 bytes on stack (ptr + len), always heap-allocates
- CompactString: 24 bytes on stack, inlines ≤24 byte strings

the extra 8 bytes per key struct is offset by eliminating heap allocation
overhead (typically 16-32 bytes per allocation) for short keys. lookups
work unchanged since CompactString implements Borrow<str>.

ENTRY_OVERHEAD bumped from 120 to 128 to account for the larger key struct.
@kacy kacy merged commit 0004b8f into main Feb 20, 2026
4 of 7 checks passed
@kacy kacy deleted the perf/compact-string-keys branch February 20, 2026 19:34
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant