zsh-syntax-highlighting icon indicating copy to clipboard operation
zsh-syntax-highlighting copied to clipboard

Review performance of ZSH_HIGHLIGHT_DIRS_BLACKLIST

Open danielshahaf opened this issue 3 years ago • 0 comments

Here's the implementation of ZSH_HIGHLIGHT_DIRS_BLACKLIST:

https://github.com/zsh-users/zsh-syntax-highlighting/blob/e8517244f7d2ae4f9d979faf94608d6e4a74a73e/highlighters/main/main-highlighter.zsh#L1229-L1240

That runs a nested loop for each ordinary (non-option) word on the command line. I suspect that's not very performant.

  • [ ] Confirm whether that's performing acceptably or not

In case that's an issue, ideas:

  • Special case the common case that ZSH_HIGHLIGHT_DIRS_BLACKLIST is empty.
  • Join the blacklist entries into a pattern that uses alternations.
  • Join the blacklist entries with NULs and use plain substring matching

The last option should be something like this:

readonly NUL=$'\0'
local -a t=( "" "${ZSH_HIGHLIGHT_DIRS_BLACKLIST[@]}" "" )
haystack="${(pj.\0.)^ZSH_HIGHLIGHT_DIRS_BLACKLIST[@]%/}/"
needle=${NUL}${expanded_arg%/}/${NUL}
if [[ $haystack == *$needle* ]]; then

That munges both the needle and the haystack by ensuring each path has a trailing slash and is both prefixed and suffixed by NULs (the latter is because /foo shouldn't match /foobar or /baz/foo).

danielshahaf avatar Mar 10 '21 20:03 danielshahaf