Add combine helper method for arrays.

GitOrigin-RevId: a67c7a72525a9853072ad93fca47cd573a2da764
This commit is contained in:
levlam 2019-01-19 04:09:58 +03:00
parent 561b409d02
commit 7ff1b83a24
3 changed files with 27 additions and 20 deletions

View File

@ -96,15 +96,6 @@ void FileReferenceManager::remove_file_source(NodeId node_id, FileSourceId file_
nodes_[node_id].file_source_ids.remove(file_source_id);
}
static void merge(std::vector<Promise<>> &a, std::vector<Promise<>> &b) {
if (a.size() < b.size()) {
std::swap(a, b);
}
for (auto &x : b) {
a.push_back(std::move(x));
}
}
void FileReferenceManager::merge(NodeId to_node_id, NodeId from_node_id) {
VLOG(file_references) << "Merge sources of files " << to_node_id << " and " << from_node_id;
auto &to = nodes_[to_node_id];
@ -117,7 +108,7 @@ void FileReferenceManager::merge(NodeId to_node_id, NodeId from_node_id) {
to.query->generation = ++query_generation_;
}
if (from.query) {
::td::merge(to.query->promises, from.query->promises);
combine(to.query->promises, std::move(from.query->promises));
to.query->active_queries += from.query->active_queries;
from.query->proxy = {to_node_id, to.query->generation};
}

View File

@ -1888,7 +1888,7 @@ void WebPagesManager::update_web_page_instant_view_load_requests(WebPageId web_p
if (result.is_error()) {
LOG(INFO) << "Receive error " << result.error() << " for load " << web_page_id;
append(promises[0], std::move(promises[1]));
combine(promises[0], std::move(promises[1]));
for (auto &promise : promises[0]) {
promise.set_error(result.error().clone());
}
@ -1898,7 +1898,7 @@ void WebPagesManager::update_web_page_instant_view_load_requests(WebPageId web_p
auto web_page_instant_view = get_web_page_instant_view(web_page_id);
if (web_page_instant_view == nullptr) {
append(promises[0], std::move(promises[1]));
combine(promises[0], std::move(promises[1]));
for (auto &promise : promises[0]) {
promise.set_value(Unit());
}
@ -1906,20 +1906,19 @@ void WebPagesManager::update_web_page_instant_view_load_requests(WebPageId web_p
}
if (web_page_instant_view->is_loaded) {
if (web_page_instant_view->is_full) {
append(promises[0], std::move(promises[1]));
promises[1].clear();
combine(promises[0], std::move(promises[1]));
}
for (auto &promise : promises[0]) {
promise.set_value(Unit());
}
promises[0].clear();
reset_to_empty(promises[0]);
}
if (!promises[0].empty() || !promises[1].empty()) {
if (force_update) {
// protection from cycles
LOG(ERROR) << "Expected to receive " << web_page_id << " from the server, but didn't receive it";
append(promises[0], std::move(promises[1]));
combine(promises[0], std::move(promises[1]));
for (auto &promise : promises[0]) {
promise.set_value(Unit());
}
@ -1927,8 +1926,8 @@ void WebPagesManager::update_web_page_instant_view_load_requests(WebPageId web_p
}
auto &load_queries = load_web_page_instant_view_queries_[web_page_id];
auto old_size = load_queries.partial.size() + load_queries.full.size();
append(load_queries.partial, std::move(promises[0]));
append(load_queries.full, std::move(promises[1]));
combine(load_queries.partial, std::move(promises[0]));
combine(load_queries.full, std::move(promises[1]));
if (old_size == 0) {
reload_web_page_instant_view(web_page_id);
}

View File

@ -91,12 +91,12 @@ void reset_to_empty(T &value) {
}
template <class T>
auto append(vector<T> &destination, const vector<T> &source) {
void append(vector<T> &destination, const vector<T> &source) {
destination.insert(destination.end(), source.begin(), source.end());
}
template <class T>
auto append(vector<T> &destination, vector<T> &&source) {
void append(vector<T> &destination, vector<T> &&source) {
if (destination.empty()) {
destination.swap(source);
return;
@ -108,6 +108,23 @@ auto append(vector<T> &destination, vector<T> &&source) {
reset_to_empty(source);
}
template <class T>
void combine(vector<T> &destination, const vector<T> &source) {
append(destination, source);
}
template <class T>
void combine(vector<T> &destination, vector<T> &&source) {
if (destination.size() < source.size()) {
destination.swap(source);
}
destination.reserve(destination.size() + source.size());
for (auto &elem : source) {
destination.push_back(std::move(elem));
}
reset_to_empty(source);
}
inline bool begins_with(Slice str, Slice prefix) {
return prefix.size() <= str.size() && prefix == Slice(str.data(), prefix.size());
}