Repository navigation
Speed up validation and building of validators - #32
Merged
Merged
Conversation
On PHP 8.5 validation is 1.2-1.4 times faster, list_of and or 1.8 times, building a validator 1.5-1.8 times. The LIVR test suite, where every case builds a validator, runs 1.5 times faster. PHP 7.2 gets about the same. - validate() keeps the output of a field in variables instead of looking it up in the result twice for every rule - Util::isAssocArray() uses array_is_list() on PHP 8.1+ and a loop on older versions instead of making two arrays - A new validator shares the default rules instead of copying them, and registerRules() skips the merge when meta rules register the same rules - Rule builders given as "Class::method" strings are called as closures, which are not resolved on every call - parseRule() takes the first key without reset(), which copies the array - getFieldRule() returns the rules of a field as one rule, like in the JavaScript implementation. list_of, or and aliases use it instead of calling validate() for every value. - iso_date uses checkdate() instead of DateTime. This also fixes PHP 7.2, where DateTime turns 2769-12-31 into 2770-01-00. - one_of looks values up in a set instead of in_array()
This was referenced Oct 23, 2026
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.
Depends on #31, please merge it after #31. Until then this PR targets the branch of #31, so it shows only its own commit.
Summary
Validation is 1.2-1.4 times faster,
list_ofandor1.8 times, building a validator 1.5-1.8 times. The behavior does not change.Operations per second, the same core, before and after:
list_ofwith 10 itemsoriso_dateone_ofwith 26 valuesBuilding a validator matters most in PHP, as it is usually done on every request.
Changes
validate()keeps the output of a field in variables instead of looking it up in the result twice for every rule.Util::isAssocArray()usesarray_is_list()on PHP 8.1+ and a loop on older versions instead of making two arrays.registerRules()skips the merge when meta rules register the same rules.Class::methodstrings are called as closures, which are not resolved on every call.parseRule()takes the first key withoutreset(), which copies the array.getFieldRule($fieldName)returns the rules of a field as one rule, like in the JavaScript implementation.list_of,orand aliases use it instead of callingvalidate()for every value. It is described in the README.iso_dateusescheckdate()instead ofDateTime. This also fixes PHP 7.2, whereDateTimeturns2769-12-31into2770-01-00, so some real dates were rejected and some wrong ones accepted.one_oflooks values up in a set instead ofin_array().Testing
getFieldRule(). phpcs passes.iso_dategives the same results as before for every date from 0000 to 9999 (months 00-19, days 00-39) and for 300,000 changed strings on PHP 8.5. On PHP 7.2 only the dates above differ.Found while testing, not changed here: on PHP 8,
trim,to_lcandto_ucthrow a TypeError when a list or an object has an array in it, for example['a', ['b']].