diff --git a/td/telegram/files/FileManager.cpp b/td/telegram/files/FileManager.cpp index 8958daae..4cdcf188 100644 --- a/td/telegram/files/FileManager.cpp +++ b/td/telegram/files/FileManager.cpp @@ -3852,6 +3852,14 @@ void FileManager::memory_cleanup() { } } + /* DESTROY INVALID remote_location_info_ */ + for (const auto &pair : remote_location_info_.get_map()) { + auto &file = file_id_info_[pair.first.file_id_.fast_get()]; + if (file_nodes_[file.node_id_] == nullptr) { + remote_location_info_.erase_map_key(pair.first, pair.second); + } + } + file_hash_to_file_id_.rehash(0); file_id_info_.shrink_to_fit(); diff --git a/tdutils/td/utils/Enumerator.h b/tdutils/td/utils/Enumerator.h index e616eb5b..d2f81383 100644 --- a/tdutils/td/utils/Enumerator.h +++ b/tdutils/td/utils/Enumerator.h @@ -19,9 +19,29 @@ class Enumerator { public: using Key = int32; + std::map get_map() const { + return map_; + } + + Key next() { + if (!empty_id_.empty()) { + auto res = empty_id_.back(); + empty_id_.pop_back(); + return res; + } + + return arr_.size() + 1; + } + + void erase_map_key(ValueT key_x, Key key_y) { + empty_id_.push_back(key_y); + map_.erase(key_x); + arr_[key_y] = nullptr; + } + Key add(ValueT v) { CHECK(arr_.size() < static_cast(std::numeric_limits::max() - 1)); - int32 next_id = static_cast(arr_.size() + 1); + Key next_id = next(); bool was_inserted; decltype(map_.begin()) it; std::tie(it, was_inserted) = map_.emplace(std::move(v), next_id); @@ -38,6 +58,7 @@ class Enumerator { } private: + std::vector empty_id_; std::map map_; std::vector arr_; };