Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Performance improvement to SetCurrent #699

Merged
merged 3 commits into from Jul 31, 2015
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion autowiring/CoreContext.h
Expand Up @@ -1000,7 +1000,7 @@ class CoreContext:
/// context is assigned before invoking a CoreRunnable instance's Run method, and it's also assigned
/// when a context is first constructed by a thread.
/// </remarks>
static std::shared_ptr<CoreContext> CurrentContextOrNull(void);
static const std::shared_ptr<CoreContext>& CurrentContextOrNull(void);

/// <summary>
/// Identical to CurrentContextNoCheck, except returns the global context instead of a null pointer
Expand Down
14 changes: 11 additions & 3 deletions src/autowiring/CoreContext.cpp
Expand Up @@ -617,9 +617,10 @@ bool CoreContext::DelayUntilInitiated(void) {
return !IsShutdown();
}

std::shared_ptr<CoreContext> CoreContext::CurrentContextOrNull(void) {
const std::shared_ptr<CoreContext>& CoreContext::CurrentContextOrNull(void) {
static const std::shared_ptr<CoreContext> empty;
auto retVal = autoCurrentContext.get();
return retVal ? *retVal : nullptr;
return retVal ? *retVal : empty;
}

std::shared_ptr<CoreContext> CoreContext::CurrentContext(void) {
Expand Down Expand Up @@ -1269,7 +1270,14 @@ std::ostream& operator<<(std::ostream& os, const CoreContext& rhs) {
}

std::shared_ptr<CoreContext> CoreContext::SetCurrent(const std::shared_ptr<CoreContext>& ctxt) {
std::shared_ptr<CoreContext> retVal = CoreContext::CurrentContextOrNull();
const auto& currentContext = CurrentContextOrNull();

// Short-circuit test, no need to proceed if we aren't changing the context:
if (currentContext == ctxt)
return currentContext;

// Value is changing, update:
auto retVal = currentContext;
if (ctxt)
autoCurrentContext.reset(new std::shared_ptr<CoreContext>(ctxt));
else
Expand Down