From f8a7cb0672a2432ec24dbb6043d3f63ca72f6e93 Mon Sep 17 00:00:00 2001 From: rupa Date: Mon, 12 Dec 2011 02:15:09 -0500 Subject: [PATCH] performance improvement from clvv see https://github.com/clvv/z/commit/4f313ac39eb34ff08a41e5efafbf0d7a7ffcfcd1 --- z.sh | 27 ++++++++++++++++++++++++--- 1 file changed, 24 insertions(+), 3 deletions(-) diff --git a/z.sh b/z.sh index 753a416..91a498f 100755 --- a/z.sh +++ b/z.sh @@ -70,12 +70,23 @@ _z() { # tab completion elif [ "$1" = "--complete" ]; then awk -v q="$2" -F"|" ' + function notdir(path, tmp) { + # faster than system() + n = gsub("/+", "/", path) + for( i = 0; i < n; i++ ) path = path "/.." + path = path datafile + if( ( getline tmp < path ) >= 0 ) { + close(path) + return 0 + } + return 1 + } BEGIN { if( q == tolower(q) ) nocase = 1 split(substr(q,3),fnd," ") } { - if( system("test -d \"" $1 "\"") ) next + if( notdir($1) ) next if( nocase ) { for( i in fnd ) tolower($1) !~ tolower(fnd[i]) && $1 = "" } else { @@ -107,7 +118,17 @@ _z() { [ -f "$datafile" ] || return local cd - cd="$(awk -v t="$(date +%s)" -v list="$list" -v typ="$typ" -v q="$fnd" -F"|" ' + cd="$(awk -v t="$(date +%s)" -v list="$list" -v typ="$typ" -v q="$fnd" -v datafile="$datafile" -F"|" ' + function notdir(path, tmp) { + n = gsub("/+", "/", path) + for( i = 0; i < n; i++ ) path = path "/.." + path = path datafile + if( ( getline tmp < path ) >= 0 ) { + close(path) + return 0 + } + return 1 + } function frecent(rank, time) { dx = t-time if( dx < 3600 ) return rank*4 @@ -139,7 +160,7 @@ _z() { } BEGIN { split(q, a, " ") } { - if( system("test -d \"" $1 "\"") ) next + if( notdir($1) ) next if( typ == "rank" ) { f = $2 } else if( typ == "recent" ) {