Optimize performance of running command finalization hooks #1504
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.
Optimize performance of the
_run_cmdfinalization_hooks
method by replacing a subprocess call tostty sane
with a call totermios.tcsetattr()
. For optimial performance, the initial termios settings are cached when instantiating thecmd2.Cmd
class and are restored in the_run_cmdfinalization_hooks
method.The motivation behind this was a user report of slowness in Discussion #1503.
I also temporarily tweaked where we we report our elapsed timing when the
timiing
settable is True to include the full command lifecycle timing and did measurements before and after.I did some testing on a system with the following setup:
cmd2
from main branch on GitHubin this testing I modified cmd's built-in timing measurement to capture the full command lifecycle timing. With the code as-is on the
main
branch, the full lifecycle for an empty command on this older computer was about 0.007 seconds (7 thousands of a second). This is not something I would even remotely call "slow" given typical human reaction times.I then re-ran the same tests using the code in this PR. This did substantially speed things up to the point where an empty command ran with an elapsed time of about 0.00011 seconds (about 64 times faster).
I think we should do some manual evaluation with the code here to verify it is fixing the same things that
stty sane
was fixing. Even though the performance difference should be below what would normally be perceptible for the system I tested on, user reports of slowness in some circumstances suggest this may be a beneficial and noticeable change overall.TODO:
stty sane