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

Use Array#to_h instead of Hash[array] #1523

Merged
merged 4 commits into from
May 27, 2021

Conversation

marcotc
Copy link
Member

@marcotc marcotc commented May 26, 2021

This PR uses Array#to_h, instead of Hash[array], as this method is available since Ruby 2.1.

Array#to_h has cleaner syntax, is much faster with empty arrays, and is slightly faster with populated arrays:

require 'benchmark/ips'

arr = []
a16 = 16.times.map{|i|[i.to_s.to_sym, 'test']}

Benchmark.ips do |x|
  x.config(time: 2, warmup: 0.1) if x.respond_to? :config

  x.report('empty array: Hash[A]') { Hash[arr] }
  x.report('empty array: A.to_h ') { arr.to_h }
  x.report('16 elements: Hash[A]') { Hash[a16] }
  x.report('16 elements: A.to_h ') { a16.to_h }

  x.compare!
end

# empty array: A.to_h : 13333116.0 i/s
# empty array: Hash[A]:  8432958.2 i/s - 1.58x  (± 0.00) slower
# 16 elements: A.to_h :   770205.8 i/s - 17.31x  (± 0.00) slower
# 16 elements: Hash[A]:   697552.1 i/s - 19.11x  (± 0.00) slower

Ruby 2.0 deprecation checklist

  • Remove 2.0 from CI
  • Remove 2.0-specific code paths
  • Update to 2.1+ standards: https://github.com/ruby/ruby/blob/v2_1_0/NEWS
    • Module#include and Module#prepend are now public methods.
    • Exception#cause (no changes)
    • Array#to_h converts an array of key-value pairs into a Hash.
    • Kernel#singleton_method
    • Process.clock_gettime
    • Enumerable#to_h converts a list of key-value pairs into a Hash.
    • Now the default values of keyword arguments can be omitted. Those “required keyword arguments” need giving explicitly at the call time.
  • Update documentation

@marcotc marcotc added the deprecation Involves a deprecation label May 26, 2021
@marcotc marcotc self-assigned this May 26, 2021
@marcotc marcotc requested a review from a team May 26, 2021 23:03
@marcotc marcotc force-pushed the deprecate-ruby-2.0-exception-cause branch from 49e0223 to 6e3fd4f Compare May 26, 2021 23:12
@codecov-commenter
Copy link

Codecov Report

Merging #1523 (6e3fd4f) into master (2dcc26b) will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #1523   +/-   ##
=======================================
  Coverage   98.22%   98.22%           
=======================================
  Files         862      862           
  Lines       41720    41721    +1     
=======================================
+ Hits        40978    40979    +1     
  Misses        742      742           
Impacted Files Coverage Δ
lib/ddtrace/configuration/base.rb 97.61% <ø> (-0.06%) ⬇️
lib/ddtrace/configuration/settings.rb 100.00% <100.00%> (ø)
lib/ddtrace/diagnostics/environment_logger.rb 98.05% <100.00%> (+0.01%) ⬆️
lib/ddtrace/runtime/metrics.rb 96.36% <100.00%> (+0.06%) ⬆️
lib/ddtrace/tracer.rb 94.64% <100.00%> (ø)

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update 2dcc26b...6e3fd4f. Read the comment docs.

ivoanjo
ivoanjo previously approved these changes May 27, 2021
Copy link
Member

@ivoanjo ivoanjo left a comment

Choose a reason for hiding this comment

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

LGTM!

You probably also want to re-enable Style/HashConversion in .rubocop.yml.

@marcotc marcotc merged commit cdd9270 into master May 27, 2021
@marcotc marcotc deleted the deprecate-ruby-2.0-exception-cause branch May 27, 2021 18:19
@github-actions github-actions bot added this to the 0.50.0 milestone May 27, 2021
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
deprecation Involves a deprecation
Projects
Active work
  
Awaiting triage
Development

Successfully merging this pull request may close these issues.

None yet

3 participants