Skip to content

cachelib: skip reaper traversal for empty caches - #485

Open
SimBuddy wants to merge 1 commit into
facebook:mainfrom
SimBuddy:perf/skip-empty-reaper-scan
Open

cachelib: skip reaper traversal for empty caches#485
SimBuddy wants to merge 1 commit into
facebook:mainfrom
SimBuddy:perf/skip-empty-reaper-scan

Conversation

@SimBuddy

@SimBuddy SimBuddy commented Sep 5, 2026

Copy link
Copy Markdown

Summary

The reaper can continue scanning assigned allocation slots after all accessible primary items have been removed. Use the existing primary-item count to skip that periodic slab walk when the cache is empty. Nonempty traversal and periodic wakeups are unchanged.

Why

Assigned slabs can remain after the accessible population reaches zero, but walking those slots cannot find an accessible item to expire. The existing atomic count supplies the signal without another flag, lock, or per-item update.

Concurrency

The count observation can race with insertion/publication. A concurrently inserted item may be handled on a subsequent periodic pass, consistent with the existing weak slab traversal. Foreground expiration checks and synchronized removal/ownership behavior are unchanged.

Performance

In a local default-style idle workload (64 MiB configured cache, approximately 16 MiB assigned slots, 5-second reaper interval, 12-second window):

Metric Original Patched
Median idle-process CPU 5.44 ms 0.27 ms
Median slot visits 131,072 0

This saves about 5 ms CPU over the 12-second idle window. It measures idle/background reaper work, not foreground cache throughput; the process CPU measurement includes other process bookkeeping. Absolute savings are modest and depend on assigned slab population, reaper frequency and hardware. A concise confirmation on the final rebased source verified zero empty-cache visits, active nonempty traversal and reactivation.

Testing

  • Current-upstream getdeps build and registered suite: 781 passed, 5 skipped, 1 existing disabled, 0 failures.
  • Three new registered tests cover allocated/unpublished/unlinked empty caches and chains; repeated reactivation, pinned expired lifetime and live retention; and deterministic concurrent slab traversal.
  • Five unavailable huge-page tests were skipped; BlockCache.ExpiredItemDestructorCallback remains disabled upstream. Unregistered suites and TSAN are not claimed.
  • python3 build/fbcode_builder/getdeps.py --allow-system-packages --scratch-path /root/wob3/getdeps --num-jobs 4 build cachelib --src-dir /root/wob3/CacheLib --build-type RelWithDebInfo
  • python3 build/fbcode_builder/getdeps.py --allow-system-packages --scratch-path /root/wob3/getdeps --num-jobs 2 test cachelib --src-dir /root/wob3/CacheLib --build-type RelWithDebInfo --no-testpilot --timeout 300 --retry 0 (private ext4 /tmp and DAC-bypass capabilities removed for filesystem/permission tests).
  • clang-format-21 --dry-run --Werror cachelib/allocator/CacheAllocator.h cachelib/allocator/tests/ReaperTest.cpp
  • git diff --check upstream/main...HEAD

Scope

No cache or eviction policy changes, new persistent state, or NVM changes. No foreground hit/miss behavior change is intended. Numerical CPU results are workload and hardware dependent.

The opportunities were identified and validated using WOB/WOB3 techniques.

WOB is an experimental method of refocussing workloads in data-heavy code. I am stress testing various codebases, to develop the method further, and cachelib seemed like a worthwhile candidate. This single result was a small win, so I am sharing. Feedback would be greatly appreciated.

WOB is human-designed, human-led, and machine assisted.

Assigned allocation slots can remain after all accessible primary items have
been removed. Use the existing primary-item count to skip the current reaper
slab walk when that count is zero, without adding state or writer work.

Periodic wakeups and nonempty traversal are unchanged. A concurrent insertion
may be handled on a later pass, consistent with the existing weak traversal
semantics; lookup expiration and synchronized ownership checks are unchanged.

Add registered tests for empty allocations, reactivation, pinned expired
parents with chained items, live retention and concurrent slab traversal.
@meta-cla meta-cla Bot added the CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed. label Sep 5, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

CLA Signed This label is managed by the Facebook bot. Authors need to sign the CLA before a PR can be reviewed.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant