Skip to content

Conversation

@danmayer
Copy link

@danmayer danmayer commented Feb 14, 2025

Ok, this is a bit silly, but after our conversation I was curious how much we could reduce allocations in the get multi tight loop... This does reduce allocations which is nice, but the overall benchmark doesn't show much of a difference... Still kind of fun to poke around at, and it might be nice to add a check to CI that ensures we keep pushing allocations down for our commands.

allocations before: 1944
allocations after: 1659

benchmark before:

❯❯❯$ BENCH_TARGET=get_multi RUBY_YJIT_ENABLE=1 bundle exec bin/benchmark  
  yjit: true  
  ruby 3.3.4 (2024-07-09 revision be1089c8ec) +YJIT [arm64-darwin23]  
  Warming up --------------------------------------  
          get 100 keys    68.000 i/100ms  
  get 100 keys raw sock  
                          84.000 i/100ms  
  Calculating -------------------------------------  
          get 100 keys    581.042 (±13.3%) i/s    (1.72 ms/i) -      5.780k in  10.118846s  
  get 100 keys raw sock  
                          685.477 (±18.5%) i/s    (1.46 ms/i) -      6.636k in  10.014161s  
  Comparison:  
  get 100 keys raw sock:      685.5 i/s  
          get 100 keys:      581.0 i/s - same-ish: difference falls within error

benchmark after:

❯❯❯$ BENCH_TARGET=get_multi RUBY_YJIT_ENABLE=1 bundle exec bin/benchmark 
yjit: true
ruby 3.3.4 (2024-07-09 revision be1089c8ec) +YJIT [arm64-darwin23]
Warming up --------------------------------------
        get 100 keys    67.000 i/100ms
get 100 keys raw sock
                        89.000 i/100ms
Calculating -------------------------------------
        get 100 keys    633.133 (±14.2%) i/s    (1.58 ms/i) -      6.164k in  10.010311s
get 100 keys raw sock
                        760.185 (±21.7%) i/s    (1.32 ms/i) -      7.209k in  10.066808s
Comparison:
get 100 keys raw sock:      760.2 i/s
        get 100 keys:      633.1 i/s - same-ish: difference falls within error

paired with Aaron and was able to cut allocations in half... while it still doesn't show up as much in a micro benchmark this should add up across all cache calls across all web requests. The new method is a bit harder to understand, but feels worth it for such a frequently used tight loop.

before: 1944
after: 851

@danmayer danmayer requested a review from tenderworks February 14, 2025 19:57
Copy link

@tenderworks tenderworks left a comment

Choose a reason for hiding this comment

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

Only a couple comments, but nothing that should block this PR as-is.

@danmayer
Copy link
Author

Note: this is the optimized get multi only use for single server with raw mode... we should run tests in deployed systems (staging) before merging

def read_data(data_size)
resp_data = @io_source.read(data_size)
@io_source.read(TERMINATOR.bytesize)
@io_source.read_to_outstring(TERMINATOR.bytesize, @terminator_buffer)
Copy link
Author

Choose a reason for hiding this comment

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

this removes an additional allocation for all the non multi calls

…what is supported for simplified fast get multi
@danmayer
Copy link
Author

OK, I finally had some time to capture data on this. Setting up two staging environments and running 1:1 traffic between them. The differences are small but seem to be better across all metrics with this change. Reduced object allocations, CPU, and request time. With no increases in errors.

Data available in internal drive "Dalli Object Allocations"

alias multi? quiet?

# NOTE: Additional public methods should be overridden in Dalli::Threadsafe
ALLOWED_QUIET_OPS = %i[add replace set delete incr decr append prepend flush noop].freeze

Choose a reason for hiding this comment

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

I'm not understanding the reason for making this public (at least as part of this PR)?

Copy link
Author

Choose a reason for hiding this comment

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

rubocop update complained about private constant.

class Meta < Base # rubocop:disable Metrics/ClassLength
TERMINATOR = "\r\n"
META_NOOP = "mn\r\n"
META_NOOP_RESP = 'MN'

Choose a reason for hiding this comment

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

Where's this used?

Copy link
Author

Choose a reason for hiding this comment

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

ah these got refactored away, I can remove them

TERMINATOR = "\r\n"
META_NOOP = "mn\r\n"
META_NOOP_RESP = 'MN'
META_VALUE_RESP = 'VA'

Choose a reason for hiding this comment

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

Same question as above

@grcooper grcooper removed their request for review July 11, 2025 14:04
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