*highlight: Optimization – a hash of token types instead of 3 arrays

./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
This commit is contained in:
Sebastian Gniazdowski 2016-10-25 15:08:23 +02:00
parent 0ab5b98062
commit 7eaa9f26aa

View file

@ -61,6 +61,55 @@ typeset -gA HSMW_HIGHLIGHT_STYLES
: ${HSMW_HIGHLIGHT_STYLES[redirection]:=none} : ${HSMW_HIGHLIGHT_STYLES[redirection]:=none}
: ${HSMW_HIGHLIGHT_STYLES[comment]:=fg=black,bold} : ${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. # Get the type of a command.
# #
# Uses the zsh/parameter module if available to avoid forks, and a # 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 ## Variable declarations and initializations
local start_pos=0 end_pos highlight_glob=true arg style local start_pos=0 end_pos highlight_glob=true arg style
local in_array_assignment=false # true between 'a=(' and the matching ')' 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 ^ integer arg_type=0 # Can be 0, 1, 2 or 3 - look up ^
local -a options_to_set # used in callees local -a options_to_set # used in callees
local buf="$1" local buf="$1"
@ -182,37 +228,6 @@ typeset -gA HSMW_HIGHLIGHT_STYLES
fi fi
unset path_dirs_was_set 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 -a match mbegin mend
local MATCH; integer MBEGIN MEND local MATCH; integer MBEGIN MEND
@ -318,15 +333,8 @@ typeset -gA HSMW_HIGHLIGHT_STYLES
((start_pos+=offset)) ((start_pos+=offset))
((end_pos=start_pos+${#arg})) ((end_pos=start_pos+${#arg}))
if [[ -n ${__HSMW_HIGHLIGHT_TOKENS_PRECOMMANDS[(r)$arg]} ]]; then # No-hit will result in value 0
arg_type=1 arg_type=${__HSMW_HIGHLIGHT_TOKENS_TYPES[$arg]}
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
fi fi
proc_buf="${proc_buf[offset + $#arg + 1,len]}" proc_buf="${proc_buf[offset + $#arg + 1,len]}"
@ -444,7 +452,7 @@ typeset -gA HSMW_HIGHLIGHT_STYLES
style=alias style=alias
-hsmw-highlight-resolve-alias $arg -hsmw-highlight-resolve-alias $arg
local alias_target="$REPLY" 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 fi
} }
;; ;;