From bf825da533c05f7a486fc5ac7f5a9afc9fc3b2be Mon Sep 17 00:00:00 2001
From: Daniel Silverstone <daniel.silverstone@codethink.co.uk>
Date: Tue, 12 Feb 2019 09:02:42 +0000
Subject: [PATCH] _yaml.py: Reduce cost of node_final_assertions

By re-using the isinstance replacements from earlier commits and
using a tuple of the string constants for checking for composition
markers, we reduce the cost of node_final_assertions by two thirds
in basic testing.

Signed-off-by: Daniel Silverstone <daniel.silverstone@codethink.co.uk>
---
 buildstream/_yaml.py | 18 +++++++++++++-----
 1 file changed, 13 insertions(+), 5 deletions(-)

diff --git a/buildstream/_yaml.py b/buildstream/_yaml.py
index e0c090b49a..2f8db1f895 100644
--- a/buildstream/_yaml.py
+++ b/buildstream/_yaml.py
@@ -1177,6 +1177,10 @@ def list_copy(source):
     return copy
 
 
+# Helpers for the assertions
+__node_assert_composition_directives = ('(>)', '(<)', '(=)')
+
+
 # node_final_assertions()
 #
 # This must be called on a fully loaded and composited node,
@@ -1195,22 +1199,26 @@ def node_final_assertions(node):
         # indicates that the user intended to override a list which
         # never existed in the underlying data
         #
-        if key in ['(>)', '(<)', '(=)']:
+        if key in __node_assert_composition_directives:
             provenance = node_get_provenance(node, key)
             raise LoadError(LoadErrorReason.TRAILING_LIST_DIRECTIVE,
                             "{}: Attempt to override non-existing list".format(provenance))
 
-        if isinstance(value, collections.abc.Mapping):
+        value_type = type(value)
+
+        if value_type in __dict_types:
             node_final_assertions(value)
-        elif isinstance(value, list):
+        elif value_type in __list_types:
             list_final_assertions(value)
 
 
 def list_final_assertions(values):
     for value in values:
-        if isinstance(value, collections.abc.Mapping):
+        value_type = type(value)
+
+        if value_type in __dict_types:
             node_final_assertions(value)
-        elif isinstance(value, list):
+        elif value_type in __list_types:
             list_final_assertions(value)
 
 
-- 
GitLab