Skip to content

Commit

Permalink
get rid of a needless step in HashWithIndifferentAccess
Browse files Browse the repository at this point in the history
There was an extra operation in
`Grape::Extensions::ActiveSupport::HashWithIndifferentAccess::ParamBuilder`
it is unnecessary, people already use Rails 4, 5 or 6. Devs who are on Rails 3
less likely will upgrade to latest Grape.

After removing that operation Grape consumes less memory.

Before: 36080 bytes
After:  32472 bytes
  • Loading branch information
dnesteryuk committed Dec 3, 2019
1 parent 69ce0df commit 7ed7598
Show file tree
Hide file tree
Showing 6 changed files with 10 additions and 25 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#### Features

* Your contribution here.
* [#1940](https://github.com/ruby-grape/grape/pull/1940): Get rid of a needless step in HashWithIndifferentAccess - [@dnesteryuk](https://github.com/dnesteryuk).
* [#1938](https://github.com/ruby-grape/grape/pull/1938): Add project metadata to the gemspec - [@orien](https://github.com/orien).
* [#1920](https://github.com/ruby-grape/grape/pull/1920): Replace Virtus with dry-types - [@dnesteryuk](https://github.com/dnesteryuk).

Expand Down
3 changes: 2 additions & 1 deletion lib/grape/dsl/inside_route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,8 @@ def should_be_empty_array?(declared_param, passed_children_params)
end

def declared_param_is_array?(declared_param)
route_options_params[declared_param.to_s] && route_options_params[declared_param.to_s][:type] == 'Array'
key = declared_param.to_s
route_options_params[key] && route_options_params[key][:type] == 'Array'
end

def route_options_params
Expand Down
2 changes: 1 addition & 1 deletion lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,7 @@ def mount_in(router)
methods << Grape::Http::Headers::HEAD
end
methods.each do |method|
unless route.request_method.to_s.upcase == method
unless route.request_method == method
route = Grape::Router::Route.new(method, route.origin, route.attributes.to_h)
end
router.append(route.apply(self))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -14,10 +14,9 @@ def params_builder
end

def build_params
params = ::ActiveSupport::HashWithIndifferentAccess[rack_params]
params = ::ActiveSupport::HashWithIndifferentAccess.new(rack_params)
params.deep_merge!(grape_routing_args) if env[Grape::Env::GRAPE_ROUTING_ARGS]
# TODO: remove, in Rails 4 or later ::ActiveSupport::HashWithIndifferentAccess converts nested Hashes into indifferent access ones
DeepHashWithIndifferentAccess.deep_hash_with_indifferent_access(params)
params
end
end
end
Expand Down
18 changes: 0 additions & 18 deletions lib/grape/extensions/deep_hash_with_indifferent_access.rb

This file was deleted.

6 changes: 4 additions & 2 deletions lib/grape/router/route.rb
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,12 @@ def route_path
end

def initialize(method, pattern, **options)
upcased_method = method.to_s.upcase

@suffix = options[:suffix]
@options = options.merge(method: method.to_s.upcase)
@options = options.merge(method: upcased_method)
@pattern = Pattern.new(pattern, **options)
@translator = AttributeTranslator.new(**options, request_method: method.to_s.upcase)
@translator = AttributeTranslator.new(**options, request_method: upcased_method)
end

def exec(env)
Expand Down

0 comments on commit 7ed7598

Please sign in to comment.