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 +@ instead of dup for duplicating strings #2704

Merged
merged 3 commits into from
Apr 11, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
22 changes: 20 additions & 2 deletions lib/datadog/core/utils/safe_dup.rb
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,26 @@ def dup
using RefineNil
end

def self.frozen_or_dup(v)
v.frozen? ? v : v.dup
# String#+@ was introduced in Ruby 2.3
if String.method_defined?(:+@) && String.method_defined?(:-@)
def self.frozen_or_dup(v)
# 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)
Copy link
Member

Choose a reason for hiding this comment

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

@nirebu, can you leave a comment here explaining why +(-v) is being done here? I think this will raise questions in the future when someone stumbles upon this line.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@marcotc added!

end

def self.frozen_dup(v)
-v if v
end
else
def self.frozen_or_dup(v)
v.frozen? ? v : v.dup
end

def self.frozen_dup(v)
v.frozen? ? v : v.dup.freeze
end
end
end
end
Expand Down
20 changes: 10 additions & 10 deletions lib/datadog/tracing/correlation.rb
Original file line number Diff line number Diff line change
Expand Up @@ -48,18 +48,18 @@ def initialize(
version: nil
)
# Dup and freeze strings so they aren't modified by reference.
@env = Core::Utils::SafeDup.frozen_or_dup(env || Datadog.configuration.env).freeze
@service = Core::Utils::SafeDup.frozen_or_dup(service || Datadog.configuration.service).freeze
@env = Core::Utils::SafeDup.frozen_dup(env || Datadog.configuration.env)
@service = Core::Utils::SafeDup.frozen_dup(service || Datadog.configuration.service)
@span_id = span_id || 0
@span_name = Core::Utils::SafeDup.frozen_or_dup(span_name).freeze
@span_resource = Core::Utils::SafeDup.frozen_or_dup(span_resource).freeze
@span_service = Core::Utils::SafeDup.frozen_or_dup(span_service).freeze
@span_type = Core::Utils::SafeDup.frozen_or_dup(span_type).freeze
@span_name = Core::Utils::SafeDup.frozen_dup(span_name)
@span_resource = Core::Utils::SafeDup.frozen_dup(span_resource)
@span_service = Core::Utils::SafeDup.frozen_dup(span_service)
@span_type = Core::Utils::SafeDup.frozen_dup(span_type)
@trace_id = trace_id || 0
@trace_name = Core::Utils::SafeDup.frozen_or_dup(trace_name).freeze
@trace_resource = Core::Utils::SafeDup.frozen_or_dup(trace_resource).freeze
@trace_service = Core::Utils::SafeDup.frozen_or_dup(trace_service).freeze
@version = Core::Utils::SafeDup.frozen_or_dup(version || Datadog.configuration.version).freeze
@trace_name = Core::Utils::SafeDup.frozen_dup(trace_name)
@trace_resource = Core::Utils::SafeDup.frozen_dup(trace_resource)
@trace_service = Core::Utils::SafeDup.frozen_dup(trace_service)
@version = Core::Utils::SafeDup.frozen_dup(version || Datadog.configuration.version)
end

def to_log_format
Expand Down