Skip to content

Commit

Permalink
Optimize getLogger() for common case of single singleton (saves ~45 n…
Browse files Browse the repository at this point in the history
…s) (#5138)
  • Loading branch information
Cameron Desrochers authored and GitHub Enterprise committed Dec 20, 2024
1 parent 705fde9 commit 0ff1cf0
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 1 deletion.
8 changes: 7 additions & 1 deletion groups/bal/ball/ball_loggermanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -739,6 +739,7 @@ LoggerManager::LoggerManager(
, d_userFieldsPopulator(configuration.userFieldsPopulatorCallback())
, d_attributeCollectors(bslma::Default::globalAllocator(globalAllocator))
, d_logger_p(0)
, d_defaultLoggerCount(0)
, d_categoryManager(bslma::Default::globalAllocator(globalAllocator))
, d_maxNumCategoriesMinusOne((unsigned int)-1)
, d_loggers(bslma::Default::globalAllocator(globalAllocator))
Expand Down Expand Up @@ -1143,6 +1144,7 @@ LoggerManager::LoggerManager(
, d_userFieldsPopulator(configuration.userFieldsPopulatorCallback())
, d_attributeCollectors(bslma::Default::globalAllocator(globalAllocator))
, d_logger_p(0)
, d_defaultLoggerCount(0)
, d_categoryManager(bslma::Default::globalAllocator(globalAllocator))
, d_maxNumCategoriesMinusOne((unsigned int)-1)
, d_loggers(bslma::Default::globalAllocator(globalAllocator))
Expand Down Expand Up @@ -1334,13 +1336,15 @@ void LoggerManager::deallocateLogger(Logger *logger)
++itr;
}
}
d_defaultLoggerCount.storeRelease(
static_cast<unsigned>(d_defaultLoggers.size()));
d_defaultLoggersLock.unlock();

logger->~Logger();
d_allocator_p->deallocate(logger);
}

Logger& LoggerManager::getLogger()
Logger& LoggerManager::getLoggerSlow()
{
// TBD: optimize it using thread local storage

Expand All @@ -1363,6 +1367,8 @@ void LoggerManager::setLogger(Logger *logger)
else {
d_defaultLoggers[id] = logger;
}
d_defaultLoggerCount.storeRelease(
static_cast<unsigned>(d_defaultLoggers.size()));
}

// Category Management
Expand Down
20 changes: 20 additions & 0 deletions groups/bal/ball/ball_loggermanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -900,7 +900,9 @@ BSLS_IDENT("$Id: $")
#include <bslmt_mutex.h>
#include <bslmt_readerwritermutex.h>

#include <bsls_atomic.h>
#include <bsls_compilerfeatures.h>
#include <bsls_performancehint.h>
#include <bsls_util.h> // 'forward<T>(V)'

#include <bsl_functional.h>
Expand Down Expand Up @@ -1289,6 +1291,9 @@ class LoggerManager {
Logger *d_logger_p; // holds default logger
// (owned)

bsls::AtomicUint d_defaultLoggerCount; // number of thread-specific
// default loggers

CategoryManager d_categoryManager; // category manager

unsigned int d_maxNumCategoriesMinusOne;
Expand Down Expand Up @@ -1375,6 +1380,11 @@ class LoggerManager {
/// publication `cause`.
void publishAllImp(Transmission::Cause cause);

/// Return a non-`const` reference to a logger managed by this logger
/// manager suitable for performing logging operations for this thread
/// of execution.
Logger& getLoggerSlow();

public:
// CLASS METHODS
#ifndef BDE_OMIT_INTERNAL_DEPRECATED
Expand Down Expand Up @@ -2349,6 +2359,16 @@ void LoggerManager::visitObservers(
BSLS_COMPILERFEATURES_FORWARD(t_OBSERVER_VISITOR, visitor));
}

inline
Logger& LoggerManager::getLogger()
{
if (BSLS_PERFORMANCEHINT_PREDICT_UNLIKELY(
d_defaultLoggerCount.loadAcquire())) {
return getLoggerSlow();
}
return *d_logger_p;
}

// ACCESSORS
inline
bslma::Allocator *LoggerManager::allocator() const
Expand Down

0 comments on commit 0ff1cf0

Please sign in to comment.