Skip to content

Commit

Permalink
alias_resolve: switch from explicit map locks to std::atomic
Browse files Browse the repository at this point in the history
Improve performance of concurrent access. Since C++20 is now
guaranteed in the codebase, std::atomic<std::shared_ptr> can be used
instead of explicit mutex locking. This should improve performance
slightly.

(Citing https://stackoverflow.com/a/40227410: """wrap the shared_ptr
with a lock, but this solution is not so scalable under some
contention, and in a sense, loses the automatic feeling of the
standard shared pointer.""")

Fixes: gromox-1.11-77-g4f4bef351
  • Loading branch information
jengelh committed Oct 11, 2024
1 parent a5b8bf4 commit 316f1e4
Showing 1 changed file with 6 additions and 13 deletions.
19 changes: 6 additions & 13 deletions mda/alias_resolve.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -52,9 +52,8 @@ static std::atomic<bool> xa_notify_stop{false};
static std::condition_variable xa_thread_wake;
static alias_map xa_empty_alias_map;
static domain_set xa_empty_domain_set;
static std::shared_ptr<alias_map> xa_alias_map;
static std::shared_ptr<domain_set> xa_domain_set;
static std::mutex xa_alias_lock;
static std::atomic<std::shared_ptr<alias_map>> xa_alias_map;
static std::atomic<std::shared_ptr<domain_set>> xa_domain_set;
static std::thread xa_thread;
static mysql_adaptor_init_param g_parm;
static std::chrono::seconds g_cache_lifetime;
Expand Down Expand Up @@ -156,11 +155,10 @@ static void xa_refresh_once()
auto conn = sql_make_conn();
auto newmap = xa_refresh_aliases(conn);
auto newdom = xa_refresh_domains(conn);
std::unique_lock lk(xa_alias_lock);
if (newmap != nullptr)
xa_alias_map = std::move(newmap);
xa_alias_map.exchange(std::move(newmap));
if (newdom != nullptr)
xa_domain_set = std::move(newdom);
xa_domain_set.exchange(std::move(newdom));
}

static void xa_refresh_thread()
Expand All @@ -177,13 +175,8 @@ static void xa_refresh_thread()

static hook_result xa_alias_subst(MESSAGE_CONTEXT *ctx) try
{
decltype(xa_alias_map) alias_map_ptr;
decltype(xa_domain_set) domset_ptr;
{
std::unique_lock lk(xa_alias_lock);
alias_map_ptr = xa_alias_map;
domset_ptr = xa_domain_set;
}
auto alias_map_ptr = xa_alias_map.load();
auto domset_ptr = xa_domain_set.load();
auto &alias_map = alias_map_ptr != nullptr ? *alias_map_ptr : xa_empty_alias_map;
auto &domset = domset_ptr != nullptr ? *domset_ptr : xa_empty_domain_set;

Expand Down

0 comments on commit 316f1e4

Please sign in to comment.