From 00ee0a367c8bcc9562a706fffeeb5da0bda353b7 Mon Sep 17 00:00:00 2001 From: Edmund Kump Date: Tue, 13 Feb 2024 12:10:41 -0500 Subject: [PATCH] remove methods backported to ruby < 2.5. Ruby 2.5 is the MSV for 2.0 --- .rubocop_todo.yml | 1 - lib/datadog/core/backport.rb | 51 ---------------- lib/datadog/core/utils/safe_dup.rb | 60 ++++++++----------- .../tracing/contrib/rack/middlewares.rb | 3 +- lib/datadog/tracing/sampling/rule_sampler.rb | 2 +- sig/datadog/core/backport.rbs | 11 ---- 6 files changed, 26 insertions(+), 102 deletions(-) delete mode 100644 lib/datadog/core/backport.rb delete mode 100644 sig/datadog/core/backport.rbs diff --git a/.rubocop_todo.yml b/.rubocop_todo.yml index 40c5d12d8d..55fcb5ca41 100644 --- a/.rubocop_todo.yml +++ b/.rubocop_todo.yml @@ -42,7 +42,6 @@ Metrics/BlockNesting: Naming/FileName: Exclude: - 'lib/datadog/appsec/autoload.rb' - - 'lib/datadog/core/backport.rb' - 'lib/datadog/opentelemetry/api/trace/span.rb' - 'lib/datadog/opentelemetry/sdk/trace/span.rb' - 'lib/datadog/profiling/load_native_extension.rb' diff --git a/lib/datadog/core/backport.rb b/lib/datadog/core/backport.rb deleted file mode 100644 index 0ebc7809f4..0000000000 --- a/lib/datadog/core/backport.rb +++ /dev/null @@ -1,51 +0,0 @@ -# frozen_string_literal: true - -module Datadog - module Core - # This module is used to provide features from Ruby 2.5+ to older Rubies - module BackportFrom25 - if ::String.method_defined?(:delete_prefix) - def self.string_delete_prefix(string, prefix) - string.delete_prefix(prefix) - end - else - def self.string_delete_prefix(string, prefix) - prefix = prefix.to_s - if string.start_with?(prefix) - string[prefix.length..-1] || raise('rbs-guard: String#[] is non-nil as `prefix` is guaranteed present') - else - string.dup - end - end - end - end - - # This module is used to provide features from Ruby 2.4+ to older Rubies - module BackportFrom24 - if RUBY_VERSION < '2.4' - def self.dup(value) - case value - when NilClass, TrueClass, FalseClass, Numeric - value - else - value.dup - end - end - else - def self.dup(value) - value.dup - end - end - - if ::Hash.method_defined?(:compact!) - def self.hash_compact!(hash) - hash.compact! - end - else - def self.hash_compact!(hash) - hash.reject! { |_key, value| value.nil? } - end - end - end - end -end diff --git a/lib/datadog/core/utils/safe_dup.rb b/lib/datadog/core/utils/safe_dup.rb index abc22bfa69..a55bf79a6c 100644 --- a/lib/datadog/core/utils/safe_dup.rb +++ b/lib/datadog/core/utils/safe_dup.rb @@ -1,49 +1,37 @@ # frozen_string_literal: true -require_relative '../backport' - module Datadog module Core module Utils # Helper methods for safer dup module SafeDup # String#+@ was introduced in Ruby 2.3 - if String.method_defined?(:+@) && String.method_defined?(:-@) - def self.frozen_or_dup(v) - # For the case of a String we use the methods +@ and -@. - # Those methods are only for String objects - # they are faster and chepaer on the memory side. - # Check the benchmark on - # https://github.com/DataDog/dd-trace-rb/pull/2704 - if v.is_a?(String) - # If the string is not frozen, the +(-v) will: - # - first create a frozen deduplicated copy with -v - # - then it will dup it more efficiently with +v - v.frozen? ? v : +(-v) - else - v.frozen? ? v : Core::BackportFrom24.dup(v) - end - end - - def self.frozen_dup(v) - # For the case of a String we use the methods -@ - # That method are only for String objects - # they are faster and chepaer on the memory side. - # Check the benchmark on - # https://github.com/DataDog/dd-trace-rb/pull/2704 - if v.is_a?(String) - -v if v - else - v.frozen? ? v : Core::BackportFrom24.dup(v).freeze - end - end - else - def self.frozen_or_dup(v) - v.frozen? ? v : Core::BackportFrom24.dup(v) + def self.frozen_or_dup(v) + # For the case of a String we use the methods +@ and -@. + # Those methods are only for String objects + # they are faster and chepaer on the memory side. + # Check the benchmark on + # https://github.com/DataDog/dd-trace-rb/pull/2704 + if v.is_a?(String) + # If the string is not frozen, the +(-v) will: + # - first create a frozen deduplicated copy with -v + # - then it will dup it more efficiently with +v + v.frozen? ? v : +(-v) + else + v.frozen? ? v : v.dup end + end - def self.frozen_dup(v) - v.frozen? ? v : Core::BackportFrom24.dup(v).freeze + def self.frozen_dup(v) + # For the case of a String we use the methods -@ + # That method are only for String objects + # they are faster and chepaer on the memory side. + # Check the benchmark on + # https://github.com/DataDog/dd-trace-rb/pull/2704 + if v.is_a?(String) + -v if v + else + v.frozen? ? v : v.dup.freeze end end end diff --git a/lib/datadog/tracing/contrib/rack/middlewares.rb b/lib/datadog/tracing/contrib/rack/middlewares.rb index de888a28b4..6a239151d6 100644 --- a/lib/datadog/tracing/contrib/rack/middlewares.rb +++ b/lib/datadog/tracing/contrib/rack/middlewares.rb @@ -3,7 +3,6 @@ require 'date' require_relative '../../../core/environment/variable_helpers' -require_relative '../../../core/backport' require_relative '../../../core/remote/tie/tracing' require_relative '../../client_ip' require_relative '../../metadata/ext' @@ -252,7 +251,7 @@ def parse_url(env, original_env) else # normally REQUEST_URI starts at the path, but it # might contain the full URL in some cases (e.g WEBrick) - Datadog::Core::BackportFrom25.string_delete_prefix(request_uri, base_url) + request_uri.delete_prefix(base_url) end base_url + fullpath diff --git a/lib/datadog/tracing/sampling/rule_sampler.rb b/lib/datadog/tracing/sampling/rule_sampler.rb index d290d1bcc8..15a3ab86b6 100644 --- a/lib/datadog/tracing/sampling/rule_sampler.rb +++ b/lib/datadog/tracing/sampling/rule_sampler.rb @@ -65,7 +65,7 @@ def self.parse(rules, rate_limit, default_sample_rate) sample_rate: sample_rate, } - Core::BackportFrom24.hash_compact!(kwargs) + kwargs.compact! SimpleRule.new(**kwargs) end diff --git a/sig/datadog/core/backport.rbs b/sig/datadog/core/backport.rbs deleted file mode 100644 index 586e6fa58d..0000000000 --- a/sig/datadog/core/backport.rbs +++ /dev/null @@ -1,11 +0,0 @@ -module Datadog - module Core - module BackportFrom25 - def self.string_delete_prefix: (::String string, ::String prefix) -> ::String - end - - module BackportFrom24 - def self.dup: (Object value) -> Object - end - end -end