Skip to content

Conversation

@mtmk
Copy link
Member

@mtmk mtmk commented Oct 22, 2025

This pull request focuses on improving timer initialization and timeout handling in the NatsSubBase class, and introduces a new microbenchmark for subscription construction. The main changes ensure that timers are only created when necessary and initialized with explicit parameters, and that timeouts default to TimeSpan.Zero instead of the default struct value. Additionally, the code now uses static delegates for timer callbacks to reduce allocations.

Timeout and Timer Initialization Improvements:

  • Changed the default values for _idleTimeout, _startUpTimeout, and _timeout to use TimeSpan.Zero instead of the default struct value, ensuring more explicit and predictable timeout behavior.
  • Updated timer creation logic to check for TimeSpan.Zero instead of default and to initialize timers with explicit state and callback parameters. This reduces GC pressure and ensures timers are only allocated when needed. [1] [2]

Delegate and Callback Optimization:

  • Replaced inline delegates for cancellation token and timer callbacks with static delegates, reducing allocations and improving performance. [1] [2]

Benchmarking:

  • Added a new microbenchmark class SubscribeCtor to measure the performance of constructing new NatsSub<int> instances, aiding in performance testing and analysis.

Benchmark SubscribeCtor.NewNatsSub

this PR:

Method Mean Error StdDev Gen0 Gen1 Allocated
NewNatsSub 2.270 us 1.555 us 0.0852 us 0.1183 0.1144 1.61 KB

main:

Method Mean Error StdDev Gen0 Gen1 Allocated
NewNatsSub 2.790 us 0.7886 us 0.0432 us 0.1335 0.1297 1.8 KB

@mtmk mtmk requested a review from scottf October 22, 2025 12:34
Copy link
Contributor

@scottf scottf left a comment

Choose a reason for hiding this comment

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

LGTM

@mtmk mtmk merged commit 6fa0700 into main Oct 23, 2025
27 of 29 checks passed
@mtmk mtmk deleted the fix-sub-callback-internal branch October 23, 2025 08:15
mtmk added a commit that referenced this pull request Dec 11, 2025
* Add prioritized mode (#1011)
* Add Publish Timeout on Disconnect (#1001)
* Fix test flap (#1012)
* Add test to promote mirrored to regular stream (#1008)
* Add Nats-Expected-Last-Subject-Sequence-Subject (#1007)
* Fix keyed NATS clients with configurations (#1006)
* ensure NatsConnectionPool cannot overflow (#1005)
* Fix stream config adjustments on update (#995)
* Handel Unobserved Exceptions During Connection State Transitions (#999)
* Added verification of the consumer sequence number for pull ordered consumers (#981)
* Fix build warnings (#991)
* Reduce closure allocations (#988)
@mtmk mtmk mentioned this pull request Dec 11, 2025
mtmk added a commit that referenced this pull request Dec 11, 2025
* Add prioritized mode (#1011)
* Add Publish Timeout on Disconnect (#1001)
* Fix test flap (#1012)
* Add test to promote mirrored to regular stream (#1008)
* Add Nats-Expected-Last-Subject-Sequence-Subject (#1007)
* Fix keyed NATS clients with configurations (#1006)
* ensure NatsConnectionPool cannot overflow (#1005)
* Fix stream config adjustments on update (#995)
* Handel Unobserved Exceptions During Connection State Transitions (#999)
* Added verification of the consumer sequence number for pull ordered consumers (#981)
* Fix build warnings (#991)
* Reduce closure allocations (#988)
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.

3 participants