Skip to content

Upgrade to match modern gen_server (continuation of #24)#25

Merged
michaelklishin merged 9 commits intomasterfrom
gen-batch-server-24
Mar 18, 2026
Merged

Upgrade to match modern gen_server (continuation of #24)#25
michaelklishin merged 9 commits intomasterfrom
gen-batch-server-24

Conversation

@michaelklishin
Copy link
Copy Markdown
Contributor

@michaelklishin michaelklishin commented Mar 18, 2026

This is a continuation to #24 by @the-mikedavis with a few changes from me:

  1. Tests (even though they are mock-based ones 😢)
  2. More tests
  3. Refactoring, including an unexpected throughput gain (see below)
  4. README.md doc updates
  5. A drive-by change in gen_start/4 where splitwith/2 incorrectly could leave some options behind, so it now uses partition/2
  6. This version handles single atom options (that Team RabbitMQ rarely uses but this is a generic open source library)

Benchmarks

Since this library is used on the hot code path in RabbitMQ, I figured benchmarking the changes is a must.

The benchmark demonstrate a 4% to 9% gain:

  • cast workloads gain 4-5% in terms of throughput (the RabbitMQ/Ra workloads fall into this category)
  • call workloads gain up to 8-9% with comparably lower latency
  • cast_batch, perhaps as expected, gains meager 0.1% because batching largely eliminates per-operation gains

Where Do the Gains Come From?

Apparently the modern JIT optimises the try/catch ops better than case catch.

But curiously an even nicer gain comes from a switch in loop_batched/2 where / 2 was replaced with div 2: the former produces an intermediary float for every call while the latter does not, eliminating a few wasted CPU cycles and memory churn.

Nice! Thank you, @the-mikedavis!

RabbitMQ, Ra Compatibility

These changes should be compatible with RabbitMQ branches going back to v3.13.x:

  • v3.13.x uses call/3
  • v4.0.x through main use (via Ra and Osiris) 2-tuple casts on the key code paths

the-mikedavis and others added 7 commits March 17, 2026 15:25
This change roughly syncs the init_it with gen_server's. The nice part
of this is that we can now return `ignore` and `{error, Reason}` which
are supported by `gen_server` but not `gen_batch_server`. `ignore` might
be useful for `ra_log_wal` when there is no available disk space.
This bump is perfectly reasonable for any modern
RabbitMQ series.
1. Type spec updates
2. A few more ports from the standard OTP `gen_server
@michaelklishin michaelklishin added this to the 0.9.0 milestone Mar 18, 2026
michaelklishin added a commit to rabbitmq/ra that referenced this pull request Mar 18, 2026
@michaelklishin michaelklishin merged commit de2903e into master Mar 18, 2026
3 checks passed
@michaelklishin michaelklishin deleted the gen-batch-server-24 branch March 18, 2026 21:15
michaelklishin added a commit to rabbitmq/rabbitmq-server that referenced this pull request Mar 18, 2026
mergify bot pushed a commit to rabbitmq/rabbitmq-server that referenced this pull request Mar 19, 2026
mergify bot pushed a commit to rabbitmq/rabbitmq-server that referenced this pull request Mar 19, 2026
See rabbitmq/gen-batch-server#25.

(cherry picked from commit a586b10)
(cherry picked from commit 7506d9d)
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