Skip to content

Extract weighted partitioning into WeightedLfuCapacityPartition#815

Draft
bitfaster wants to merge 1 commit into
users/alexpeck/weightedlfufrom
users/alexpeck/weightedlfu-partition
Draft

Extract weighted partitioning into WeightedLfuCapacityPartition#815
bitfaster wants to merge 1 commit into
users/alexpeck/weightedlfufrom
users/alexpeck/weightedlfu-partition

Conversation

@bitfaster

Copy link
Copy Markdown
Owner

Summary

Refactor (no behavior change): extract the weighted partitioning logic out of the large
ConcurrentLfuCore into a dedicated WeightedLfuCapacityPartition, mirroring LfuCapacityPartition.

Stacked on #814 (weighted LFU). Base is users/alexpeck/weightedlfu so the diff shows only this
refactor; retarget to main once #814 merges.

What moves

  • New internal interface ICapacityPartition { int Capacity { get; } } (Lfu namespace; distinct
    from the existing public Lru.ICapacityPartition). Both LfuCapacityPartition and the new
    WeightedLfuCapacityPartition implement it.
  • New WeightedLfuCapacityPartition holds the window/main weighted maximums
    (Maximum/WindowMaximum/MainProtectedMaximum), the hill-climb state, all weighted tuning
    consts
    (MainPercentage, MainProtectedPercentage, AdmitHashDosThreshold, the HillClimber*
    set, SmallCacheThreshold, QueueTransferThreshold), the maximum-initialization, and the climb
    (OptimizePartitioning + DetermineWeightedAdjustment/IncreaseWindow/DecreaseWindow/CopySign).
  • ConcurrentLfuCore now holds an ICapacityPartition and is ~150 lines lighter.

Design

The weighted climb physically moves nodes between the core's LRU lists and mutates the core's
weighted sizes, so it isn't a pure calculator like LfuCapacityPartition. The partition's
OptimizePartitioning<K,V,N,P,E>(ref ConcurrentLfuCore<…> cache, ICacheMetrics metrics, int sampleThreshold) reaches back into the core — the same pattern as the existing
INodePolicy.ExpireEntries(ref ConcurrentLfuCore<…> cache). Conceptual boundary: the maximums
move to the partition; the sizes (weightedSize/windowWeightedSize/mainProtectedWeightedSize)
remain in the core as runtime accounting (the climb mutates them via the ref). The core exposes the
minimum needed members as internal for the reach-back; the count and weighted paths select the
concrete partition via the compile-time IsWeighted flag (cast helpers CountCapacity/WeightedCapacity).

Testing

  • Behavior-preserving: full suite green (1579) across the count and weighted paths; builds on all
    TFMs (netstandard2.0, netcoreapp3.1, net6.0, net10.0, net48); dotnet format clean.
  • New WeightedLfuCapacityPartitionTests covers the ctor maximum split (mirrors
    LfuCapacityPartitionTests); the climb itself is exercised end-to-end by the existing
    WeightedNodePolicyTests window-adaptation test.

Notes / possible follow-ups

  • The reach-back widens the core's internal surface slightly (lists, two size fields,
    ReFitProtectedWeighted). A narrower set of encapsulated move helpers is a possible follow-up.
  • AdmitHashDosThreshold is admission tuning (used by AdmitCandidateWeighted) rather than
    partitioning, but moves with the other tuning consts per the requested grouping.

Draft

Opened as a draft for review of the boundary and the ICapacityPartition shape.

Move the weighted hill-climb (OptimizeWeightedPartitioning + Determine/Increase/Decrease/CopySign), the window/main weighted maximums, the climb state, and all weighted tuning consts out of ConcurrentLfuCore into a new WeightedLfuCapacityPartition, mirroring LfuCapacityPartition. Both implement a new internal ICapacityPartition. The climb reaches back into the core via a generic OptimizePartitioning(ref core, ...) method, matching the existing INodePolicy.ExpireEntries(ref core) pattern; weighted sizes remain in the core as accounting. Behavior-preserving: full suite green (1579).

Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com>
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