Skip to content
This repository was archived by the owner on Aug 8, 2020. It is now read-only.

Commit 17b82c8

Browse files
committed
Redesigned the enumerator
1 parent f5418f0 commit 17b82c8

File tree

2 files changed

+77
-36
lines changed

2 files changed

+77
-36
lines changed

td/telegram/files/FileManager.cpp

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1243,8 +1243,8 @@ Result<FileId> FileManager::register_file(FileData &&data, FileLocationSource fi
12431243
int32 remote_key = 0;
12441244
if (file_view.has_remote_location()) {
12451245
RemoteInfo info{file_view.remote_location(), file_location_source, file_id};
1246-
remote_key = remote_location_info_.add(info);
1247-
auto &stored_info = remote_location_info_.get(remote_key);
1246+
RemoteInfo stored_info;
1247+
std::tie(remote_key, stored_info) = remote_location_info_.add_and_get(info);
12481248
if (stored_info.file_id_ == file_id) {
12491249
get_file_id_info(file_id)->pin_flag_ = true;
12501250
new_remote = true;
@@ -3068,8 +3068,9 @@ Result<FileId> FileManager::check_input_file_id(FileType type, Result<FileId> re
30683068
int32 remote_id = file_id.get_remote();
30693069
if (remote_id == 0) {
30703070
RemoteInfo info{file_view.remote_location(), FileLocationSource::FromUser, file_id};
3071-
remote_id = remote_location_info_.add(info);
3072-
if (remote_location_info_.get(remote_id).file_id_ == file_id) {
3071+
RemoteInfo stored_info;
3072+
std::tie(remote_id, stored_info) = remote_location_info_.add_and_get(info);
3073+
if (stored_info.file_id_ == file_id) {
30733074
get_file_id_info(file_id)->pin_flag_ = true;
30743075
}
30753076
}
@@ -4059,20 +4060,27 @@ void FileManager::memory_cleanup() {
40594060

40604061
/* DESTROY INVALID remote_location_info_ */
40614062
if (true) {
4062-
std::set<int32> empty_remote_ids = {};
4063-
for (auto empty_remote_id : remote_location_info_.get_empty_id()) {
4064-
empty_remote_ids.insert(empty_remote_id);
4065-
}
4066-
auto map = remote_location_info_.get_map();
4063+
remote_location_info_.lock_access_mutex();
4064+
4065+
std::unordered_map<int32, RemoteInfo> old_remote_info = {};
4066+
{
4067+
auto map = remote_location_info_.get_map();
4068+
4069+
auto mapSize = map.size();
4070+
auto mapMaxSize = map.max_size();
40674071

4068-
auto emptyIdsSize = empty_remote_ids.size();
4069-
auto mapSize = map.size();
4072+
auto it = map.begin();
4073+
while (it != map.end()) {
4074+
old_remote_info[it->second] = it->first;
4075+
it++;
4076+
}
4077+
}
40704078

4071-
auto it = map.begin();
4072-
while (it != map.end() && (empty_remote_ids.find(it->second) == empty_remote_ids.end())) {
4079+
auto it = old_remote_info.begin();
4080+
while (it != old_remote_info.end()) {
40734081
auto is_invalid = false;
40744082

4075-
auto find_file = file_id_info_.find(it->first.file_id_.fast_get());
4083+
auto find_file = file_id_info_.find(it->second.file_id_.fast_get());
40764084
if (find_file != file_id_info_.end()) {
40774085
auto &file = find_file->second;
40784086
auto find_file_node = file_nodes_.find(file.node_id_);
@@ -4085,10 +4093,12 @@ void FileManager::memory_cleanup() {
40854093
}
40864094

40874095
if (is_invalid) {
4088-
remote_location_info_.erase(it->second);
4096+
remote_location_info_.erase_unsafe(it->first);
40894097
}
40904098
it++;
40914099
}
4100+
4101+
remote_location_info_.unlock_access_mutex();
40924102
}
40934103

40944104
/* DESTROY NULL file_id_info_ */

tdutils/td/utils/Enumerator.h

Lines changed: 52 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
#include <limits>
1212
#include <map>
1313
#include <tuple>
14+
#include <shared_mutex>
1415

1516
namespace td {
1617

@@ -23,44 +24,74 @@ class Enumerator {
2324
return map_;
2425
}
2526

26-
std::pair<Key, bool> next() {
27-
if (!empty_id_.empty()) {
28-
auto res = empty_id_.back();
29-
empty_id_.pop_back();
30-
return std::make_pair(res, true);
31-
}
27+
void lock_access_mutex() const {
28+
access_mutex.lock();
29+
}
30+
31+
void unlock_access_mutex() const {
32+
access_mutex.unlock();
33+
}
3234

33-
return std::make_pair((Key) (arr_.size() + 1), false);
35+
/**
36+
*
37+
* @return true if the key is new
38+
*/
39+
Key next() {
40+
auto id = next_id++;
41+
42+
return id;
3443
}
3544

36-
void erase(Key key_y) {
37-
empty_id_.push_back(key_y);
45+
void erase_unsafe(Key key_y) {
46+
auto find_val = arr_.find(key_y);
47+
if (find_val != arr_.end()) {
48+
// Erase this
49+
map_.erase(find_val->second);
50+
// TODO: Not sure about erasing this, instead
51+
arr_.erase(key_y);
52+
}
3853
}
3954

4055
Key add(ValueT v) {
41-
auto next_id = next();
56+
std::lock_guard<std::shared_timed_mutex> writerLock(access_mutex);
57+
58+
return add_internal(v);
59+
}
60+
61+
Key add_internal(ValueT v) {
62+
auto id = next();
4263
bool was_inserted;
4364
decltype(map_.begin()) it;
44-
std::tie(it, was_inserted) = map_.emplace(std::move(v), next_id.first);
45-
if (was_inserted && next_id.second) {
46-
arr_[next_id.first - 1] = &it->first;
47-
} else if (was_inserted) {
48-
arr_.push_back(&it->first);
49-
} else if (next_id.second) {
50-
empty_id_.push_back(next_id.first);
65+
std::tie(it, was_inserted) = map_.emplace(std::move(v), id);
66+
if (was_inserted) {
67+
arr_[id] = it->first;
5168
}
5269
return it->second;
5370
}
5471

5572
const ValueT &get(Key key) const {
56-
auto pos = static_cast<Key>(key - 1);
57-
return *arr_[pos];
73+
std::shared_lock<std::shared_timed_mutex> readerLock(access_mutex);
74+
75+
return get_internal(key);
76+
}
77+
78+
const ValueT &get_internal(Key key) const {
79+
return arr_.at(key);
80+
}
81+
82+
std::pair<Key,ValueT> add_and_get(ValueT v) {
83+
std::lock_guard<std::shared_timed_mutex> writerLock(access_mutex);
84+
85+
auto remote_key = add_internal(v);
86+
auto &stored_info = get_internal(remote_key);
87+
return std::make_pair(remote_key, stored_info);
5888
}
5989

6090
private:
61-
std::vector<Key> empty_id_;
91+
mutable int32 next_id = 1;
6292
std::map<ValueT, int32> map_;
63-
std::vector<const ValueT *> arr_;
93+
std::unordered_map<int32, ValueT> arr_;
94+
mutable std::shared_timed_mutex access_mutex;
6495
};
6596

6697
} // namespace td

0 commit comments

Comments
 (0)