Skip to content

Optimize *_highlight_string() #383

Open
@psprint

Description

@psprint

Hello
The function _zsh_highlight_main_highlighter_highlight_string() can be significantly optimized (2x) by using patterns rather than char-by-char iteration. Here is a zprof result and the code: https://github.com/psprint/history-search-multi-word/commit/08419957eef0379d6c21a0348bec470c128bf369

Also, not using local variables gives another 30% in my tests (2.8x in total):
https://github.com/psprint/history-search-multi-word/commit/610144c56f90582d7f0bb3595730062ab2d6b332
https://github.com/psprint/history-search-multi-word/commit/4b70d8d9779d58b0f421dfae48390c7c2360b1dc

Current code is:

https://github.com/psprint/history-search-multi-word/blob/78a7f0cf79f630885a56b7e8b8321f795cacf9ab/hsmw-highlight#L636-L656

The pattern matches $0ABC variables, but (#B) flag can be used to save $match entries and accomplish the pattern's goal with 9 (the limit) $match entries.

The pattern:

 while [[ "$mybuf" = (#b)[^\$\\]#((\$[a-zA-Z0-9_]##(\[[^\]]#\])(#c0,1))|(\$[{](\([a-zA-Z0@%#]##\))(#c0,1)[a-zA-Z0-9_]##(\[[^\]]#\])(#c0,1)[}])|[\\][\'\"\$]|[\\](*))(*) ]]; do

is ((A)|(B)|(C)|(D))(*), A matches $var[abc], B matches ${(...)var[abc]}, C matches \$ or \" or \', D matches \* – and the first condition -n ${match[7] uses D to continue searching when backslash-something (not ['"$]) is occured.

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions