Skip to content

Commit ccd2330

Browse files
authored
run_validators memory optmization (#2331)
* Yield Validator instead of map all of them * Changelog entry
1 parent cc285a3 commit ccd2330

File tree

2 files changed

+15
-12
lines changed

2 files changed

+15
-12
lines changed

CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
* [#2326](https://github.com/ruby-grape/grape/pull/2326): Use ActiveSupport extensions - [@ericproulx](https://github.com/ericproulx).
66
* [#2327](https://github.com/ruby-grape/grape/pull/2327): Use ActiveSupport deprecation - [@ericproulx](https://github.com/ericproulx).
77
* [#2330](https://github.com/ruby-grape/grape/pull/2330): Use ActiveSupport inflector - [@ericproulx](https://github.com/ericproulx).
8+
* [#2331](https://github.com/ruby-grape/grape/pull/2331): Memory optimization when running validators - [@ericproulx](https://github.com/ericproulx).
89
* Your contribution here.
910

1011
#### Fixes

lib/grape/endpoint.rb

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -317,8 +317,8 @@ def build_stack(helpers)
317317
end
318318

319319
def build_helpers
320-
helpers = namespace_stackable(:helpers) || []
321-
Module.new { helpers.each { |mod_to_include| include mod_to_include } }
320+
helpers = namespace_stackable(:helpers)
321+
Module.new { helpers&.each { |mod_to_include| include mod_to_include } }
322322
end
323323

324324
private :build_stack, :build_helpers
@@ -344,11 +344,9 @@ def lazy_initialize!
344344
end
345345
end
346346

347-
def run_validators(validator_factories, request)
347+
def run_validators(validators, request)
348348
validation_errors = []
349349

350-
validators = validator_factories.map { |options| Grape::Validations::ValidatorFactory.create_validator(**options) }
351-
352350
ActiveSupport::Notifications.instrument('endpoint_run_validators.grape', endpoint: self, validators: validators, request: request) do
353351
validators.each do |validator|
354352
validator.validate(request)
@@ -366,34 +364,38 @@ def run_validators(validator_factories, request)
366364

367365
def run_filters(filters, type = :other)
368366
ActiveSupport::Notifications.instrument('endpoint_run_filters.grape', endpoint: self, filters: filters, type: type) do
369-
(filters || []).each { |filter| instance_eval(&filter) }
367+
filters&.each { |filter| instance_eval(&filter) }
370368
end
371369
post_extension = DSL::InsideRoute.post_filter_methods(type)
372370
extend post_extension if post_extension
373371
end
374372

375373
def befores
376-
namespace_stackable(:befores) || []
374+
namespace_stackable(:befores)
377375
end
378376

379377
def before_validations
380-
namespace_stackable(:before_validations) || []
378+
namespace_stackable(:before_validations)
381379
end
382380

383381
def after_validations
384-
namespace_stackable(:after_validations) || []
382+
namespace_stackable(:after_validations)
385383
end
386384

387385
def afters
388-
namespace_stackable(:afters) || []
386+
namespace_stackable(:afters)
389387
end
390388

391389
def finallies
392-
namespace_stackable(:finallies) || []
390+
namespace_stackable(:finallies)
393391
end
394392

395393
def validations
396-
route_setting(:saved_validations) || []
394+
return enum_for(:validations) unless block_given?
395+
396+
route_setting(:saved_validations)&.each do |saved_validation|
397+
yield Grape::Validations::ValidatorFactory.create_validator(**saved_validation)
398+
end
397399
end
398400

399401
def options?

0 commit comments

Comments
 (0)