Skip to content

Commit 0e05af7

Browse files
authored
Concat and Uniq only when needed (#2020)
1 parent 8deebb5 commit 0e05af7

File tree

4 files changed

+18
-9
lines changed

4 files changed

+18
-9
lines changed

CHANGELOG.md

+1
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
#### Features
44
* Your contribution here.
5+
* [#2020](https://github.com/ruby-grape/grape/pull/2020): Reduce array allocation - [@ericproulx](https://github.com/ericproulx).
56
* [#2015](https://github.com/ruby-grape/grape/pull/2014): Reduce MatchData allocation - [@ericproulx](https://github.com/ericproulx).
67
* [#2014](https://github.com/ruby-grape/grape/pull/2014): Reduce total allocated arrays - [@ericproulx](https://github.com/ericproulx).
78
* [#2011](https://github.com/ruby-grape/grape/pull/2011): Reduce total retained regexes - [@ericproulx](https://github.com/ericproulx).

lib/grape/api/instance.rb

+7-3
Original file line numberDiff line numberDiff line change
@@ -243,9 +243,13 @@ def add_head_not_allowed_methods_and_options_methods
243243
# Generate a route that returns an HTTP 405 response for a user defined
244244
# path on methods not specified
245245
def generate_not_allowed_method(pattern, allowed_methods: [], **attributes)
246-
not_allowed_methods = %w[GET PUT POST DELETE PATCH HEAD] - allowed_methods
247-
not_allowed_methods << Grape::Http::Headers::OPTIONS if self.class.namespace_inheritable(:do_not_route_options)
248-
246+
supported_methods =
247+
if self.class.namespace_inheritable(:do_not_route_options)
248+
Grape::Http::Headers::SUPPORTED_METHODS
249+
else
250+
Grape::Http::Headers::SUPPORTED_METHODS_WITHOUT_OPTIONS
251+
end
252+
not_allowed_methods = supported_methods - allowed_methods
249253
return if not_allowed_methods.empty?
250254

251255
@router.associate_routes(pattern, not_allowed_methods: not_allowed_methods, **attributes)

lib/grape/http/headers.rb

+1
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,7 @@ module Headers
2121
OPTIONS = 'OPTIONS'
2222

2323
SUPPORTED_METHODS = [GET, POST, PUT, PATCH, DELETE, HEAD, OPTIONS].freeze
24+
SUPPORTED_METHODS_WITHOUT_OPTIONS = Grape::Util::LazyObject.new { [GET, POST, PUT, PATCH, DELETE, HEAD].freeze }
2425

2526
HTTP_ACCEPT_VERSION = 'HTTP_ACCEPT_VERSION'
2627
X_CASCADE = 'X-Cascade'

lib/grape/util/base_inheritable.rb

+9-6
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,7 @@ module Util
55
# Base for classes which need to operate with own values kept
66
# in the hash and inherited values kept in a Hash-like object.
77
class BaseInheritable
8-
attr_accessor :inherited_values
9-
attr_accessor :new_values
8+
attr_accessor :inherited_values, :new_values
109

1110
# @param inherited_values [Object] An object implementing an interface
1211
# of the Hash class.
@@ -26,10 +25,14 @@ def initialize_copy(other)
2625
end
2726

2827
def keys
29-
combined = inherited_values.keys
30-
combined.concat(new_values.keys)
31-
combined.uniq!
32-
combined
28+
if new_values.any?
29+
combined = inherited_values.keys
30+
combined.concat(new_values.keys)
31+
combined.uniq!
32+
combined
33+
else
34+
inherited_values.keys
35+
end
3336
end
3437

3538
def key?(name)

0 commit comments

Comments
 (0)