Skip to content

Conversation

@678098
Copy link
Collaborator

@678098 678098 commented Dec 24, 2025

Commit 1:

  1. Replace bslmt::ThreadUtil::Handle usage with bslmt::ThreadUtil::Id. According to docs, Handle doesn't allow usage from a thread different from the one it corresponds to. Id allows this.
  2. Keep a copy of the assigned dispatcher thread id in mqbi::DispatcherClientData. This allows to simplify lookups for inDispatcherThread checks.
  3. Move mqbi::Dispatcher::inDispatcherThread(int) -> mqbi::DispatcherClient::inDispatcherThread(void). The previous declaration was abstract and it was not possible to inline inDispatcherThread, now it is possible to inline it.
  4. Simplify inDispatcherThread calls. Before: BSLS_ASSERT_SAFE(dispatcher()->inDispatcherThread(this));, after: BSLS_ASSERT_SAFE(inDispatcherThread());

Commit 2:

  1. Move d_threadId: mqbi::DispatcherClientData::d_threadId -> mqbi::DispatcherClient::d_threadId. This allows to simplify calls in inDispatcherThread() and make it even faster.

Benchmarks

Darwin M2 - RelWithDebInfo

Before: 4.1 ns per iteration BSLS_ASSERT_OPT(dispatcher_p->inDispatcherThread(client_p));
After (commit 1): 2.7 ns per iteration BSLS_ASSERT_OPT(client_p->inDispatcherThread());
After (commit 2): 2.0 ns per iteration BSLS_ASSERT_OPT(client_p->inDispatcherThread());

Linux amd64 - RelWithDebInfo

Before: 4.6 ns per iteration BSLS_ASSERT_OPT(dispatcher_p->inDispatcherThread(client_p));
After (commit 1): 3.4 ns per iteration BSLS_ASSERT_OPT(client_p->inDispatcherThread());
After (commit 2): 0.6 ns per iteration BSLS_ASSERT_OPT(client_p->inDispatcherThread());

Note that the speedup on M2 Darwin is 2x, on amd64 Linux is 7.5x.

@678098 678098 requested a review from a team as a code owner December 24, 2025 00:12
@678098 678098 force-pushed the 251223_opt_inDispatcherThread branch 8 times, most recently from 586dff0 to 829bded Compare December 29, 2025 21:33
@678098 678098 force-pushed the 251223_opt_inDispatcherThread branch from 829bded to 4bf33e5 Compare December 29, 2025 21:33
@678098 678098 requested a review from pniedzielski December 29, 2025 22:30
Copy link

@bmq-oss-ci bmq-oss-ci bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build 3174 of commit 4bf33e5 has completed with FAILURE

// the first operand:
return (dispatcherClientData().threadId() ==
bslmt::ThreadUtil::selfId()) ||
(dispatcherClientData().threadId() == 0);
Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Just to be sure I verified in my benchmark that inDispatcherThread() takes the same time with or without the second part of the expression when the first part is true (short-circuit).

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Also dispatcherClientData() is a virtual function and makes inDispatcherThread() slower than it can be. If we just keep threadId as a field in mqbi::DispatcherClient, we can address it directly without virtual function call and save time on this check.

Copy link
Collaborator Author

@678098 678098 Jan 5, 2026

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If we just keep threadId as a field in mqbi::DispatcherClient, we can address it directly without virtual function call and save time on this check.

I applied this in the second commit and updated the benchmarks

Note that amd64 Linux time improved much more:

TEST /blazingmq/src/groups/mqb/mqba/mqba_dispatcher.t.cpp CASE -1
inDispatcherThread():
       total: 6.11 ms (10000000 iterations)
    per call: 0 ns

@678098 678098 force-pushed the 251223_opt_inDispatcherThread branch from a4307af to 675493f Compare January 5, 2026 20:25
Copy link

@bmq-oss-ci bmq-oss-ci bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Build 3178 of commit 675493f has completed with FAILURE

Copy link
Collaborator

@pniedzielski pniedzielski left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update yesterday, looks good.

@678098 678098 merged commit ce2fdaf into bloomberg:main Jan 6, 2026
40 checks passed
@678098 678098 deleted the 251223_opt_inDispatcherThread branch January 6, 2026 20:21
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants