You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Only do "task --version" once in TaskWarriorShellout() class
A TaskWarriorShellout instance calls .get_version() already thrice: once
when determining if we .can_use() TaskWarriorShellout rather than
TaskWarriorDirect, and then twice in the instance constructor itself.
Therefore creation of the instance already calls it thrice. Each of
these ends up executing "task --version" with execve(2) via
subprocess.Popen().
Use of the methods .sync() or .filter_tasks() would execute this once
again, while using .task_add() would do it twice. We therefore end up
executing "task --version" four times for a single filter_tasks() and
five times for a task_add().
Scripts which are doing multiple filters are constantly adding the
overhead of these fork/execve just to obtain the version. My own
personal taskgrep wrapper is making several calls with different
filters, and strace is showing 18 different "task --version" calls,
which on my modern-ish laptop adds about a tenth of a second to my
taskgreps:
$ TIMEFORMAT='time: %3Rs'; time \
for ((i = 0; i < 18; i++))
do task --version &>/dev/null; done
time: 0.089s
It's not huge, but these unncessary repeated "task --version" calls
really can just be done once and the value cached as a class attribute
Note that we cannot do this in the instance constructor, because
can_use() is a class method called at module import time, before any
instance will have been created, so we need get_version() to continue to
be a class method.
Thanks also to @ryneeverett for suggesting to use a simple cached class
attribute in get_version() rather than the first implementation which
moved these statements into the class definition.
0 commit comments