remove unused file sources

This commit is contained in:
andrew (from workstation) 2021-01-01 12:21:33 +01:00
parent 1f7ae82450
commit e0d8a39ce4
3 changed files with 34 additions and 6 deletions

View File

@ -63,7 +63,7 @@ FileSourceId FileReferenceManager::get_current_file_source_id() const {
template <class T> template <class T>
FileSourceId FileReferenceManager::add_file_source_id(T source, Slice source_str) { FileSourceId FileReferenceManager::add_file_source_id(T source, Slice source_str) {
file_sources_.emplace_back(std::move(source)); file_sources_[unique_file_source_id++] = (std::move(source));
VLOG(file_references) << "Create file source " << file_sources_.size() << " for " << source_str; VLOG(file_references) << "Create file source " << file_sources_.size() << " for " << source_str;
return get_current_file_source_id(); return get_current_file_source_id();
} }
@ -144,7 +144,6 @@ vector<FullMessageId> FileReferenceManager::get_some_message_file_sources(NodeId
vector<FullMessageId> result; vector<FullMessageId> result;
for (auto file_source_id : file_source_ids) { for (auto file_source_id : file_source_ids) {
auto index = static_cast<size_t>(file_source_id.get()) - 1; auto index = static_cast<size_t>(file_source_id.get()) - 1;
CHECK(index < file_sources_.size());
const auto &file_source = file_sources_[index]; const auto &file_source = file_sources_[index];
if (file_source.get_offset() == 0) { if (file_source.get_offset() == 0) {
result.push_back(file_source.get<FileSourceMessage>().full_message_id); result.push_back(file_source.get<FileSourceMessage>().full_message_id);
@ -249,7 +248,6 @@ void FileReferenceManager::send_query(Destination dest, FileSourceId file_source
std::move(result), std::move(new_promise)); std::move(result), std::move(new_promise));
}); });
auto index = static_cast<size_t>(file_source_id.get()) - 1; auto index = static_cast<size_t>(file_source_id.get()) - 1;
CHECK(index < file_sources_.size());
file_sources_[index].visit(overloaded( file_sources_[index].visit(overloaded(
[&](const FileSourceMessage &source) { [&](const FileSourceMessage &source) {
send_closure_later(G()->messages_manager(), &MessagesManager::get_message_from_server, source.full_message_id, send_closure_later(G()->messages_manager(), &MessagesManager::get_message_from_server, source.full_message_id,
@ -366,7 +364,33 @@ void FileReferenceManager::reload_photo(PhotoSizeSource source, Promise<Unit> pr
} }
void FileReferenceManager::memory_cleanup() { void FileReferenceManager::memory_cleanup() {
auto file_source_it = file_sources_.begin();
while (file_source_it != file_sources_.end()) {
auto source_id = file_source_it->first;
auto file_nodes_it = nodes_.begin();
auto remove = true;
while (file_nodes_it != nodes_.end() && remove) {
auto elements = get_some_file_sources(file_nodes_it->first);
auto elements_it = elements.begin();
while (elements_it != elements.end()) {
if (source_id == (u_long) elements_it->get()) {
remove = false;
break;
}
elements_it++;
}
}
if (remove) {
file_source_it = file_sources_.erase(file_source_it);
} else {
file_source_it++;
}
}
} }
void FileReferenceManager::memory_cleanup(NodeId node_id) { void FileReferenceManager::memory_cleanup(NodeId node_id) {

View File

@ -143,7 +143,10 @@ class FileReferenceManager : public Actor {
Variant<FileSourceMessage, FileSourceUserPhoto, FileSourceChatPhoto, FileSourceChannelPhoto, FileSourceWallpapers, Variant<FileSourceMessage, FileSourceUserPhoto, FileSourceChatPhoto, FileSourceChannelPhoto, FileSourceWallpapers,
FileSourceWebPage, FileSourceSavedAnimations, FileSourceRecentStickers, FileSourceFavoriteStickers, FileSourceWebPage, FileSourceSavedAnimations, FileSourceRecentStickers, FileSourceFavoriteStickers,
FileSourceBackground, FileSourceChatFull, FileSourceChannelFull>; FileSourceBackground, FileSourceChatFull, FileSourceChannelFull>;
vector<FileSource> file_sources_;
std::unordered_map<u_long, FileSource> file_sources_;
u_long unique_file_source_id = 0;
int64 query_generation_{0}; int64 query_generation_{0};

View File

@ -29,8 +29,9 @@ namespace td {
template <class StorerT> template <class StorerT>
void FileReferenceManager::store_file_source(FileSourceId file_source_id, StorerT &storer) const { void FileReferenceManager::store_file_source(FileSourceId file_source_id, StorerT &storer) const {
auto index = static_cast<size_t>(file_source_id.get()) - 1; auto index = static_cast<size_t>(file_source_id.get()) - 1;
CHECK(index < file_sources_.size()); auto source_tuple = file_sources_.find(index);
auto &source = file_sources_[index]; auto source = source_tuple->second;
td::store(source.get_offset(), storer); td::store(source.get_offset(), storer);
source.visit(overloaded([&](const FileSourceMessage &source) { td::store(source.full_message_id, storer); }, source.visit(overloaded([&](const FileSourceMessage &source) { td::store(source.full_message_id, storer); },
[&](const FileSourceUserPhoto &source) { [&](const FileSourceUserPhoto &source) {