Commit 0f3478f4 authored by Matthieu Martin's avatar Matthieu Martin Committed by Facebook Github Bot

Optimize RequestData shallow copying

Summary:
The shallow copy code path currently iterates and copies all values manually. Instead of benefiting from the underlying data collection's implementation of self-copy.
At the very least, it's simpler looking code.

Inheriting unique_ptr is usually discouraged, but in this specific (internal impl detail) use case, I can't foresee any risk.

Reviewed By: yfeldblum

Differential Revision: D15599260

fbshipit-source-id: 73edea27a27e8fbc8b057cf3e974ee3d83a6e760
parent c719177f
...@@ -282,15 +282,8 @@ RequestContext::setShallowCopyContext() { ...@@ -282,15 +282,8 @@ RequestContext::setShallowCopyContext() {
auto child = std::make_shared<RequestContext>(); auto child = std::make_shared<RequestContext>();
if (parent) { if (parent) {
auto ret = folly::acquireLocked(as_const(parent->state_), child->state_); auto locks = folly::acquireLocked(as_const(parent->state_), child->state_);
auto& parentLock = std::get<0>(ret); *std::get<1>(locks) = *std::get<0>(locks);
auto& childLock = std::get<1>(ret);
childLock->callbackData_ = parentLock->callbackData_;
childLock->requestData_.reserve(parentLock->requestData_.size());
for (const auto& entry : parentLock->requestData_) {
childLock->requestData_.insert(std::make_pair(
entry.first, RequestData::constructPtr(entry.second.get())));
}
} }
// Do not use setContext to avoid global set/unset // Do not use setContext to avoid global set/unset
......
...@@ -98,7 +98,16 @@ class RequestData { ...@@ -98,7 +98,16 @@ class RequestData {
struct DestructPtr { struct DestructPtr {
void operator()(RequestData* ptr); void operator()(RequestData* ptr);
}; };
using SharedPtr = std::unique_ptr<RequestData, DestructPtr>; struct SharedPtr : public std::unique_ptr<RequestData, DestructPtr> {
SharedPtr() = default;
using std::unique_ptr<RequestData, DestructPtr>::unique_ptr;
SharedPtr(const SharedPtr& other) : SharedPtr(constructPtr(other.get())) {}
SharedPtr& operator=(const SharedPtr& other) {
return operator=(constructPtr(other.get()));
}
SharedPtr(SharedPtr&&) = default;
SharedPtr& operator=(SharedPtr&&) = default;
};
// Initialize the pseudo-shared ptr, increment the counter // Initialize the pseudo-shared ptr, increment the counter
static SharedPtr constructPtr(RequestData* ptr); static SharedPtr constructPtr(RequestData* ptr);
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment