Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

chunk: add fast path to join chunk data #4765

Merged
merged 1 commit into from
Jan 14, 2025
Merged

Conversation

Watson1978
Copy link
Contributor

Which issue(s) this PR fixes:
Fixes #

What this PR does / why we need it:
This PR will add fast path to join chunk data using Array#join.
Array#join is faster and consumes less memory.

  • verify
require 'bundler/inline'
gemfile do
  source 'https://rubygems.org'
  gem 'benchmark-ips'
  gem 'benchmark-memory'
end

def benchmarks(x, ary)
  x.report("b") {
    adding = ''.b
    ary.each do |a|
      adding << a.b
    end
  }

  x.report("join") {
    ary.join.force_encoding(Encoding::ASCII_8BIT)
  }
end

ary = ('あ'..'ん').to_a

Benchmark.ips do |x|
  benchmarks(x, ary)
  x.compare!
end

Benchmark.memory do |x|
  benchmarks(x, ary)
end
ruby 3.4.1 (2024-12-25 revision 48d4efcb85) +PRISM [x86_64-linux]
Warming up --------------------------------------
                   b    15.571k i/100ms
                join    94.632k i/100ms
Calculating -------------------------------------
                   b    153.486k (± 0.9%) i/s    (6.52 μs/i) -    778.550k in   5.072851s
                join    910.273k (± 2.1%) i/s    (1.10 μs/i) -      4.637M in   5.096382s

Comparison:
                join:   910273.4 i/s
                   b:   153486.4 i/s - 5.93x  slower

Calculating -------------------------------------
                   b     3.616k memsize (     0.000  retained)
                        84.000  objects (     0.000  retained)
                        50.000  strings (     0.000  retained)
                join   320.000  memsize (     0.000  retained)
                         1.000  objects (     0.000  retained)
                         1.000  strings (     0.000  retained)

Docs Changes:

Release Note:

@Watson1978 Watson1978 requested a review from daipom January 10, 2025 05:53
Copy link
Contributor

@daipom daipom left a comment

Choose a reason for hiding this comment

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

LGTM. Thanks!

@daipom daipom added this to the v1.19.0 milestone Jan 14, 2025
@daipom daipom merged commit 6df9954 into fluent:master Jan 14, 2025
13 checks passed
@Watson1978 Watson1978 deleted the join branch January 14, 2025 02:42
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