rc git: Batch flags when passing commit info for "git blame"

The wrapper for "git blame" creates flags for each line of the buffer.
It parses the output from git and would send a flag (or a series of
flags) each time the commit to blame for a line differs from the
previous one. For files that were touched by a large number of commits,
this results in a high number of kakoune processes being launched, and
may take some time. This is visible in the session through the flags for
the different commits appearing on the lines one by one, possibly during
several seconds.

To speed up the process, batch flags before passing them to the kak
session. One solution could be to send all flags at once, but this might
delay the appearance of commit info for too long if "git blame" really
takes a long time. The alternative solution retained for this commit
consists in grouping as many flags as we can during one second
(roughly), to pass them to kakoune, and then to move on to the next
flags. This way, a new batch of commit information flags appears every
second or so in the client, until all information is added. This should
be much faster than lauching a kakoune process for each commit
reported by "git blame": tests have shown that blaming a large file in
the Linux repository goes 4.5 times faster when batching flags.

Co-authored-by: Johannes Altmanninger <aclopte@gmail.com>
This commit is contained in:
Qeole 2022-01-27 23:06:00 +00:00
parent 9acd4e62dc
commit 5650bf33fa

View File

@ -109,21 +109,27 @@ define-command -params 1.. \
set-option buffer=$kak_bufname git_blame_flags '$kak_timestamp' set-option buffer=$kak_bufname git_blame_flags '$kak_timestamp'
}" | kak -p ${kak_session} }" | kak -p ${kak_session}
git blame "$@" --incremental ${kak_buffile} | awk ' git blame "$@" --incremental ${kak_buffile} | awk '
function send_flags(text, flag, i) { function send_flags(flush, text, i) {
if (line == "") { return; } if (line == "") { return; }
text=substr(sha,1,8) " " dates[sha] " " authors[sha] text=substr(sha,1,8) " " dates[sha] " " authors[sha]
# gsub("|", "\\|", text) # gsub("|", "\\|", text)
gsub("~", "~~", text) gsub("~", "~~", text)
flag="%~" line "|" text "~" for ( i=0; i < count; i++ ) {
for ( i=1; i < count; i++ ) { flags = flags " %~" line+i "|" text "~"
flag=flag " %~" line+i "|" text "~" }
now = systime()
# Send roughly one update per second, to avoid creating too many kak processes.
if (!flush && now - last_sent < 1) {
return
} }
cmd = "kak -p " ENVIRON["kak_session"] cmd = "kak -p " ENVIRON["kak_session"]
print "set-option -add buffer=" ENVIRON["kak_bufname"] " git_blame_flags " flag | cmd print "set-option -add buffer=" ENVIRON["kak_bufname"] " git_blame_flags " flags | cmd
close(cmd) close(cmd)
flags = ""
last_sent = now
} }
/^([0-9a-f]+) ([0-9]+) ([0-9]+) ([0-9]+)/ { /^([0-9a-f]+) ([0-9]+) ([0-9]+) ([0-9]+)/ {
send_flags() send_flags(0)
sha=$1 sha=$1
line=$3 line=$3
count=$4 count=$4
@ -134,7 +140,7 @@ define-command -params 1.. \
cmd | getline dates[sha] cmd | getline dates[sha]
close(cmd) close(cmd)
} }
END { send_flags(); }' END { send_flags(1); }'
) > /dev/null 2>&1 < /dev/null & ) > /dev/null 2>&1 < /dev/null &
} }