Skip to content

Commit 70a74b1

Browse files
committed
Catch TypeError of non-duplicable objects
In Ruby versions prior to 2.4.x NilClass, Fixnum, Float and Symbol among others where not cloneable/duplicable and threw a TypeError. In newer versions they simply return self. Since rescuing the TypeError is approximately 40x slower than using a predicate to check whether an object is cloneable or not, one could consider to implement a `duplicable?` method on Object and conditionally overwrite that on descendants. See https://stackoverflow.com/questions/49321553/why-is-duplicable-defined-the-way-it-is However, assuming that this code is not part of a hot code path, that seems like an unnecessary performance improvement.
1 parent 677046a commit 70a74b1

File tree

1 file changed

+7
-1
lines changed

1 file changed

+7
-1
lines changed

lib/concurrent/synchronization/abstract_struct.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -117,7 +117,13 @@ def ns_merge(other, &block)
117117

118118
# @!visibility private
119119
def ns_initialize_copy
120-
@values = @values.map(&:clone)
120+
@values = @values.map do |val|
121+
begin
122+
val.clone
123+
rescue TypeError
124+
val
125+
end
126+
end
121127
end
122128

123129
# @!visibility private

0 commit comments

Comments
 (0)