Skip to content

Commit

Permalink
Replacement: Don't double count memory usage.
Browse files Browse the repository at this point in the history
  • Loading branch information
unknownbrackets committed Oct 30, 2022
1 parent 41b1ce0 commit c31e1c4
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 23 deletions.
39 changes: 18 additions & 21 deletions Core/TextureReplacer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -847,9 +847,14 @@ void TextureReplacer::Decimate(ReplacerDecimateMode mode) {
}

const double threshold = time_now_d() - age;
size_t totalSize = 0;
for (auto &item : cache_) {
totalSize += item.second.PurgeIfOlder(threshold);
item.second.PurgeIfOlder(threshold);
}

size_t totalSize = 0;
for (auto &item : levelCache_) {
std::lock_guard<std::mutex> guard(item.second.lock);
totalSize += item.second.data.size();
}

double totalSizeGB = totalSize / (1024.0 * 1024.0 * 1024.0);
Expand Down Expand Up @@ -1211,29 +1216,21 @@ void ReplacedTexture::PrepareData(int level) {
cleanup();
}

size_t ReplacedTexture::PurgeIfOlder(double t) {
void ReplacedTexture::PurgeIfOlder(double t) {
if (threadWaitable_ && !threadWaitable_->WaitFor(0.0))
return 0;

if (lastUsed_ < t) {
for (auto &l : levelData_) {
if (l->lastUsed < t) {
// We have to lock since multiple textures might reference this same data.
std::lock_guard<std::mutex> guard(l->lock);
l->data.clear();
// This means we have to reload. If we never purge any, there's no need.
initDone_ = false;
}
}
return 0;
}
return;
if (lastUsed_ >= t)
return;

size_t s = 0;
for (auto &l : levelData_) {
std::lock_guard<std::mutex> guard(l->lock);
s += l->data.size();
if (l->lastUsed < t) {
// We have to lock since multiple textures might reference this same data.
std::lock_guard<std::mutex> guard(l->lock);
l->data.clear();
// This means we have to reload. If we never purge any, there's no need.
initDone_ = false;
}
}
return s;
}

ReplacedTexture::~ReplacedTexture() {
Expand Down
3 changes: 1 addition & 2 deletions Core/TextureReplacer.h
Original file line number Diff line number Diff line change
Expand Up @@ -201,8 +201,7 @@ struct ReplacedTexture {
protected:
void Prepare();
void PrepareData(int level);
// Returns size of data not purged.
size_t PurgeIfOlder(double t);
void PurgeIfOlder(double t);

std::vector<ReplacedTextureLevel> levels_;
std::vector<ReplacedLevelCache *> levelData_;
Expand Down

0 comments on commit c31e1c4

Please sign in to comment.