From fe6220dd99ff0179b0616e29aafc81393bfbaa86 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Mon, 31 Oct 2022 22:46:37 -0500 Subject: [PATCH 1/2] GH-98766: Modest speed-up from ChainMap.__iter__ --- Lib/collections/__init__.py | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index 58607874be93d6..19b9659a995172 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -1011,8 +1011,8 @@ def __len__(self): def __iter__(self): d = {} - for mapping in reversed(self.maps): - d.update(dict.fromkeys(mapping)) # reuses stored hash values if possible + for mapping in map(dict.fromkeys, reversed(self.maps)): + d |= mapping return iter(d) def __contains__(self, key): From 17334600bd270add45a578b2bc5e95ff48e60dff Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Mon, 31 Oct 2022 23:11:35 -0500 Subject: [PATCH 2/2] Restore comment --- Lib/collections/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/Lib/collections/__init__.py b/Lib/collections/__init__.py index 19b9659a995172..f07ee143a5aff1 100644 --- a/Lib/collections/__init__.py +++ b/Lib/collections/__init__.py @@ -1012,7 +1012,7 @@ def __len__(self): def __iter__(self): d = {} for mapping in map(dict.fromkeys, reversed(self.maps)): - d |= mapping + d |= mapping # reuses stored hash values if possible return iter(d) def __contains__(self, key):