From 3b891bc146f371b6e334f0463e80669a39eb3489 Mon Sep 17 00:00:00 2001 From: "J. Nick Koston" Date: Fri, 10 Nov 2023 03:17:40 -0600 Subject: [PATCH] Speed up YAML by using YAML C loader when available (#5721) --- esphome/yaml_util.py | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/esphome/yaml_util.py b/esphome/yaml_util.py index 3d3fa8c5b4..a954415d12 100644 --- a/esphome/yaml_util.py +++ b/esphome/yaml_util.py @@ -23,6 +23,14 @@ from esphome.core import ( from esphome.helpers import add_class_to_obj from esphome.util import OrderedDict, filter_yaml_files +try: + from yaml import CSafeLoader as FastestAvailableSafeLoader +except ImportError: + from yaml import ( # type: ignore[assignment] + SafeLoader as FastestAvailableSafeLoader, + ) + + _LOGGER = logging.getLogger(__name__) # Mostly copied from Home Assistant because that code works fine and @@ -89,7 +97,7 @@ def _add_data_ref(fn): return wrapped -class ESPHomeLoader(yaml.SafeLoader): +class ESPHomeLoader(FastestAvailableSafeLoader): """Loader class that keeps track of line numbers.""" @_add_data_ref