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

Perf improvement on Logger::Formatter#call #74

Merged
merged 1 commit into from
May 5, 2022

Conversation

amatsuda
Copy link
Member

@amatsuda amatsuda commented May 5, 2022

Here's a tiny performance improvement that suppresses an extra Array object creation on the Logger::Formatter per each call.

Just in case anyone needs a benchmark, here's a micro benchmark/ips result on String#[0..0] vs String#[0, 1].

require 'benchmark/ips'

Benchmark.ips do |x|
  string = 'debug'

  x.report('[0..0]') { string[0..0] }
  x.report('[0, 1]') { string[0, 1] }

  x.compare!
end
Warming up --------------------------------------
              [0..0]     1.013M i/100ms
              [0, 1]     1.097M i/100ms
Calculating -------------------------------------
              [0..0]     10.145M (± 0.7%) i/s -     51.666M in   5.093090s
              [0, 1]     10.946M (± 0.7%) i/s -     54.835M in   5.009652s

Comparison:
              [0, 1]: 10946494.4 i/s
              [0..0]: 10144924.5 i/s - 1.08x  (± 0.00) slower

[0..0] internally creates an extra Array object, and so is slower and much more memory consuming
@sonots sonots merged commit cafbc35 into ruby:master May 5, 2022
@sonots
Copy link
Member

sonots commented May 5, 2022

thanks

@amatsuda amatsuda deleted the sevirity_first_perf branch May 5, 2022 12:10
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Development

Successfully merging this pull request may close these issues.

2 participants