From cdd7f899cb9aae7b5a39729d3b5aab33f89b4f6f Mon Sep 17 00:00:00 2001 From: Daniel Shahaf Date: Tue, 14 Jul 2020 20:15:50 +0000 Subject: [PATCH] brackets: Optimize the character iteration Test case: See the first comment of issue #295. Before: num calls time self name ----------------------------------------------------------------------------------- 1) 14 284.67 20.33 82.84% 284.67 20.33 82.84% _zsh_highlight_highlighter_brackets_paint 2) 15 313.72 20.91 91.30% 26.62 1.77 7.75% _zsh_highlight 3) 14 318.68 22.76 92.74% 7.41 0.53 2.16% _zsh_highlight_call_widget After: num calls time self name ----------------------------------------------------------------------------------- 1) 15 57.25 3.82 45.33% 28.30 1.89 22.41% _zsh_highlight 2) 14 81.96 5.85 64.90% 27.02 1.93 21.40% _zsh_highlight_call_widget 3) 14 26.31 1.88 20.84% 26.31 1.88 20.84% _zsh_highlight_highlighter_brackets_paint 4) 2 10.66 5.33 8.44% 8.97 4.49 7.10% VCS_INFO_formats 5) 2 25.92 12.96 20.52% 7.72 3.86 6.11% VCS_INFO_get_data_git (That's not in my everyday configuration but in my minimal testing setup, which runs vcs_info with the default settings.) --- changelog.md | 2 ++ highlighters/brackets/brackets-highlighter.zsh | 5 +++-- 2 files changed, 5 insertions(+), 2 deletions(-) diff --git a/changelog.md b/changelog.md index daeda05..cc3153e 100644 --- a/changelog.md +++ b/changelog.md @@ -112,6 +112,8 @@ to issue #418. revision zsh-5.8-172-gdd6e702ee or newer is also fine.) [#418, https://github.com/okapia/zsh-viexchange/issues/1] +- Improve performance of the `brackets` highlighter. + # Changes in version 0.7.1 diff --git a/highlighters/brackets/brackets-highlighter.zsh b/highlighters/brackets/brackets-highlighter.zsh index fc71f2a..bc388fd 100644 --- a/highlighters/brackets/brackets-highlighter.zsh +++ b/highlighters/brackets/brackets-highlighter.zsh @@ -51,8 +51,9 @@ _zsh_highlight_highlighter_brackets_paint() local -A levelpos lastoflevel matching # Find all brackets and remember which one is matching - for (( pos = 1; pos <= buflen; pos++ )) ; do - char=$BUFFER[pos] + pos=0 + for char in ${(s..)BUFFER} ; do + (( ++pos )) case $char in ["([{"]) levelpos[$pos]=$((++level))