From cdfac6bf79f5641476ea3ac0f151642bdec5dcf8 Mon Sep 17 00:00:00 2001 From: Robert Mosolgo Date: Tue, 13 Feb 2018 14:57:08 -0500 Subject: [PATCH 1/3] Use a specialized hash for no-typed-children --- lib/graphql/internal_representation/node.rb | 23 +++++++-------------- 1 file changed, 8 insertions(+), 15 deletions(-) diff --git a/lib/graphql/internal_representation/node.rb b/lib/graphql/internal_representation/node.rb index 3cd28311fd..3fe941ac8a 100644 --- a/lib/graphql/internal_representation/node.rb +++ b/lib/graphql/internal_representation/node.rb @@ -8,23 +8,16 @@ class Node # A specialized, reusable object for leaf nodes. # Behaves like a Hash, but doesn't copy itself. # @api private - class NoTypedChildren - CHILDREN = {}.freeze - def dup; self; end - def any?; false; end - def none?; true; end - def [](key); CHILDREN; end - def each; end - - # Compatibility for when this was an Array: - def method_missing(method_name, *args, &block) - if CHILDREN.respond_to?(method_name) - CHILDREN.send(method_name, *args, &block) - else - raise NotImplementedError - end + class NoTypedChildren < Hash + def initialize + super + freeze end + CHILDREN = [].freeze + def [](k); CHILDREN; end + def dup; self; end end + NO_TYPED_CHILDREN = NoTypedChildren.new # @return [String] the name this node has in the response From 689cf1ace4f7049adf7719fc49885aeca54a14f2 Mon Sep 17 00:00:00 2001 From: Robert Mosolgo Date: Tue, 13 Feb 2018 15:03:07 -0500 Subject: [PATCH 2/3] Use a special hash instance --- lib/graphql/internal_representation/node.rb | 15 ++------------- 1 file changed, 2 insertions(+), 13 deletions(-) diff --git a/lib/graphql/internal_representation/node.rb b/lib/graphql/internal_representation/node.rb index 3fe941ac8a..c27dd30a1f 100644 --- a/lib/graphql/internal_representation/node.rb +++ b/lib/graphql/internal_representation/node.rb @@ -6,19 +6,8 @@ class Node DEFAULT_TYPED_CHILDREN = Proc.new { |h, k| h[k] = {} } # A specialized, reusable object for leaf nodes. - # Behaves like a Hash, but doesn't copy itself. - # @api private - class NoTypedChildren < Hash - def initialize - super - freeze - end - CHILDREN = [].freeze - def [](k); CHILDREN; end - def dup; self; end - end - - NO_TYPED_CHILDREN = NoTypedChildren.new + NO_TYPED_CHILDREN = Hash.new([].freeze).freeze + def NO_TYPED_CHILDREN.dup; self; end; # @return [String] the name this node has in the response attr_reader :name From 510adc5f8b6fc4c208cf9aab72b1ade41dd95566 Mon Sep 17 00:00:00 2001 From: Robert Mosolgo Date: Tue, 13 Feb 2018 15:03:54 -0500 Subject: [PATCH 3/3] Freeze _after_ modifying the object, ya dummy --- lib/graphql/internal_representation/node.rb | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/lib/graphql/internal_representation/node.rb b/lib/graphql/internal_representation/node.rb index c27dd30a1f..24d617380f 100644 --- a/lib/graphql/internal_representation/node.rb +++ b/lib/graphql/internal_representation/node.rb @@ -6,8 +6,9 @@ class Node DEFAULT_TYPED_CHILDREN = Proc.new { |h, k| h[k] = {} } # A specialized, reusable object for leaf nodes. - NO_TYPED_CHILDREN = Hash.new([].freeze).freeze + NO_TYPED_CHILDREN = Hash.new([].freeze) def NO_TYPED_CHILDREN.dup; self; end; + NO_TYPED_CHILDREN.freeze # @return [String] the name this node has in the response attr_reader :name