-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathCaching.cpp
More file actions
99 lines (87 loc) · 3.77 KB
/
Caching.cpp
File metadata and controls
99 lines (87 loc) · 3.77 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
#include <iomanip>
#include <iostream>
#include <algorithm>
#include <cassert>
#include "Caching.h"
namespace Caching {
//==============================================================================================
class BaseCache::BaseCacheIMPL {
public:
std::unordered_map<term_t, Term*> lookupTable;
Term *lookup(term_t term) {
auto it = lookupTable.find(term);
return (it == lookupTable.end()) ?
nullptr : it->second;
}
Term* placeNew(term_t term, size_t length) {
Term *t = new Term();
t->term = term;
t->length = length;
lookupTable[term] = t;
return t;
}
inline void evict(term_t term) {
auto it = lookupTable.find(term);
assert(it != lookupTable.end());
if(it->second->hitCount < 3) { //higher values => smaller lookup table, closer to vanilla
delete it->second;
lookupTable.erase(it);
}
else
it->second->length = 0;
}
size_t tableSz() const { return lookupTable.size(); }
size_t size() const { return std::count_if(lookupTable.begin(),lookupTable.end(),
[](std::unordered_map<term_t, Term*>::const_reference val)
{ return val.second->length > 0;}); }
};
//==============================================================================================
size_t BaseCache::size() const { return baseimpl->size(); }
Term* BaseCache::lookup(term_t term) const {return baseimpl->lookup(term); }
Term* BaseCache::placeNew(term_t term, size_t length) { return baseimpl->placeNew(term,length); }
void BaseCache::evict(term_t t) { return baseimpl->evict(t);}
BaseCache::BaseCache() :
cacheHits(0),cachePostingsServed(0),
cachePostingsMissed(0),cacheRejected(0),
baseimpl(new BaseCacheIMPL()){}
BaseCache::~BaseCache() { delete baseimpl; }
void BaseCache::setMaxPostings(size_t maxP) {
if(maxPostings)
throw("can't resize cache in this implementation");
maxPostings = maxP;
}
bool BaseCache::visit(unsigned term, size_t length) {
auto tptr = BaseCache::lookup(term);
if(tptr && tptr->length) { //hit
++cacheHits;
cachePostingsServed += length;
hit(tptr, length);
return true;
}
else if(!tptr) { //full miss
if(maxPostings <= length)
++cacheRejected;
else
miss(term, length);
cachePostingsMissed += length;
return false;
}
//hit of evicted
hit(tptr, length);
cachePostingsMissed += length;
return false;
}
void BaseCache::report(std::ostream& out, unsigned totalQs)const {
//size_t acc = 0; for(auto t: baseimpl->lookupTable ) acc += t.second.length; //expect_eq getTotalP()
out << name()
<< " hits: " << std::setw(9) << cacheHits
<< " hit-pct: " << std::setw(5) << std::fixed << std::setprecision(2) << double(cacheHits)/double(totalQs) * 100.0
<< " size: " << std::setw(14) << getTotalP()
<< " members: " << std::setw(4) << size() << '(' << baseimpl->tableSz() << ')'
<< " rejects: " << std::setw(6) << cacheRejected
<< " postings-served: " << std::setw(14) << cachePostingsServed
<< " postings-missed: " << std::setw(14) << cachePostingsMissed
<< " srv-pct: " << std::fixed << std::setprecision(2) << double(cachePostingsServed)/double(cachePostingsServed+cachePostingsMissed) * 100.0
<< std::endl;
}
}