Skip to content

Commit

Permalink
dtschema: Delay ruamel.yaml import
Browse files Browse the repository at this point in the history
When running dt-validate, YAML support isn't needed when using a
preprocessed schema JSON file. The import time for ruamel takes about
40ms which around 1/4th of the total startup time. It is mostly due to
pkg_resources module import by ruamel. Move the imports to where they
are needed.

It's not much of a speed-up overall, but every little bit helps.

Signed-off-by: Rob Herring <robh@kernel.org>
  • Loading branch information
robherring committed May 31, 2024
1 parent a5bbb3e commit 7c8798f
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 9 deletions.
21 changes: 13 additions & 8 deletions dtschema/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
# Python library for Devicetree schema validation
import sys
import os
import ruamel.yaml
import re
import copy
import jsonschema
Expand All @@ -16,13 +15,6 @@
schema_base_url = "http://devicetree.org/"
schema_basedir = os.path.dirname(os.path.abspath(__file__))

rtyaml = ruamel.yaml.YAML(typ='rt')
rtyaml.allow_duplicate_keys = False
rtyaml.preserve_quotes = True

yaml = ruamel.yaml.YAML(typ='safe')
yaml.allow_duplicate_keys = False


def path_to_obj(tree, path):
for pc in path:
Expand All @@ -31,6 +23,8 @@ def path_to_obj(tree, path):


def get_line_col(tree, path, obj=None):
import ruamel.yaml

if isinstance(obj, ruamel.yaml.comments.CommentedBase):
return obj.lc.line, obj.lc.col
obj = path_to_obj(tree, path)
Expand Down Expand Up @@ -64,9 +58,17 @@ class DTSchema(dict):
def __init__(self, schema_file, line_numbers=False):
self.paths = [(schema_base_url, schema_basedir + '/')]
with open(schema_file, 'r', encoding='utf-8') as f:
import ruamel.yaml

if line_numbers:
rtyaml = ruamel.yaml.YAML(typ='rt')
rtyaml.allow_duplicate_keys = False
rtyaml.preserve_quotes = True

schema = rtyaml.load(f.read())
else:
yaml = ruamel.yaml.YAML(typ='safe')
yaml.allow_duplicate_keys = False
schema = yaml.load(f.read())

self.filename = os.path.abspath(schema_file)
Expand All @@ -85,6 +87,9 @@ def http_handler(self, uri):
if not os.path.isfile(filename):
continue
with open(filename, 'r', encoding='utf-8') as f:
import ruamel.yaml
yaml = ruamel.yaml.YAML(typ='safe')
yaml.allow_duplicate_keys = False
return yaml.load(f.read())

raise RefResolutionError('Error in referenced schema matching $id: ' + uri)
Expand Down
2 changes: 1 addition & 1 deletion dtschema/validator.py
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@
import glob
import json
import jsonschema
import ruamel.yaml

from jsonschema.exceptions import RefResolutionError

Expand Down Expand Up @@ -347,6 +346,7 @@ def __init__(self, schema_files, filter=None):
except json.decoder.JSONDecodeError:
try:
f.seek(0)
import ruamel.yaml
yaml = ruamel.yaml.YAML(typ='safe')
schema_cache = yaml.load(f.read())
except:
Expand Down

0 comments on commit 7c8798f

Please sign in to comment.