Skip to content

Commit 6bc88d1

Browse files
committed
(PUP-7301) Speed up PTupleType and PVariantType instance checking
This commit provides minor, but measurable performance improvements to the methods `PTupleType#instance?` and `PVariantType#really_instance?`.
1 parent 3f8764f commit 6bc88d1

File tree

1 file changed

+10
-10
lines changed

1 file changed

+10
-10
lines changed

lib/puppet/pops/types/types.rb

+10-10
Original file line numberDiff line numberDiff line change
@@ -2039,14 +2039,15 @@ def resolve(type_parser, loader)
20392039

20402040
def instance?(o, guard = nil)
20412041
return false unless o.is_a?(Array)
2042-
# compute the tuple's min/max size, and check if that size matches
2043-
size_t = size_type || PIntegerType.new(*size_range)
2044-
2045-
return false unless size_t.instance?(o.size, guard)
2046-
o.each_with_index do |element, index|
2047-
return false unless (types[index] || types[-1]).instance?(element, guard)
2042+
if @size_type
2043+
return false unless @size_type.instance?(o.size, guard)
2044+
else
2045+
return false unless @types.empty? || @types.size == o.size
2046+
end
2047+
index = -1
2048+
@types.empty? || o.all? do |element|
2049+
@types.fetch(index += 1) { @types.last }.instance?(element, guard)
20482050
end
2049-
true
20502051
end
20512052

20522053
def iterable?(guard = nil)
@@ -2757,10 +2758,9 @@ def instance?(o, guard = nil)
27572758
end
27582759

27592760
def really_instance?(o, guard = nil)
2760-
@types.inject(-1) do |memo, type|
2761+
@types.reduce(-1) do |memo, type|
27612762
ri = type.really_instance?(o, guard)
2762-
memo = ri if ri > memo
2763-
memo
2763+
ri > memo ? ri : memo
27642764
end
27652765
end
27662766

0 commit comments

Comments
 (0)