From 53949600ffdfd323913ee5e0950b1a8dffd3d736 Mon Sep 17 00:00:00 2001 From: Ivan Babrou Date: Sun, 3 May 2020 12:48:43 -0700 Subject: [PATCH] Only inspect one frame in depends decorator Some very good background can be found on StackOverflow: * https://stackoverflow.com/questions/17407119/python-inspect-stack-is-slow I filed an issue about slow `inspect.stack()` in Python some time ago: * https://bugs.python.org/issue39643 Slow `inspect.stack()` is a problem, but it's not the problem in this case. The problem is that Salt asks Python to get a full blown stack with all the frames, but then discards everything but the second frame from the top. This is very wasteful, and it's much more economic to ask for the current frame and walk back once. Here's how long it takes to run `test.ping` with existing code: ``` $ time sudo salt-call --local test.ping local: True real 0m12.166s user 0m11.679s sys 0m0.499s ``` And this is how much better it gets with the proposed change: ``` $ time sudo salt-call --local test.ping local: True real 0m2.092s user 0m1.772s sys 0m0.336s ``` If you follow the advice from #48773 and disable `vmware` module, which is responsible for the most calls into inspect module, you won't get much better than that: ``` $ cat /etc/salt/minion disable_grains: - esxi disable_modules: - vsphere $ time sudo salt-call --local test.ping local: True real 0m2.006s user 0m1.671s sys 0m0.353s ``` Closes #48773. --- salt/utils/decorators/__init__.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/salt/utils/decorators/__init__.py b/salt/utils/decorators/__init__.py index 04f055a6e8f0..940d0a90f2cc 100644 --- a/salt/utils/decorators/__init__.py +++ b/salt/utils/decorators/__init__.py @@ -102,7 +102,7 @@ class wide dependency_dict # This inspect call may fail under certain conditions in the loader. # Possibly related to a Python bug here: # http://bugs.python.org/issue17735 - frame = inspect.stack()[1][0] + frame = inspect.currentframe().f_back # due to missing *.py files under esky we cannot use inspect.getmodule # module name is something like salt.loaded.int.modules.test _, kind, mod_name = frame.f_globals["__name__"].rsplit(".", 2)