Skip to content

Commit e350409

Browse files
swalkinshawclaude
andcommitted
Cache unwrap and to_type_signature on type wrappers
Type wrappers (NonNull, List) are schema-level immutable objects whose unwrap and to_type_signature results never change. Memoize both to eliminate repeated recursive unwrap calls and string allocations during validation and introspection. Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 680b1d7 commit e350409

3 files changed

Lines changed: 9 additions & 3 deletions

File tree

lib/graphql/schema/list.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def list?
1919
end
2020

2121
def to_type_signature
22-
"[#{@of_type.to_type_signature}]"
22+
@type_signature ||= -"[#{@of_type.to_type_signature}]"
2323
end
2424

2525
# This is for introspection, where it's expected the name will be `null`

lib/graphql/schema/non_null.rb

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ def list?
2424
end
2525

2626
def to_type_signature
27-
"#{@of_type.to_type_signature}!"
27+
@type_signature ||= -"#{@of_type.to_type_signature}!"
2828
end
2929

3030
def inspect

lib/graphql/schema/wrapper.rb

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,13 @@ def initialize(of_type)
1313
end
1414

1515
def unwrap
16-
@of_type.unwrap
16+
@unwrapped ||= @of_type.unwrap
17+
end
18+
19+
def freeze
20+
unwrap
21+
to_type_signature
22+
super
1723
end
1824

1925
def ==(other)

0 commit comments

Comments
 (0)