From a50647e77b0029aef2cba28a56bfb72a9f7cb6bc Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Mon, 4 May 2020 19:42:31 +0000 Subject: [PATCH 1/3] tests: Add a performance testing script, for measuring the performance of the 'main' highlighter on a large file. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit % git co HEAD^ && repeat 3 { zsh -f tests/test-zprof.zsh main | tee … | grep -w _zsh_highlight | head -n1 } HEAD is now at f1948df tests: Add a performance testing script, for measuring the performance of the 'main' highlighter on a large file. 19) 1 34378.97 34378.97 100.00% 5.43 5.43 0.02% _zsh_highlight 19) 1 34058.34 34058.34 100.00% 5.50 5.50 0.02% _zsh_highlight 19) 1 34364.80 34364.80 100.00% 5.36 5.36 0.02% _zsh_highlight --- tests/test-zprof.zsh | 77 ++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 77 insertions(+) create mode 100755 tests/test-zprof.zsh diff --git a/tests/test-zprof.zsh b/tests/test-zprof.zsh new file mode 100755 index 0000000..2538f7d --- /dev/null +++ b/tests/test-zprof.zsh @@ -0,0 +1,77 @@ +#!/usr/bin/env zsh +# ------------------------------------------------------------------------------------------------- +# Copyright (c) 2010-2015 zsh-syntax-highlighting contributors +# All rights reserved. +# +# Redistribution and use in source and binary forms, with or without modification, are permitted +# provided that the following conditions are met: +# +# * Redistributions of source code must retain the above copyright notice, this list of conditions +# and the following disclaimer. +# * Redistributions in binary form must reproduce the above copyright notice, this list of +# conditions and the following disclaimer in the documentation and/or other materials provided +# with the distribution. +# * Neither the name of the zsh-syntax-highlighting contributors nor the names of its contributors +# may be used to endorse or promote products derived from this software without specific prior +# written permission. +# +# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR +# IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND +# FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR +# CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL +# DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, +# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER +# IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT +# OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. +# ------------------------------------------------------------------------------------------------- +# -*- mode: zsh; sh-indentation: 2; indent-tabs-mode: nil; sh-basic-offset: 2; -*- +# vim: ft=zsh sw=2 ts=2 et +# ------------------------------------------------------------------------------------------------- + +# Load the main script. +. ${0:h:h}/zsh-syntax-highlighting.zsh + +# Activate the highlighter. +ZSH_HIGHLIGHT_HIGHLIGHTERS=(main) + +source_file=0.7.1:highlighters/$1/$1-highlighter.zsh + +# Runs a highlighting test +# $1: data file +run_test_internal() { + setopt interactivecomments + + local -a highlight_zone + + local tests_tempdir="$1"; shift + local srcdir="$PWD" + builtin cd -q -- "$tests_tempdir" || { echo >&2 "Bail out! cd failed: $?"; return 1 } + + # Load the data and prepare checking it. + PREBUFFER= + BUFFER=$(cd -- "$srcdir" && git cat-file blob $source_file) + expected_region_highlight=() + + zmodload zsh/zprof + zprof -c + # Set $? for _zsh_highlight + true && _zsh_highlight + zprof +} + +run_test() { + # Do not combine the declaration and initialization: «local x="$(false)"» does not set $?. + local __tests_tempdir + __tests_tempdir="$(mktemp -d)" && [[ -d $__tests_tempdir ]] || { + echo >&2 "Bail out! mktemp failed"; return 1 + } + typeset -r __tests_tempdir # don't allow tests to override the variable that we will 'rm -rf' later on + + { + (run_test_internal "$__tests_tempdir" "$@") + } always { + rm -rf -- "$__tests_tempdir" + } +} + +run_test From 700c0e18fea594b16358726d00ec06445b7ead37 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Mon, 4 May 2020 20:00:01 +0000 Subject: [PATCH 2/3] 'main': Optimize a hot path. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit % git co HEAD^ && repeat 3 { zsh -f tests/test-zprof.zsh main | tee … | grep -w _zsh_highlight | head -n1 } HEAD is now at 64e3651 'main': Optimize a hot path. 19) 1 28765.13 28765.13 100.00% 5.57 5.57 0.02% _zsh_highlight 19) 1 28566.46 28566.46 100.00% 5.91 5.91 0.02% _zsh_highlight 19) 1 28248.12 28248.12 100.00% 5.57 5.57 0.02% _zsh_highlight ---- This commit has been rebased. The above statistics were measured after the rebase. The below statistics had been measured before the rebase. Before this patch: num calls time self name ----------------------------------------------------------------------------------- 1) 3 33410.81 11136.94 98.51% 19277.07 6425.69 56.84% _zsh_highlight_main_highlighter_highlight_list 19) 1 33916.21 33916.21 100.00% 5.27 5.27 0.02% _zsh_highlight With this patch: num calls time self name ----------------------------------------------------------------------------------- 1) 3 27167.49 9055.83 98.17% 18754.77 6251.59 67.77% _zsh_highlight_main_highlighter_highlight_list 19) 1 27674.40 27674.40 100.00% 5.39 5.39 0.02% _zsh_highlight And if test-zprof.zsh is changed to not set interactivecomments: num calls time self name ----------------------------------------------------------------------------------- 1) 13360 36029.12 2.70 83.56% 30304.23 2.27 70.28% _zsh_highlight_main_highlighter_highlight_argument 21) 1 43117.76 43117.76 100.00% 4.52 4.52 0.01% _zsh_highlight num calls time self name ----------------------------------------------------------------------------------- 1) 13360 14782.89 1.11 68.12% 9163.42 0.69 42.23% _zsh_highlight_main_highlighter_highlight_argument 21) 1 21699.93 21699.93 100.00% 4.17 4.17 0.02% _zsh_highlight --- highlighters/main/main-highlighter.zsh | 5 +++++ 1 file changed, 5 insertions(+) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 66a3d2a..903c9f5 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -1311,7 +1311,12 @@ _zsh_highlight_main_highlighter_highlight_argument() fi esac + # This loop is a hot path. Keep it fast! for (( ; i <= $#arg ; i += 1 )); do + if [[ $arg[$i] != [\\\'\"\`\$\<\>\*\?] ]]; then + continue + fi + case "$arg[$i]" in "\\") (( i += 1 )); continue;; "'") From 8f5d74d2190c124110cde3e240eb703b2594dca4 Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Mon, 4 May 2020 20:08:49 +0000 Subject: [PATCH 3/3] 'main': Further optimize argument parsing. MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit % repeat 3 { zsh -f tests/test-zprof.zsh main | tee … | grep -w _zsh_highlight | head -n1 } 18) 1 26895.86 26895.86 100.00% 6.35 6.35 0.02% _zsh_highlight 19) 1 27399.11 27399.11 100.00% 5.52 5.52 0.02% _zsh_highlight 19) 1 27027.58 27027.58 100.00% 5.66 5.66 0.02% _zsh_highlight ---- This commit has been rebased. The above statistics were measured after the rebase. The below statistics had been measured before the rebase. num calls time self name ----------------------------------------------------------------------------------- 1) 3 25689.17 8563.06 98.15% 18422.01 6140.67 70.38% _zsh_highlight_main_highlighter_highlight_list 2) 32390 5706.13 0.18 21.80% 2315.68 0.07 8.85% _zsh_highlight_main_highlighter_highlight_argument 19) 1 26173.33 26173.33 100.00% 5.27 5.27 0.02% _zsh_highlight Interestingly, if I make the change in this diff to _zsh_highlight_main_highlighter_highlight_double_quote — > diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh > index da6ab2b..bb17618 100644 > --- a/highlighters/main/main-highlighter.zsh > +++ b/highlighters/main/main-highlighter.zsh > @@ -1462,10 +1462,13 @@ _zsh_highlight_main_highlighter_highlight_double_quote() > local i j k ret style > reply=() > > - for (( i = $1 + 1 ; i <= $#arg ; i += 1 )) ; do > + (( i = $1 )) > + while (( ++i <= $#arg )); do > + i=${arg[(ib.i.)[\"\`\$\\\\${histchars[1]}]]} > (( j = i + start_pos - 1 )) > (( k = j + 1 )) > case "$arg[$i]" in > + ("") break;; > ('"') break;; > ('`') saved_reply=($reply) > _zsh_highlight_main_highlighter_highlight_backtick $i — it actually makes things measurably slower (!), even on input that has a large number of pasted double-quoted strings: on «BUFFER=": ${(r.8*1500..foo"bar".):-}"» the slowdown is (1123.24ms / 1091.06ms = 1.0295). Therefore, I won't be committing that change. --- highlighters/main/main-highlighter.zsh | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/highlighters/main/main-highlighter.zsh b/highlighters/main/main-highlighter.zsh index 903c9f5..da6ab2b 100644 --- a/highlighters/main/main-highlighter.zsh +++ b/highlighters/main/main-highlighter.zsh @@ -1312,12 +1312,11 @@ _zsh_highlight_main_highlighter_highlight_argument() esac # This loop is a hot path. Keep it fast! - for (( ; i <= $#arg ; i += 1 )); do - if [[ $arg[$i] != [\\\'\"\`\$\<\>\*\?] ]]; then - continue - fi - + (( --i )) + while (( ++i <= $#arg )); do + i=${arg[(ib.i.)[\\\'\"\`\$\<\>\*\?]]} case "$arg[$i]" in + "") break;; "\\") (( i += 1 )); continue;; "'") _zsh_highlight_main_highlighter_highlight_single_quote $i