[status-] decrease latency of status() when other threads run #2370
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
While benchmarking, I noticed that
vd.status()
is slow to finish when vd is running other very busy threads (taking ~150ms). If there are no busy threads, it's much faster, (taking ~2ms).By recording the run time in memory, I found that it's only a matter of latency. That is,
inspect.stack()
takes a lot of clock time to finish, around 150 milliseconds, but very little of that time is spent in the thread. I don't know why, but I assumestack()
pauses to let the busy threads run.It turns out
inspect.stack()
is slow because it reads source files. And it can be made much faster by not asking for lines of source file context.Here is a comparison of the latency, before and after this PR.
Before:
Using
inspect.stack()
, while loading a 2 million line tsv file takes 150-190ms per call tostatus()
:quit.vdj.txt
After:
using
inspect.stack(0)
, it takes only 40-50ms: