Skip to content

Commit

Permalink
Only inspect one frame in depends decorator
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
bobrik authored and dwoz committed May 9, 2020
1 parent e839fd6 commit 5394960
Showing 1 changed file with 1 addition and 1 deletion.
2 changes: 1 addition & 1 deletion salt/utils/decorators/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit 5394960

Please sign in to comment.