Skip to content

Commit 10e025e

Browse files
committed
perf: use shallow dup instead of deep_dup for Scope and BreadcrumbBuffer
⚠️ Needs closer review — changes copy semantics for scope containers. Replace deep_dup with dup for Scope's contexts, extra, tags, user, and fingerprint hashes, and for BreadcrumbBuffer's buffer array. Rationale: These containers are not mutated in-place after duplication. Scope methods like set_tags, set_extras, set_user all use assignment (replacing the entire hash) or merge! on the copy's own hash. The inner values (strings, numbers, symbols) are immutable or treated as such. BreadcrumbBuffer: Individual Breadcrumb objects in the buffer are not mutated after being recorded — they are only read during serialization. A shallow dup of the buffer array is sufficient to prevent the copy from seeing new breadcrumbs added to the original. span, session, and propagation_context still use deep_dup as they contain mutable nested state that may be modified in-place.
1 parent ebb05d6 commit 10e025e

2 files changed

Lines changed: 8 additions & 6 deletions

File tree

sentry-ruby/lib/sentry/breadcrumb_buffer.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -57,7 +57,7 @@ def to_h
5757
# @return [BreadcrumbBuffer]
5858
def dup
5959
copy = super
60-
copy.buffer = buffer.deep_dup
60+
copy.buffer = buffer.dup
6161
copy
6262
end
6363
end

sentry-ruby/lib/sentry/scope.rb

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -124,13 +124,15 @@ def clear_breadcrumbs
124124
def dup
125125
copy = super
126126
copy.breadcrumbs = breadcrumbs.dup
127-
copy.contexts = contexts.deep_dup
128-
copy.extra = extra.deep_dup
129-
copy.tags = tags.deep_dup
130-
copy.user = user.deep_dup
127+
# Shallow dup is sufficient for these containers — inner values are not
128+
# mutated after scope duplication, only replaced via merge! or assignment
129+
copy.contexts = contexts.dup
130+
copy.extra = extra.dup
131+
copy.tags = tags.dup
132+
copy.user = user.dup
131133
copy.transaction_name = transaction_name.dup
132134
copy.transaction_source = transaction_source.dup
133-
copy.fingerprint = fingerprint.deep_dup
135+
copy.fingerprint = fingerprint.dup
134136
copy.span = span.deep_dup
135137
copy.session = session.deep_dup
136138
copy.propagation_context = propagation_context.deep_dup

0 commit comments

Comments
 (0)