From 7eaa9f26aa2da2d063b3cd2838161fbb301fcf95 Mon Sep 17 00:00:00 2001 From: Sebastian Gniazdowski Date: Tue, 25 Oct 2016 15:08:23 +0200 Subject: [PATCH] =?UTF-8?q?*highlight:=20Optimization=20=E2=80=93=20a=20ha?= =?UTF-8?q?sh=20of=20token=20types=20instead=20of=203=20arrays?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit ./parse -oo, before changes: Running time: 2.0874240000 num calls time self name ----------------------------------------------------------------------------------- 1) 350 2079,89 5,94 100,00% 1591,12 4,55 76,50% -hsmw-highlight-process 2) 2800 342,91 0,12 16,49% 342,91 0,12 16,49% -hsmw-highlight-string 3) 2450 85,09 0,03 4,09% 85,09 0,03 4,09% -hsmw-highlight-check-path 4) 1400 42,54 0,03 2,05% 42,54 0,03 2,05% -hsmw-highlight-main-type 5) 350 9,98 0,03 0,48% 9,98 0,03 0,48% -hsmw-highlight-stack-pop 6) 350 8,24 0,02 0,40% 8,24 0,02 0,40% -hsmw-highlight-path-separators 7) 1 0,01 0,01 0,00% 0,01 0,01 0,00% -hsmw-highlight-init ./parse -oo, after changes: Running time: 1.9223140000 num calls time self name ----------------------------------------------------------------------------------- 1) 350 1914,66 5,47 100,00% 1435,29 4,10 74,96% -hsmw-highlight-process 2) 2800 340,12 0,12 17,76% 340,12 0,12 17,76% -hsmw-highlight-string 3) 2450 81,34 0,03 4,25% 81,34 0,03 4,25% -hsmw-highlight-check-path 4) 1400 39,98 0,03 2,09% 39,98 0,03 2,09% -hsmw-highlight-main-type 5) 350 9,87 0,03 0,52% 9,87 0,03 0,52% -hsmw-highlight-stack-pop 6) 350 8,05 0,02 0,42% 8,05 0,02 0,42% -hsmw-highlight-path-separators 7) 1 0,01 0,01 0,00% 0,01 0,01 0,00% -hsmw-highlight-init --- hsmw-highlight | 96 +++++++++++++++++++++++++++----------------------- 1 file changed, 52 insertions(+), 44 deletions(-) diff --git a/hsmw-highlight b/hsmw-highlight index 4757956..06fe0bb 100644 --- a/hsmw-highlight +++ b/hsmw-highlight @@ -61,6 +61,55 @@ typeset -gA HSMW_HIGHLIGHT_STYLES : ${HSMW_HIGHLIGHT_STYLES[redirection]:=none} : ${HSMW_HIGHLIGHT_STYLES[comment]:=fg=black,bold} + +typeset -A __HSMW_HIGHLIGHT_TOKENS_TYPES + +__HSMW_HIGHLIGHT_TOKENS_TYPES=( + + # Precommand + + 'builtin' 1 + 'command' 1 + 'exec' 1 + 'nocorrect' 1 + 'noglob' 1 + 'pkexec' 1 # immune to #121 because it's usually not passed --option flags + + # Control flow + # Tokens that, at (naively-determined) "command position", are followed by + # a de jure command position. All of these are reserved words. + + $'\x7b' 2 # block + $'\x28' 2 # subshell + '()' 2 # anonymous function + 'while' 2 + 'until' 2 + 'if' 2 + 'then' 2 + 'elif' 2 + 'else' 2 + 'do' 2 + 'time' 2 + 'coproc' 2 + '!' 2 # reserved word; unrelated to $histchars[1] + + # Command separators + + '|' 3 + '||' 3 + ';' 3 + '&' 3 + '&&' 3 + '|&' 3 + '&!' 3 + '&|' 3 + # ### 'case' syntax, but followed by a pattern, not by a command + # ';;' ';&' ';|' +) + + + + # Get the type of a command. # # Uses the zsh/parameter module if available to avoid forks, and a @@ -167,9 +216,6 @@ typeset -gA HSMW_HIGHLIGHT_STYLES ## Variable declarations and initializations local start_pos=0 end_pos highlight_glob=true arg style local in_array_assignment=false # true between 'a=(' and the matching ')' - typeset -a __HSMW_HIGHLIGHT_TOKENS_PRECOMMANDS - typeset -a __HSMW_HIGHLIGHT_TOKENS_CONTROL_FLOW - typeset -a __HSMW_HIGHLIGHT_TOKENS_COMMANDSEPARATOR integer arg_type=0 # Can be 0, 1, 2 or 3 - look up ^ local -a options_to_set # used in callees local buf="$1" @@ -182,37 +228,6 @@ typeset -gA HSMW_HIGHLIGHT_STYLES fi unset path_dirs_was_set - __HSMW_HIGHLIGHT_TOKENS_PRECOMMANDS=( - 'builtin' 'command' 'exec' 'nocorrect' 'noglob' - 'pkexec' # immune to #121 because it's usually not passed --option flags - ) - - # Tokens that, at (naively-determined) "command position", are followed by - # a de jure command position. All of these are reserved words. - __HSMW_HIGHLIGHT_TOKENS_CONTROL_FLOW=( - $'\x7b' # block - $'\x28' # subshell - '()' # anonymous function - 'while' - 'until' - 'if' - 'then' - 'elif' - 'else' - 'do' - 'time' - 'coproc' - '!' # reserved word; unrelated to $histchars[1] - ) - - __HSMW_HIGHLIGHT_TOKENS_COMMANDSEPARATOR=( - '|' '||' ';' '&' '&&' - '|&' - '&!' '&|' - # ### 'case' syntax, but followed by a pattern, not by a command - # ';;' ';&' ';|' - ) - local -a match mbegin mend local MATCH; integer MBEGIN MEND @@ -318,15 +333,8 @@ typeset -gA HSMW_HIGHLIGHT_STYLES ((start_pos+=offset)) ((end_pos=start_pos+${#arg})) - if [[ -n ${__HSMW_HIGHLIGHT_TOKENS_PRECOMMANDS[(r)$arg]} ]]; then - arg_type=1 - elif [[ -n ${__HSMW_HIGHLIGHT_TOKENS_CONTROL_FLOW[(r)$arg]} ]]; then - arg_type=2 - elif [[ -n ${__HSMW_HIGHLIGHT_TOKENS_COMMANDSEPARATOR[(r)$arg]} ]]; then - arg_type=3 - else - arg_type=0 - fi + # No-hit will result in value 0 + arg_type=${__HSMW_HIGHLIGHT_TOKENS_TYPES[$arg]} fi proc_buf="${proc_buf[offset + $#arg + 1,len]}" @@ -444,7 +452,7 @@ typeset -gA HSMW_HIGHLIGHT_STYLES style=alias -hsmw-highlight-resolve-alias $arg local alias_target="$REPLY" - [[ -n ${(M)__HSMW_HIGHLIGHT_TOKENS_PRECOMMANDS:#"$alias_target"} && "$arg_type" != "1" ]] && __HSMW_HIGHLIGHT_TOKENS_PRECOMMANDS+=($arg) + [[ ${__HSMW_HIGHLIGHT_TOKENS_TYPES[$alias_target]} = "1" && "$arg_type" != "1" ]] && __HSMW_HIGHLIGHT_TOKENS_TYPES[$arg]="1" fi } ;;