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

Avoid extra lookup in overrideContextData

Summary: Title, unecessary cost in case of conflict

Reviewed By: yfeldblum

Differential Revision: D15604536

fbshipit-source-id: ad40c84b822a96a688f810b24b539ac162fdcc59
parent 762658ad
...@@ -92,9 +92,9 @@ bool RequestContext::doSetContextData( ...@@ -92,9 +92,9 @@ bool RequestContext::doSetContextData(
// Need non-const iterators to use under write lock. // Need non-const iterators to use under write lock.
auto& state = ulock.asNonConstUnsafe(); auto& state = ulock.asNonConstUnsafe();
bool conflict = false;
auto it = state.requestData_.find(val); auto it = state.requestData_.find(val);
if (it != state.requestData_.end()) { bool conflict = it != state.requestData_.end();
if (conflict) {
if (behaviour == DoSetBehaviour::SET_IF_ABSENT) { if (behaviour == DoSetBehaviour::SET_IF_ABSENT) {
return false; return false;
} else if (behaviour == DoSetBehaviour::SET) { } else if (behaviour == DoSetBehaviour::SET) {
...@@ -102,7 +102,6 @@ bool RequestContext::doSetContextData( ...@@ -102,7 +102,6 @@ bool RequestContext::doSetContextData(
<< "Calling RequestContext::setContextData for " << "Calling RequestContext::setContextData for "
<< val.getDebugString() << " but it is already set"; << val.getDebugString() << " but it is already set";
} }
conflict = true;
} }
auto wlock = ulock.moveFromUpgradeToWrite(); auto wlock = ulock.moveFromUpgradeToWrite();
...@@ -123,8 +122,12 @@ bool RequestContext::doSetContextData( ...@@ -123,8 +122,12 @@ bool RequestContext::doSetContextData(
wlock->callbackData_.insert(data.get()); wlock->callbackData_.insert(data.get());
data->onSet(); data->onSet();
} }
wlock->requestData_[val] = RequestData::constructPtr(data.release()); auto ptr = RequestData::constructPtr(data.release());
if (conflict) {
it->second = std::move(ptr);
} else {
wlock->requestData_.insert(std::make_pair(val, std::move(ptr)));
}
return true; return true;
} }
......
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