aboutsummaryrefslogtreecommitdiff
path: root/src/nvim/match.c
diff options
context:
space:
mode:
authorzeertzjq <zeertzjq@outlook.com>2022-10-01 21:59:53 +0800
committerzeertzjq <zeertzjq@outlook.com>2022-10-02 07:28:38 +0800
commitcb310d2901a0eb63721ac5930daaadee91929208 (patch)
tree9fa048c20fb0f182c42c490f0dfa5c8e81ffc082 /src/nvim/match.c
parent85c7d4f7a92326dcd70317b048bafe96c8051701 (diff)
downloadrneovim-cb310d2901a0eb63721ac5930daaadee91929208.tar.gz
rneovim-cb310d2901a0eb63721ac5930daaadee91929208.tar.bz2
rneovim-cb310d2901a0eb63721ac5930daaadee91929208.zip
vim-patch:9.0.0622: matchaddpos() can get slow when adding many matches
Problem: matchaddpos() can get slow when adding many matches. Solution: Update the next available match ID when manually picking an ID and remove check if the available ID can be used. (idea by Rick Howe) https://github.com/vim/vim/commit/9f573a8df02d9f699a43d2afbd1d2841d700b9ad
Diffstat (limited to 'src/nvim/match.c')
-rw-r--r--src/nvim/match.c30
1 files changed, 14 insertions, 16 deletions
diff --git a/src/nvim/match.c b/src/nvim/match.c
index e06643d998..b422dc0ba8 100644
--- a/src/nvim/match.c
+++ b/src/nvim/match.c
@@ -58,16 +58,26 @@ static int match_add(win_T *wp, const char *const grp, const char *const pat, in
(int64_t)id);
return -1;
}
- if (id != -1) {
- cur = wp->w_match_head;
- while (cur != NULL) {
+ if (id == -1) {
+ // use the next available match ID
+ id = wp->w_next_match_id++;
+ } else {
+ // check the given ID is not already in use
+ for (cur = wp->w_match_head; cur != NULL; cur = cur->mit_next) {
if (cur->mit_id == id) {
semsg(_("E801: ID already taken: %" PRId64), (int64_t)id);
return -1;
}
- cur = cur->mit_next;
+ }
+
+ // Make sure the next match ID is always higher than the highest
+ // manually selected ID. Add some extra in case a few more IDs are
+ // added soon.
+ if (wp->w_next_match_id < id + 100) {
+ wp->w_next_match_id = id + 100;
}
}
+
if ((hlg_id = syn_check_group(grp, strlen(grp))) == 0) {
return -1;
}
@@ -76,18 +86,6 @@ static int match_add(win_T *wp, const char *const grp, const char *const pat, in
return -1;
}
- // Find available match ID.
- while (id == -1) {
- cur = wp->w_match_head;
- while (cur != NULL && cur->mit_id != wp->w_next_match_id) {
- cur = cur->mit_next;
- }
- if (cur == NULL) {
- id = wp->w_next_match_id;
- }
- wp->w_next_match_id++;
- }
-
// Build new match.
m = xcalloc(1, sizeof(matchitem_T));
if (pos_list != NULL) {