From 44c94328c85c6999aa9f7657bfdae7cc56823ba0 Mon Sep 17 00:00:00 2001 From: Marius Gedminas Date: Tue, 25 Aug 2015 16:07:21 +0300 Subject: [PATCH] Speed up execution `if method in dir(self):` is very inefficient: - it must construct a list object listing all the object attributes & methods - it must then perform a O(N) linear scan of that list Replace it with the idiomatic `if hasattr(self, method):`, which is a O(1) expected time hash lookup. Should fix #11981. --- lib/ansible/playbook/base.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/ansible/playbook/base.py b/lib/ansible/playbook/base.py index 711857c194..0343344be9 100644 --- a/lib/ansible/playbook/base.py +++ b/lib/ansible/playbook/base.py @@ -96,7 +96,7 @@ class Base: @staticmethod def _generic_g(prop_name, self): method = "_get_attr_%s" % prop_name - if method in dir(self): + if hasattr(self, method): return getattr(self, method)() return self._attributes[prop_name]