Instantiate validators at definition time#2657
Draft
ericproulx wants to merge 1 commit intomasterfrom
Draft
Conversation
Danger ReportNo issues found. |
5d145a5 to
f87920f
Compare
Contributor
Author
|
Missing UPGRADING notes. Working on it |
2ecb403 to
cf04c9d
Compare
Member
|
Is there a tradeoff with things like |
Contributor
Author
e16efcf to
0b9e34b
Compare
a503556 to
2b16ba1
Compare
b744723 to
30b09ea
Compare
ff94e8f to
6fcca15
Compare
Validators are now instantiated once at route definition time rather than per-request, eliminating repeated allocation overhead. Validator instances are frozen via `DeepFreeze` to make them safe for sharing across requests. - Override `Validators::Base.new` to call `freeze` after initialization - Add `Grape::Util::DeepFreeze` to recursively freeze Hash/Array/String values while leaving Procs, scopes, and coercers unfrozen - Remove `ValidatorFactory` (no longer needed) - Update all validators to be compatible with frozen instances (move any mutable state out of instance variables set at validation time) - Add specs for `DeepFreeze`, `SameAsValidator`, and `ExceptValuesValidator`
6fcca15 to
e9ebf2f
Compare
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Summary
Validators are now instantiated once at route definition time rather than per-request via
ValidatorFactory. This eliminates repeated object allocation on every request and moves expensive setup (option parsing, converter building, message formatting) out of the hot path.Because validator instances are shared across all concurrent requests they are frozen after initialization, enforced by a
Validators::Base.newoverride that callsfreeze_state!thenfreeze. A newGrape::Util::DeepFreezehelper recursively freezes Hash/Array/String values while intentionally leaving Procs, coercers, and other mutable objects unfrozen.Changes
Core
ParamsScope#validateinstantiates the validator class directly and stores the instance innamespace_stackable[:validations]; removesValidatorFactoryEndpoint#run_validatorsreads validator instances directly fromsaved_validationsand removes thevalidationsenumerator methodParamsScopeis frozen at the end ofinitialize;element,parent, andAttr#key/Attr#scopechanged fromattr_accessortoattr_readercoerce_typenow receives only the extracted coercion keys so callers don't need to delete them afterwardsValidators::BaseBase.newcalls privatefreeze_state!(deep-freezes all ivars) thenfreeze; subclasses can overridefreeze_state!to skip specific ivarsfail_fast?promoted to explicit public methodvalidate_param!promoted toprotectedwith aNotImplementedErrordefaulthash_like?extracted as a private helperGrape::Util::DeepFreezedeep_freeze(obj)functionValidator-level eager initialization
AllowBlankValidator: caches@valueand@exception_messageCoerceValidator: resolves type and builds converter at definition time; overridesfreeze_state!to leave the coercer unfrozenDefaultValidator: pre-builds@default_calllambdaExceptValuesValidator/ValuesValidator: validates proc arity at initRegexpValidator,SameAsValidator,PresenceValidator: cache messagesAllOrNoneOf,AtLeastOneOf,MutuallyExclusiveValidator,ExactlyOneOfValidator: cache exception messagesContractScopeValidator: no longer inherits fromBaseSpecs
DeepFreezeSpecSameAsValidatorandExceptValuesValidatorContractScopeValidatorSpec/custom_validations_specupdated for the new instantiation modelTest plan
bundle exec rspecFrozenErroron any attempt to mutate state at request time