Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 81 additions & 0 deletions src/flat_id_set.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,81 @@
// A set of block ids for the Simulator's per-request scratch sets. Open
// addressing, cleared in O(1) by bumping an epoch, so one instance serves every
// request without allocating. count() is a multiply and an array read: the pin
// predicate calls it for every pinned object cache_skip_pinned walks past (~9
// per block accessed, LRU at 64k blocks), which made std::unordered_set's
// modulo and node chase the hot spot. Iterates in insertion order.

#pragma once

#include <algorithm>
#include <cstdint>
#include <vector>

namespace prefixsim {

class FlatIdSet {
public:
void clear() {
ids_.clear();
if (++epoch_ == 0) { // wrapped: old stamps would alias the new epoch
std::fill(stamps_.begin(), stamps_.end(), 0u);
epoch_ = 1;
}
}
bool insert(uint64_t id) {
if (2 * (ids_.size() + 1) > stamps_.size()) grow();
size_t h = slot(id);
for (; stamps_[h] == epoch_; h = (h + 1) & mask_) {
if (keys_[h] == id) return false;
}
stamps_[h] = epoch_;
keys_[h] = id;
ids_.push_back(id);
return true;
}
size_t count(uint64_t id) const {
if (stamps_.empty()) return 0;
for (size_t h = slot(id); stamps_[h] == epoch_; h = (h + 1) & mask_) {
if (keys_[h] == id) return 1;
}
return 0;
}
size_t size() const { return ids_.size(); }
std::vector<uint64_t>::const_iterator begin() const { return ids_.begin(); }
std::vector<uint64_t>::const_iterator end() const { return ids_.end(); }

private:
// Hashes ids in groups of 8: the group picks a run of 8 slots and the low 3
// bits the slot within it. lcp ids are dense and given out in first-access
// order, so a request's blocks mostly have consecutive ids, and the pin
// predicate is asked about them run after run as cache_skip_pinned walks the
// same pinned blocks for every eviction. Keeping neighbours in one cache line
// is what made std::unordered_set (id % prime) fast there. Hashing each id
// on its own scattered them: LRU at 16k blocks on the first 100k
// freeinference requests took 12% longer than with std::unordered_set on an
// AMD Zen 4, and hashing groups takes 8% less. The groups themselves are
// still scattered, so ids that are not consecutive (raw hashes) hash as well
// as before.
size_t slot(uint64_t id) const {
const uint64_t group = (id >> 3) * 0x9E3779B97F4A7C15ull >> 32;
return ((group << 3) | (id & 7)) & mask_;
}
void grow() { // to twice the size, keeping the current ids
const size_t cap = std::max<size_t>(64, 2 * stamps_.size());
keys_.assign(cap, 0);
stamps_.assign(cap, 0);
mask_ = cap - 1;
epoch_ = 1;
std::vector<uint64_t> ids;
ids.swap(ids_);
for (const uint64_t id : ids) insert(id);
}

std::vector<uint64_t> keys_;
std::vector<uint32_t> stamps_;
std::vector<uint64_t> ids_;
uint32_t epoch_ = 1;
size_t mask_ = 0;
};

} // namespace prefixsim
24 changes: 14 additions & 10 deletions src/simulator.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -153,7 +153,7 @@ const AlgoEntry kAlgos[] = {
/// the arriving request's block set (Simulator::alpha_). extern "C" because it
/// is stored in a C function pointer declared inside libCacheSim's extern "C".
extern "C" bool alpha_is_pinned(void *ctx, obj_id_t id) {
return static_cast<const std::unordered_set<obj_id_t> *>(ctx)->count(id) != 0;
return static_cast<const FlatIdSet *>(ctx)->count(id) != 0;
}

} // namespace
Expand Down Expand Up @@ -303,7 +303,7 @@ bool Simulator::serve(const Request &request, std::string &error) {
// Every statistic this simulator reports is computed here, against the cache
// exactly as the request found it. Nothing in phases 2 and 3 can change it.
resident_.assign(n_pos, 0);
std::unordered_set<obj_id_t> missing;
missing_.clear();

for (size_t i = 0; i < n_pos; ++i) {
const int64_t cost = config_.cost_model.cost(static_cast<int64_t>(i));
Expand All @@ -324,7 +324,7 @@ bool Simulator::serve(const Request &request, std::string &error) {
++stats_.n_block_hits;
stats_.saved_cost += cost;
} else {
missing.insert(request.blocks[i]);
missing_.insert(request.blocks[i]);
}
}

Expand All @@ -333,7 +333,7 @@ bool Simulator::serve(const Request &request, std::string &error) {
// ---------------- phase 2: allocate ----------------
const int64_t occupied = cache_->get_occupied_byte(cache_);
const int64_t free_slots = config_.cache_size_blocks - occupied;
const int64_t needed = static_cast<int64_t>(missing.size()) - free_slots;
const int64_t needed = static_cast<int64_t>(missing_.size()) - free_slots;
if (needed > 0) {
// Pins cover phase 2 only. Phase 3 must not evict at all (phase 2 reserved
// exactly enough room, and n_unexpected_evictions asserts it), so leaving
Expand Down Expand Up @@ -398,12 +398,9 @@ bool Simulator::allocate(const Request &request, int64_t needed,
int64_t unobserved_freed = 0;

// Which of Alpha's blocks are resident right now. Only consulted on the
// no-victim-reported path, so building it costs nothing for the algorithms
// that do report their victims.
alpha_resident_.clear();
for (size_t i = 0; i < request.blocks.size(); ++i) {
if (resident_[i] != 0) alpha_resident_.insert(request.blocks[i]);
}
// no-victim-reported path, so it is built there, on first use, and costs
// nothing for the algorithms that do report their victims.
bool alpha_resident_built = false;

// Termination: the request needs at most `cache_size` distinct blocks (larger
// ones were skipped), so the cache always holds at least `needed` blocks the
Expand Down Expand Up @@ -477,6 +474,13 @@ bool Simulator::allocate(const Request &request, int64_t needed,
unobserved_freed += occupied_before - occupied_now;
if (unobserved_freed < needed) continue;

if (!alpha_resident_built) {
alpha_resident_.clear();
for (size_t i = 0; i < request.blocks.size(); ++i) {
if (resident_[i] != 0) alpha_resident_.insert(request.blocks[i]);
}
alpha_resident_built = true;
}
int64_t missing = 0;
for (const obj_id_t id : alpha_) {
if (probe(id)) continue;
Expand Down
4 changes: 3 additions & 1 deletion src/simulator.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
#include <vector>

#include "cost_model.hpp"
#include "flat_id_set.hpp"
#include "trace.hpp"

extern "C" {
Expand Down Expand Up @@ -158,7 +159,8 @@ class Simulator {

// Scratch reused across requests so the hot loop does not allocate.
std::vector<char> resident_;
std::unordered_set<obj_id_t> alpha_;
FlatIdSet alpha_;
FlatIdSet missing_;
std::unordered_set<obj_id_t> alpha_resident_;
std::vector<obj_id_t> victims_;
};
Expand Down
Loading