Skip to content

Commit 9c566f3

Browse files
kmichel-aivenshinnar
authored andcommitted
Avoid slow regex_meet in _joinString
There are already some places where the calling code takes care of avoiding `.*` and just use `None`. However, this was not done in `_joinString` and was producing a trivial `.{0,}` causing slow calls to `regex_meet`. A sample schema that is made faster by this change is: ``` { 'anyOf': [ { 'title': 'MyEnum', 'enum': [ 'aaaaaaaaaa', 'bbbbbbbbbb', 'cccccccccc', 'dddddddddd', 'eeeeeeeeee', 'ffffffffff', 'gggggggggg', 'hhhhhhhhhh', 'iiiiiiiiii', 'kkkkkkkkkk' ] }, {'type': 'string'} ] } ``` Which takes ~6sec to be compared with itself with `isSubset` before this change and ~0.05sec after the change.
1 parent 8e65354 commit 9c566f3

File tree

1 file changed

+10
-4
lines changed

1 file changed

+10
-4
lines changed

jsonsubschema/_checkers.py

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -344,10 +344,16 @@ def _joinString(s1, s2):
344344
mx = max(s1.maxLength, s2.maxLength)
345345
if utils.is_num(mx):
346346
ret["maxLength"] = mx
347-
s1_range = utils.string_range_to_regex(
348-
s1.minLength, s1.maxLength)
349-
s2_range = utils.string_range_to_regex(
350-
s2.minLength, s2.maxLength)
347+
if s1.minLength == 0 and s1.maxLength == I.inf:
348+
s1_range = None
349+
else:
350+
s1_range = utils.string_range_to_regex(
351+
s1.minLength, s1.maxLength)
352+
if s2.minLength == 0 and s2.maxLength == I.inf:
353+
s2_range = None
354+
else:
355+
s2_range = utils.string_range_to_regex(
356+
s2.minLength, s2.maxLength)
351357
s1_new_pattern = utils.regex_meet(s1_range, s1.pattern)
352358
s2_new_pattern = utils.regex_meet(s2_range, s2.pattern)
353359
if s1_new_pattern and s2_new_pattern:

0 commit comments

Comments
 (0)