From b7e9d9bae301ecf1e803c133b094e0cd708b730b Mon Sep 17 00:00:00 2001 From: Johannes Altmanninger Date: Sun, 28 May 2023 22:55:46 +0200 Subject: [PATCH] rc detection modeline: optimize modeline pre-filtering modeline-parse leads by matching an expensive regex against the entire buffer, which can take a long time on huge files. Perl takes too long on this regex and it seems not even ripgrep optimizes the \z component $ ruby -e '10000.times { puts "a" * 10000 }' > big $ time rg --multiline --only-matching '\A(.+\n){1,5}|(.+\n){1,5}\z' big | wc -l 10 __________________________ Executed in 419.81 millis usr time 399.84 millis sys time 20.78 millis where $ time kak big -e q __________________________ Executed in 179.19 millis usr time 133.61 millis sys time 53.50 millis Let's lose the regex. Fixes #4911 --- rc/detection/modeline.kak | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/rc/detection/modeline.kak b/rc/detection/modeline.kak index a2c5fef7..6e067b1a 100644 --- a/rc/detection/modeline.kak +++ b/rc/detection/modeline.kak @@ -114,9 +114,10 @@ define-command -hidden modeline-parse-impl %{ # [text]{white}{vi:|vim:|ex:}[white]{options} # [text]{white}{vi:|vim:|Vim:|ex:}[white]se[t] {options}:[text] define-command modeline-parse -docstring "Read and interpret vi-format modelines at the beginning/end of the buffer" %{ - try %{ evaluate-commands -draft %{ - execute-keys "s(?S)\A(.+\n){,%opt{modelines}}|(.+\n){,%opt{modelines}}\z" \ - s^\S*?\s+?\w+:\s?[^\n]+ x + try %{ evaluate-commands -draft -save-regs ^ %{ + execute-keys -save-regs "" gk %opt{modelines} JK x Z + execute-keys gj %opt{modelines} KJ x a + execute-keys s^\S*?\s+?\w+:\s?[^\n]+ x evaluate-commands -draft -itersel modeline-parse-impl } } }