Skip to content

Improve performance #2

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
stuaxo opened this issue Sep 18, 2019 · 2 comments
Open

Improve performance #2

stuaxo opened this issue Sep 18, 2019 · 2 comments

Comments

@stuaxo
Copy link

stuaxo commented Sep 18, 2019

Hi,
Thanks for this, I couldn't quite get powerline itself to work in my setup so this is handy.

Where I work the git repo is really big so each git command takes about 0.3 seconds.

As there a few it takes about a second to draw for the prompt to draw.

I don't know git well enough to provide a patch, but I was looking at whether its possible to get more of the stats using fewer external commands,

As an experiment I've made a script that gets the changed file count, and the the amount of insertions, using one call to git diff --shortstat and then parsing the output in bash.

Maybe this would be useful in helping speed up rendering of the prompt?

$  ./parse_git_stats.sh

original output: " 3 files changed"
changed: '3'
inserted: '4'

script:

#!/bin/bash

# Set comma as the delimiter
IFS=','

read -a gitstat <<< $(git diff --shortstat)

# Example output from git diff --shortstat:
#  3 files changed, 4 insertions(+)
#
# read has split it by comma, next remove space prefix and text suffix
# using bash.

git_changed=${gitstat[0]%" files changed"}
git_changed=${git_changed# }

git_insertions=${gitstat[1]% "insertions(+)"}
git_insertions=${git_insertions# }

echo "changed: '"$git_changed"'"
echo "inserted: '"$git_insertions"'"

Most of the work is removing the prefixes + suffixes from the strings.

@stuaxo stuaxo changed the title Speed up git stats by calling it less Improve performance Oct 2, 2019
@stuaxo
Copy link
Author

stuaxo commented Oct 2, 2019

Hi,
You can benchmark the segments by editing __powerline_prompt_command to output how long each one takes:

function __powerline_prompt_command {
  local last_status="$?" ## always the first
  local separator_char="${POWERLINE_PROMPT_CHAR}"

  LEFT_PROMPT=""
  SEGMENTS_AT_LEFT=0
  LAST_SEGMENT_COLOR=""

  TIMEFORMAT=%R
  ## left prompt ##
  for segment in $POWERLINE_PROMPT; do
    local f="__powerline_${segment}_prompt"
    #local info="$(__powerline_${segment}_prompt)"
    echo -ne "$segment: "
    time local info="$($f)"    
    [[ -n "${info}" ]] && __powerline_left_segment "${info}"
  done

  [[ -n "${LEFT_PROMPT}" ]] && LEFT_PROMPT+="$(__color - ${LAST_SEGMENT_COLOR})${separator_char}$(__color)"
  PS1="${LEFT_PROMPT} "

  ## cleanup ##
  unset LAST_SEGMENT_COLOR \
        LEFT_PROMPT \
        SEGMENTS_AT_LEFT
}

Then re-run .bashrc:

source ~/.bashrc

And get timing output:

/.bash/themes/git_bash_windows_powerline> > master>>
last_status: 0.389
user_info: 0.069
cwd: 0.069
scm: 2.744

There a few possible avenues for optimisation, but having timing information makes it much easier.

@stuaxo
Copy link
Author

stuaxo commented Oct 2, 2019

Based on this, I've made an easy performance fix for last_status.

#3

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant