Skip to content

Commit

Permalink
Improve string cache footprint
Browse files Browse the repository at this point in the history
Use "insert" to find or insert an entry, and tell us which happened,
rather than a magic flag in the cache entry to say its new or not.
  • Loading branch information
peadar committed Jun 11, 2024
1 parent e3144a2 commit a76b7ca
Show file tree
Hide file tree
Showing 2 changed files with 5 additions and 11 deletions.
6 changes: 1 addition & 5 deletions libpstack/reader.h
Original file line number Diff line number Diff line change
Expand Up @@ -113,12 +113,8 @@ class FileReader : public Reader {
// Reader implementations

class CacheReader final : public Reader {
struct CacheEnt {
std::string value;
bool isNew = true;
};
Reader::csptr upstream;
mutable std::unordered_map<Off, CacheEnt> stringCache;
mutable std::unordered_map<Off, std::string> stringCache;
static const size_t PAGESIZE = 256;
static const size_t MAXPAGES = 16;
class Page {
Expand Down
10 changes: 4 additions & 6 deletions reader.cc
Original file line number Diff line number Diff line change
Expand Up @@ -186,12 +186,10 @@ CacheReader::read(Off off, size_t count, char *ptr) const
string
CacheReader::readString(Off off) const
{
auto &entry = stringCache[off];
if (entry.isNew) {
entry.value = Reader::readString(off);
entry.isNew = false;
}
return entry.value;
auto [it, neu] = stringCache.insert(std::make_pair(off, std::string{}));
if (neu)
it->second = Reader::readString(off);
return it->second;
}

Reader::csptr
Expand Down

0 comments on commit a76b7ca

Please sign in to comment.