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

Nit simplify setContext

Summary: I keep modifying this code, and I keep feeling like it would be more readable without std::swap. Thoughts?

Reviewed By: yfeldblum

Differential Revision: D9140662

fbshipit-source-id: a86862dbfd1c0caeb69cc66b215994a3d1dd0294
parent 3e915e8b
...@@ -158,12 +158,17 @@ void RequestContext::clearContextData(const std::string& val) { ...@@ -158,12 +158,17 @@ void RequestContext::clearContextData(const std::string& val) {
std::shared_ptr<RequestContext> RequestContext::setContext( std::shared_ptr<RequestContext> RequestContext::setContext(
std::shared_ptr<RequestContext> newCtx) { std::shared_ptr<RequestContext> newCtx) {
auto& curCtx = getStaticContext(); auto& staticCtx = getStaticContext();
if (newCtx != curCtx) { if (newCtx == staticCtx) {
FOLLY_SDT(folly, request_context_switch_before, curCtx.get(), newCtx.get()); return newCtx;
}
FOLLY_SDT(
folly, request_context_switch_before, staticCtx.get(), newCtx.get());
// Call set/unset for all request data that differs auto curCtx = staticCtx;
if (newCtx && curCtx) { if (newCtx && curCtx) {
// Only call set/unset for all request data that differs
auto newLock = newCtx->state_.rlock(); auto newLock = newCtx->state_.rlock();
auto curLock = curCtx->state_.rlock(); auto curLock = curCtx->state_.rlock();
auto& newData = newLock->callbackData_; auto& newData = newLock->callbackData_;
...@@ -173,24 +178,22 @@ std::shared_ptr<RequestContext> RequestContext::setContext( ...@@ -173,24 +178,22 @@ std::shared_ptr<RequestContext> RequestContext::setContext(
callback->onUnset(); callback->onUnset();
} }
} }
std::swap(curCtx, newCtx); staticCtx = newCtx;
for (auto* callback : newData) { for (auto* callback : newData) {
if (curData.find(callback) == curData.end()) { if (curData.find(callback) == curData.end()) {
callback->onSet(); callback->onSet();
} }
} }
} else if (newCtx) { } else {
std::swap(curCtx, newCtx); if (curCtx) {
// Note: actually newCtx
curCtx->onSet();
} else if (curCtx) {
curCtx->onUnset(); curCtx->onUnset();
std::swap(curCtx, newCtx);
} }
staticCtx = newCtx;
if (newCtx) {
newCtx->onSet();
} }
}
// Note: actually curCtx return curCtx;
return newCtx;
} }
std::shared_ptr<RequestContext>& RequestContext::getStaticContext() { std::shared_ptr<RequestContext>& RequestContext::getStaticContext() {
......
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