| 783 | } |
| 784 | |
| 785 | int32_t ExpertCache::admit(int64_t layer, int64_t expert) { |
| 786 | if (sealed_) return kNotResident; // STRATA_EXPERT_NIBBLE: nothing is admitted (and so filled) after the startup fill |
| 787 | if (layer < 0 || layer >= n_layers_ || expert < 0 || expert >= n_expert_) return kNotResident; |
| 788 | const size_t at = (size_t) (layer * n_expert_ + expert); |
| 789 | if (residency_[at] != kNotResident) return residency_[at]; |
| 790 | // R4.2g: THE PER-LAYER PATH. Same "no eviction" rule, but the ceiling is this layer's own range rather |
| 791 | // than one counter shared by all 48 - which is what confined the measured hit rate to 2.97%. |
| 792 | if (per_layer_) { |
| 793 | if (layer_next_.empty()) return kNotResident; |
| 794 | int64_t lo = 0, hi = 0; |
| 795 | layer_slot_range(layer, lo, hi); |
| 796 | if ((int64_t) layer_next_[(size_t) layer] >= hi) return kNotResident; // this layer's quota is full |
| 797 | residency_[at] = layer_next_[(size_t) layer]++; |
| 798 | ++admitted_; |
| 799 | return residency_[at]; |
| 800 | } |
| 801 | if (next_free_ >= slots_) return kNotResident; // full: no eviction, deliberately - see the header |
| 802 | residency_[at] = (int32_t) next_free_; |
| 803 | return (int32_t) next_free_++; |
| 804 | } |
| 805 | |
| 806 | uint8_t* ExpertCache::device_slot(int32_t slot) { |
| 807 | if (slot < 0 || slot >= slots_) return nullptr; |