Skip to content
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

run_validators memory optmization #2331

Merged
merged 2 commits into from
May 20, 2023
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 @@ -5,6 +5,7 @@
* [#2326](https://github.com/ruby-grape/grape/pull/2326): Use ActiveSupport extensions - [@ericproulx](https://github.com/ericproulx).
* [#2327](https://github.com/ruby-grape/grape/pull/2327): Use ActiveSupport deprecation - [@ericproulx](https://github.com/ericproulx).
* [#2330](https://github.com/ruby-grape/grape/pull/2330): Use ActiveSupport inflector - [@ericproulx](https://github.com/ericproulx).
* [#2331](https://github.com/ruby-grape/grape/pull/2331): Memory optimization when running validators - [@ericproulx](https://github.com/ericproulx).
* Your contribution here.

#### Fixes
Expand Down
26 changes: 14 additions & 12 deletions lib/grape/endpoint.rb
Original file line number Diff line number Diff line change
Expand Up @@ -317,8 +317,8 @@ def build_stack(helpers)
end

def build_helpers
helpers = namespace_stackable(:helpers) || []
Module.new { helpers.each { |mod_to_include| include mod_to_include } }
helpers = namespace_stackable(:helpers)
Module.new { helpers&.each { |mod_to_include| include mod_to_include } }
end

private :build_stack, :build_helpers
Expand All @@ -344,11 +344,9 @@ def lazy_initialize!
end
end

def run_validators(validator_factories, request)
def run_validators(validators, request)
validation_errors = []

validators = validator_factories.map { |options| Grape::Validations::ValidatorFactory.create_validator(**options) }

ActiveSupport::Notifications.instrument('endpoint_run_validators.grape', endpoint: self, validators: validators, request: request) do
validators.each do |validator|
validator.validate(request)
Expand All @@ -366,34 +364,38 @@ def run_validators(validator_factories, request)

def run_filters(filters, type = :other)
ActiveSupport::Notifications.instrument('endpoint_run_filters.grape', endpoint: self, filters: filters, type: type) do
(filters || []).each { |filter| instance_eval(&filter) }
filters&.each { |filter| instance_eval(&filter) }
end
post_extension = DSL::InsideRoute.post_filter_methods(type)
extend post_extension if post_extension
end

def befores
namespace_stackable(:befores) || []
namespace_stackable(:befores)
end

def before_validations
namespace_stackable(:before_validations) || []
namespace_stackable(:before_validations)
end

def after_validations
namespace_stackable(:after_validations) || []
namespace_stackable(:after_validations)
end

def afters
namespace_stackable(:afters) || []
namespace_stackable(:afters)
end

def finallies
namespace_stackable(:finallies) || []
namespace_stackable(:finallies)
end

def validations
route_setting(:saved_validations) || []
return enum_for(:validations) unless block_given?

route_setting(:saved_validations)&.each do |saved_validation|
yield Grape::Validations::ValidatorFactory.create_validator(**saved_validation)
end
end

def options?
Expand Down