python-jsonschema / jsonschema

An implementation of the JSON Schema specification for Python
https://python-jsonschema.readthedocs.io
MIT License
4.52k stars 574 forks source link

The latest version is slower than 3.2.0 validator #1231

Closed samjuis closed 4 months ago

samjuis commented 4 months ago

I would like to ask if I can replace 'for error in self.evolve(schema=schema).iter_errors(instance, schema)' in version 4.3.1: This line is 'for error in self.iter_errors(instance, schema)' in version 3.2.0 :, I found that this can reduce 2-3 seconds when traversing 500 times;

4.3.1
 for error in self.evolve(schema=schema).iter_errors(instance):

12 sec

3.2.0
for error in self.iter_errors(instance, schema):

After use  9sec

In addition, my current version is 4.0.1, just replace the 'find_item' in the 'resolve_fragment' method with find.

4.0.1
         for keyword in ["$anchor", "$dynamicAnchor"]:
            for subschema in self._finditem(document, keyword):
                 if fragment == subschema[keyword]:
                     return subschema
         for keyword in ["id", "$id"]:
             for subschema in self._finditem(document, keyword):
                 if "#" + fragment == subschema[keyword]:
                    return subschema

4.3.1
@lru_cache
def _find_in_referrer
.
.
.
        if document is self.referrer:
            find = self._find_in_referrer
        else:

            def find(key):
                return self._finditem(document, key)

        for keyword in ["$anchor", "$dynamicAnchor"]:
            for subschema in find(keyword):
                if fragment == subschema[keyword]:
                    return subschema
        for keyword in ["id", "$id"]:
            for subschema in find(keyword):
                if "#" + fragment == subschema[keyword]:
                    return subschema
samjuis commented 4 months ago

I understand part of the code. The reason why it is slower than the old version is that the verification items are constantly increasing. I am currently using Draft202012Validator. After switching to Draft4Validator, the difference is doubled (the original difference was five times). dont know if it is like this

Julian commented 4 months ago

I don't know what you're meaning to report with this issue. 4.3.1 is a very old version, and 3.2.0 is an ancient one.

If you're trying to report any issue, you need to include a minimal working example of what you ran and what the change was.

samjuis commented 4 months ago

for the old version (4.0.1) I just want to speed up the execution to be close to version 3.2.0 (or older), because the latest version is slower than version 4.3.1 (as per issue #853 first (the result of running a test case)), I gave up this version and came here to ask for advice.

Julian commented 4 months ago

Slower on what? #853 is a very old issue about a very old version. The current version of this library is 4.21.1, which is the only version I support.

samjuis commented 4 months ago

Ok, I see. Thanks. I've found the solution. The new version does not have this problem, and the efficiency has returned to that of the ancient version.