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 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442
|
#include <time.h>
#include <windows.h>
struct Chrono
{
LARGE_INTEGER liStart;
LARGE_INTEGER liStop;
void Start()
{
QueryPerformanceCounter(&liStart);
}
double Stop()
{
QueryPerformanceCounter(&liStop);
LONGLONG llTimeDiff = liStop.QuadPart - liStart.QuadPart;
// To get duration in milliseconds
LARGE_INTEGER Frequency;
QueryPerformanceFrequency(&Frequency);
return llTimeDiff * 1000.0 / (double) Frequency.QuadPart;
}
};
#include <boost/intrusive/list.hpp>
#include <boost/intrusive/unordered_set.hpp>
#include <boost/functional/hash.hpp>
#include <boost/unordered_set.hpp>
#include <iostream>
#include <string>
#include <vector>
template <typename T>
struct cache_value
{
T& value;
bool created;
cache_value<T>(T& value, bool created):value(value),created(created) {}
};
#define USE_UNORDERED_SET
#ifdef USE_UNORDERED_SET
template <typename K, typename T, typename PrHash = boost::hash<K>, typename PrEqual = std::equal_to<K> >
#else
template <typename K, typename T, typename PrLess = std::less<K> >
#endif
class cache
{
//The cache organize a MRU/LRU collection of cells in a bidirectional circular list.
//The MRU cell is stored in f_mru_cell, the LRU cell is thus f_mru_cell->s_prev.
//The cells are also stored in a std::set for a fast key search.
struct cell
{
cell *s_prev,*s_next;
K s_key;
T s_value;
cell(K key):s_key(key) {s_next=s_prev=this;}
void attach_before(const cell *p)
{
p=p->s_prev;
s_prev=(s_next=p->s_next)->s_prev;
s_next->s_prev=s_prev->s_next=this;
}
void detach()
{
s_next->s_prev=s_prev;
s_prev->s_next=s_next;
}
};
#ifdef USE_UNORDERED_SET
struct cell_predhash
{
std::size_t operator()(const cell *p) const
{ return PrHash()(p->s_key); }
};
struct cell_predequal
{
bool operator()(const cell *pl,const cell *pr) const
{ return PrEqual()(pl->s_key,pr->s_key); }
};
typedef boost::unordered_set<cell *,cell_predhash,cell_predequal> cell_set;
#else
struct cell_predless
{
bool operator()(const cell *pl,const cell *pr) const
{ return PrLess()(pl->s_key,pr->s_key); }
};
typedef std::set<cell *,cell_predless> cell_set;
#endif
unsigned f_maxCell;
cell *f_cell_pooler;
cell *f_mru_cell;
cell_set f_set;
//don't allow copy or assign
cache(cache const &);
cache & operator=(cache const &);
public:
cache(unsigned maxCell): f_maxCell(maxCell<1?1:maxCell),f_cell_pooler((cell *)0),f_mru_cell((cell *)0)
#ifdef USE_UNORDERED_SET
,f_set(maxCell<1?1:maxCell)
#endif
{ f_cell_pooler=(cell *)::operator new(f_maxCell*sizeof(cell)); }
~cache()
{
clear();
::operator delete(f_cell_pooler);
}
void clear()
{
if (f_mru_cell)
{
//delete cells from LRU to MRU
cell *lru_cell=f_mru_cell->s_prev;
cell *p=lru_cell;
do
{
cell *ptmp=p->s_prev;
//cell deleted !!! caller MUST be informed
p->~cell();
p=ptmp;
}
while (p!=lru_cell);
}
f_mru_cell=(cell *)0;
f_set.clear();
}
void reset(unsigned maxCell)
{
clear();
::operator delete(f_cell_pooler);
f_cell_pooler=(cell *)0;
f_maxCell=(maxCell<1?1:maxCell);
#ifdef USE_UNORDERED_SET
f_set.rehash(f_maxCell);
#endif
f_cell_pooler=(cell *)::operator new(f_maxCell*sizeof(cell));
}
cache_value<T> fetch(K key)
{
cell tmp(key);
cell_set::iterator it=f_set.find(&tmp);
if (it!=f_set.end())
{
//key is found
//(f_mru_cell can't be NULL)
cell *found_cell=*it;
if (f_mru_cell!=found_cell)
{
//adjust cell list, found cell becomes MRU cell
found_cell->detach();
found_cell->attach_before(f_mru_cell);
f_mru_cell=found_cell;
}
//return T and notify that key was already in cache
return cache_value<T>(found_cell->s_value, false);
}
else
{
//key not found
cell *new_cell;
if (f_set.size()>=f_maxCell)
{
//cache size limit reached, remove LRU cell
//(f_mru_cell can't be NULL)
cell *lru_cell=f_mru_cell->s_prev;
tmp.s_key=lru_cell->s_key;
f_set.erase(&tmp);
lru_cell->detach();
//cell deleted !!! caller MUST be informed
lru_cell->~cell();
//create a new cell (actually reuse the just destroyed LRU cell)
new_cell=new(lru_cell) cell(key);
}
else
{
//create a new cell (using a buffer from the pooler)
new_cell=new(f_cell_pooler+f_set.size()) cell(key);
}
//insert the new cell in list
if (f_mru_cell!=(cell *)0)
new_cell->attach_before(f_mru_cell);
//set the new MRU cell
f_mru_cell=new_cell;
//insert also the new cell in the std::set
f_set.insert(new_cell);
//return the created T and notify that it wasn't yet in cache
return cache_value<T>(new_cell->s_value, true);
}
}
};
template <typename K, typename T , typename PrHash = boost::hash<K>, typename PrEqual = std::equal_to<K>>
class cache2
{
private:
//don't allow copy or assign
cache2(const cache2&);
cache2& operator=(const cache2&);
struct Entry : public boost::intrusive::list_base_hook<>, boost::intrusive::unordered_set_base_hook<>
{
K key;
T value;
friend bool operator == (const Entry& e1, const Entry& e2)
{ return PrEqual()(e1.key, e2.key); }
friend std::size_t hash_value(const Entry& e)
{ return PrHash()(e.key); }
};
struct EntryKeyHash
{
std::size_t operator()(const K& key) const
{ return PrHash()(key); }
};
struct EntryKeyEqual
{
bool operator()(const K& key, const Entry& e) const
{ return PrEqual()(key, e.key); }
bool operator()(const Entry& e, const K& key) const
{ return PrEqual()(e.key, key); }
};
typedef boost::intrusive::list<Entry> EntryList;
typedef boost::intrusive::unordered_set<Entry> EntryHashSet;
typedef typename EntryHashSet::bucket_type BucketType;
typedef typename EntryHashSet::bucket_traits BucketTraits;
std::vector<Entry> entryPool_;
EntryList entryList_;
// BE CAREFUL, the vector of bucket must be declared before the unordered set,
// in order to be initialized first in the constructor's initialization list
std::vector<BucketType> buckets_;
EntryHashSet entryHashSet_;
int maxsize_;
public:
cache2(int maxsize):
maxsize_(maxsize<1?1:maxsize),
buckets_(maxsize<1?1:maxsize),
entryHashSet_(BucketTraits(&buckets_[0], maxsize<1?1:maxsize))
{
entryPool_.reserve(maxsize<1?1:maxsize);
}
void reset(int newMaxsize)
{
entryList_.clear();
entryHashSet_.clear();
entryPool_.clear();
maxsize_ = newMaxsize<1?1:newMaxsize;
buckets.resize(maxsize_);
entryHashSet_.rehash(maxsize_);
}
cache_value<T> fetch(K key)
{
EntryHashSet::iterator found = entryHashSet_.find(key, EntryKeyHash(), EntryKeyEqual());
if(found != entryHashSet_.end())
{
//key is found
Entry& entry = *found;
EntryList::iterator it = entryList_.iterator_to(entry);
if(it != entryList_.begin())
{
//adjust entry list, found entry becomes MRU entry
entryList_.erase(it);
entryList_.push_front(entry);
}
//return T and notify that key was already in cache
return cache_value<T>(entry.value, false);
}
else
{
Entry* entry;
if(entryPool_.size() < (unsigned int)maxsize_)
{
//create a new entry
Entry newEntry;
newEntry.key = key;
entryPool_.push_back(newEntry);
entry = &(entryPool_.back());
}
else
{
//cache size limit reached, destroy the lru entry
entry = &(entryList_.back());
entryHashSet_.erase(entryHashSet_.iterator_to(*entry));
entryList_.erase(entryList_.iterator_to(*entry));
//reuse the just destroyed LRU entry
entry->key = key;
}
//insert the new entry in list
entryList_.push_front(*entry);
//insert also the new entry in the std::set
entryHashSet_.insert(*entry);
//return the created T and notify that it wasn't yet in cache
return cache_value<T>(entry->value, true);
}
}
void display()
{
//display cell key from LRU to MRU
for(EntryList::const_reverse_iterator it = entryList_.crbegin() ; it != entryList_.crend() ; ++it)
{
std::cout << it->key << '"' << it->value << '"' << " - ";
}
std::cout << '(' << entrySet_.size() << ')' << std::endl;
}
};
class HARD_DISK
{
public:
HARD_DISK(){};
HARD_DISK(int nbString, int lengthString):vec(nbString)
{
std::string gabarit('a', lengthString);
for(int i = 0 ; i < nbString ; i++)
vec[i] = gabarit;
}
void Resize(int nbString, int lengthString)
{
vec.resize(nbString);
std::string gabarit('a', lengthString);
for(int i = 0 ; i < nbString ; i++)
vec[i] = gabarit;
}
std::string Get(int num)
{
return vec[num];
}
private:
std::vector<std::string> vec;
};
void cachefetch(HARD_DISK& hdd, cache<unsigned,std::string> & c,unsigned key)
{
cache_value<std::string> cv = c.fetch(key);
if (cv.created)
cv.value = hdd.Get(key);
}
void cachefetch(HARD_DISK& hdd, cache2<unsigned,std::string> & c,unsigned key)
{
cache_value<std::string> cv = c.fetch(key);
if (cv.created)
cv.value = hdd.Get(key);
}
void Test(HARD_DISK& hdd, int numIteration, int sizecache, int numString, int lengthString)
{
srand(time(NULL));
hdd.Resize(numString, lengthString);
Chrono chrono;
double time;
std::cout << "Iteration "<< numIteration << " sizecache " << sizecache << " numString " << numString << " lengthString " << lengthString << std::endl;
chrono.Start();
{
cache<unsigned,std::string> c(sizecache);
for(int i = 0 ; i < numIteration ; i++)
{
cachefetch(hdd, c, rand() % numString);
}
}
time = chrono.Stop();
std::cout << time << std::endl;
chrono.Start();
{
cache2<unsigned,std::string> c2(sizecache);
for(int i = 0 ; i < numIteration ; i++)
{
cachefetch(hdd, c2, rand() % numString);
}
}
time = chrono.Stop();
std::cout << time << std::endl;
}
int main(int argc, char* argv[])
{
HARD_DISK hdd;
// numIteration, sizecache, numString, lengthString
Test(hdd, 1000000, 1000, 100, 20);
Test(hdd, 1000000, 10000, 10000, 20);
Test(hdd, 1000000, 100000, 100000, 20);
Test(hdd, 1000000, 1000, 10, 10000);
std::cout << "\nlot of small object (100000 objects, 100 byte each), size of cache is small" << std::endl;
Test(hdd, 1000000, 10000, 100000, 20);
std::cout << "\nlot of small object (100000 objects, 100 byte each), size of cache is good" << std::endl;
Test(hdd, 1000000, 100000, 100000, 20);
std::cout << "\nfew but BIG object (100 objects, 100000 byte each)" << std::endl;
Test(hdd, 1000000, 1000, 100, 100000);
std::cout << "\nLOT of BIG object (100000 objects, 100000 byte each), size of cache is small" << std::endl;
Test(hdd, 1000000, 10000, 100000, 100000);
std::cout << "\nLOT of BIG object (100000 objects, 100000 byte each), size of cache is good" << std::endl;
Test(hdd, 1000000, 100000, 100000, 100000);
return 0;
} |
Partager