From d868b409c66e788c1f6f0ad551573a5d72bc8a28 Mon Sep 17 00:00:00 2001 From: Daniel Silverstone Date: Thu, 1 Nov 2018 12:40:57 +0000 Subject: _yaml.py: Implement `get()` for `ChainMap` Since the core Python `ChainMap.get()` implements with: self[key] if key in self else default The double-chain-lookup is expensive. This simple change solves that for our ChainMap derived structure. As such it improves matters for #466 somewhat. Signed-off-by: Daniel Silverstone --- buildstream/_yaml.py | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/buildstream/_yaml.py b/buildstream/_yaml.py index ca12acae9..f44572ca5 100644 --- a/buildstream/_yaml.py +++ b/buildstream/_yaml.py @@ -1049,6 +1049,12 @@ class ChainMap(collections.ChainMap): for key in clearable: del self[key] + def get(self, key, default=None): + try: + return self[key] + except KeyError: + return default + def node_chain_copy(source): copy = ChainMap({}, source) -- cgit v1.2.1