Skip to content

Another array allocation reduction #2020

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

#### Features
* Your contribution here.
* [#2020](https://github.com/ruby-grape/grape/pull/2020): Reduce array allocation - [@ericproulx](https://github.com/ericproulx).
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

For next time, you can do [#2020](https://github.com/ruby-grape/grape/pull/2020), [#2014](https://github.com/ruby-grape/grape/pull/2014): Reduce ....

* [#2015](https://github.com/ruby-grape/grape/pull/2014): Reduce MatchData allocation - [@ericproulx](https://github.com/ericproulx).
* [#2014](https://github.com/ruby-grape/grape/pull/2014): Reduce total allocated arrays - [@ericproulx](https://github.com/ericproulx).
* [#2011](https://github.com/ruby-grape/grape/pull/2011): Reduce total retained regexes - [@ericproulx](https://github.com/ericproulx).
Expand Down
10 changes: 7 additions & 3 deletions lib/grape/api/instance.rb
Original file line number Diff line number Diff line change
Expand Up @@ -243,9 +243,13 @@ def add_head_not_allowed_methods_and_options_methods
# Generate a route that returns an HTTP 405 response for a user defined
# path on methods not specified
def generate_not_allowed_method(pattern, allowed_methods: [], **attributes)
not_allowed_methods = %w[GET PUT POST DELETE PATCH HEAD] - allowed_methods
not_allowed_methods << Grape::Http::Headers::OPTIONS if self.class.namespace_inheritable(:do_not_route_options)

supported_methods =
if self.class.namespace_inheritable(:do_not_route_options)
Grape::Http::Headers::SUPPORTED_METHODS
else
Grape::Http::Headers::SUPPORTED_METHODS_WITHOUT_OPTIONS
end
not_allowed_methods = supported_methods - allowed_methods
return if not_allowed_methods.empty?

@router.associate_routes(pattern, not_allowed_methods: not_allowed_methods, **attributes)
Expand Down
1 change: 1 addition & 0 deletions lib/grape/http/headers.rb
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ module Headers
OPTIONS = 'OPTIONS'

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

HTTP_ACCEPT_VERSION = 'HTTP_ACCEPT_VERSION'
X_CASCADE = 'X-Cascade'
Expand Down
15 changes: 9 additions & 6 deletions lib/grape/util/base_inheritable.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,7 @@ module Util
# Base for classes which need to operate with own values kept
# in the hash and inherited values kept in a Hash-like object.
class BaseInheritable
attr_accessor :inherited_values
attr_accessor :new_values
attr_accessor :inherited_values, :new_values

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

def keys
combined = inherited_values.keys
combined.concat(new_values.keys)
combined.uniq!
combined
if new_values.any?
combined = inherited_values.keys
combined.concat(new_values.keys)
combined.uniq!
combined
else
inherited_values.keys
end
end

def key?(name)
Expand Down