Sha256: 3a727a7c84cf70a85107c16d9ea37d2a0df56d18910feed9791d064083a5e9e9

Contents?: true

Size: 1.64 KB

Versions: 17

Compression:

Stored size: 1.64 KB

Contents

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)
          end

          def self.frozen_dup(v)
            v.frozen? ? v : Core::BackportFrom24.dup(v).freeze
          end
        end
      end
    end
  end
end

Version data entries

17 entries across 17 versions & 1 rubygems

Version Path
ddtrace-1.23.3 lib/datadog/core/utils/safe_dup.rb
ddtrace-1.23.2 lib/datadog/core/utils/safe_dup.rb
ddtrace-1.23.1 lib/datadog/core/utils/safe_dup.rb
ddtrace-1.22.0 lib/datadog/core/utils/safe_dup.rb
ddtrace-1.21.1 lib/datadog/core/utils/safe_dup.rb
ddtrace-1.21.0 lib/datadog/core/utils/safe_dup.rb
ddtrace-1.20.0 lib/datadog/core/utils/safe_dup.rb
ddtrace-1.19.0 lib/datadog/core/utils/safe_dup.rb
ddtrace-1.18.0 lib/datadog/core/utils/safe_dup.rb
ddtrace-1.17.0 lib/datadog/core/utils/safe_dup.rb
ddtrace-1.16.2 lib/datadog/core/utils/safe_dup.rb
ddtrace-1.16.1 lib/datadog/core/utils/safe_dup.rb
ddtrace-1.16.0 lib/datadog/core/utils/safe_dup.rb
ddtrace-1.15.0 lib/datadog/core/utils/safe_dup.rb
ddtrace-1.14.0 lib/datadog/core/utils/safe_dup.rb
ddtrace-1.13.1 lib/datadog/core/utils/safe_dup.rb
ddtrace-1.13.0 lib/datadog/core/utils/safe_dup.rb