From 153051ab620faf819603c5974a5b22b02154e307 Mon Sep 17 00:00:00 2001 From: Eric Proulx Date: Sat, 14 Dec 2019 23:04:55 +0100 Subject: [PATCH 1/3] Added request_method and requirements has methods instead using method_missing since it's defined by defaults. Added regexp and index in Any class instead of falling back on method_missing --- CHANGELOG.md | 1 + lib/grape/router.rb | 7 +++++-- lib/grape/router/attribute_translator.rb | 26 ++++++++++++++++-------- 3 files changed, 24 insertions(+), 10 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index c9eb3604f..0294c6fba 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,6 +3,7 @@ #### Features * Your contribution here. +* [#1943](https://github.com/ruby-grape/grape/pull/1944): Reduced attribute_translator string allocations - [@ericproulx](https://github.com/ericproulx). * [#1942](https://github.com/ruby-grape/grape/pull/1942): Optimized retained memory methods - [@ericproulx](https://github.com/ericproulx). * [#1941](https://github.com/ruby-grape/grape/pull/1941): Frozen string literal - [@ericproulx](https://github.com/ericproulx). * [#1940](https://github.com/ruby-grape/grape/pull/1940): Get rid of a needless step in HashWithIndifferentAccess - [@dnesteryuk](https://github.com/dnesteryuk). diff --git a/lib/grape/router.rb b/lib/grape/router.rb index 970699004..509c1a273 100644 --- a/lib/grape/router.rb +++ b/lib/grape/router.rb @@ -7,8 +7,11 @@ class Router attr_reader :map, :compiled class Any < AttributeTranslator - def initialize(pattern, **attributes) + attr_reader :pattern, :regexp, :index + def initialize(pattern, regexp, index, **attributes) @pattern = pattern + @regexp = regexp + @index = index super(attributes) end end @@ -51,7 +54,7 @@ def append(route) def associate_routes(pattern, **options) regexp = /(?<_#{@neutral_map.length}>)#{pattern.to_regexp}/ - @neutral_map << Any.new(pattern, regexp: regexp, index: @neutral_map.length, **options) + @neutral_map << Any.new(pattern, regexp, @neutral_map.length, **options) end def call(env) diff --git a/lib/grape/router/attribute_translator.rb b/lib/grape/router/attribute_translator.rb index aa66662ff..a445da6c0 100644 --- a/lib/grape/router/attribute_translator.rb +++ b/lib/grape/router/attribute_translator.rb @@ -4,31 +4,41 @@ module Grape class Router # this could be an OpenStruct, but doesn't work in Ruby 2.3.0, see https://bugs.ruby-lang.org/issues/12251 class AttributeTranslator + attr_reader :attributes, :request_method, :requirements + def initialize(attributes = {}) @attributes = attributes + @request_method = attributes.delete(:request_method) + @requirements = attributes.delete(:requirements) end def to_h - @attributes + attributes.merge(request_method: request_method).tap do |attr| + attr[:requirements] = requirements if requirements + end end - def method_missing(m, *args) - if m[-1] == '=' - @attributes[m[0..-1]] = *args - elsif m[-1] != '=' - @attributes[m] + def method_missing(method_name, *args) # rubocop:disable Style/MethodMissing + if setter?(method_name[-1]) + attributes[method_name[0..-1]] = *args else - super + attributes[method_name] end end def respond_to_missing?(method_name, _include_private = false) - if method_name[-1] == '=' + if setter?(method_name[-1]) true else @attributes.key?(method_name) end end + + private + + def setter?(method_name) + method_name[-1] == '=' + end end end end From 81978af15091dafe18f133e91b397cdb875c9f56 Mon Sep 17 00:00:00 2001 From: Eric Proulx Date: Sat, 14 Dec 2019 23:22:29 +0100 Subject: [PATCH 2/3] Change changelog number --- CHANGELOG.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 0294c6fba..7721c0577 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -3,7 +3,7 @@ #### Features * Your contribution here. -* [#1943](https://github.com/ruby-grape/grape/pull/1944): Reduced attribute_translator string allocations - [@ericproulx](https://github.com/ericproulx). +* [#1944](https://github.com/ruby-grape/grape/pull/1944): Reduced attribute_translator string allocations - [@ericproulx](https://github.com/ericproulx). * [#1942](https://github.com/ruby-grape/grape/pull/1942): Optimized retained memory methods - [@ericproulx](https://github.com/ericproulx). * [#1941](https://github.com/ruby-grape/grape/pull/1941): Frozen string literal - [@ericproulx](https://github.com/ericproulx). * [#1940](https://github.com/ruby-grape/grape/pull/1940): Get rid of a needless step in HashWithIndifferentAccess - [@dnesteryuk](https://github.com/dnesteryuk). From 3bc7643a957ddbfba35b75f5e7e46a0f5dd7dd08 Mon Sep 17 00:00:00 2001 From: Eric Proulx Date: Sun, 15 Dec 2019 20:21:05 +0100 Subject: [PATCH 3/3] Removed deletion from input arg --- lib/grape/router/attribute_translator.rb | 8 +++----- 1 file changed, 3 insertions(+), 5 deletions(-) diff --git a/lib/grape/router/attribute_translator.rb b/lib/grape/router/attribute_translator.rb index a445da6c0..ad2d03855 100644 --- a/lib/grape/router/attribute_translator.rb +++ b/lib/grape/router/attribute_translator.rb @@ -8,14 +8,12 @@ class AttributeTranslator def initialize(attributes = {}) @attributes = attributes - @request_method = attributes.delete(:request_method) - @requirements = attributes.delete(:requirements) + @request_method = attributes[:request_method] + @requirements = attributes[:requirements] end def to_h - attributes.merge(request_method: request_method).tap do |attr| - attr[:requirements] = requirements if requirements - end + attributes end def method_missing(method_name, *args) # rubocop:disable Style/MethodMissing