From a6b565d5028a27eb86c66be6b5c2c2fb03fa2539 Mon Sep 17 00:00:00 2001 From: ridiculousfish Date: Tue, 3 Mar 2020 01:24:05 -0800 Subject: [PATCH] Optimize parse_util_compute_indents Exploit the fact that most input strings will not contain newlines, in which case we do not have to parse anything. --- src/parse_util.cpp | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/parse_util.cpp b/src/parse_util.cpp index 794b981ea..9b90737e3 100644 --- a/src/parse_util.cpp +++ b/src/parse_util.cpp @@ -677,6 +677,12 @@ std::vector parse_util_compute_indents(const wcstring &src) { const size_t src_size = src.size(); std::vector indents(src_size, -1); + // Simple trick: if our source does not contain a newline, then all indents are 0. + if (src.find('\n') == wcstring::npos) { + std::fill(indents.begin(), indents.end(), 0); + return indents; + } + // Parse the string. We pass continue_after_error to produce a forest; the trailing indent of // the last node we visited becomes the input indent of the next. I.e. in the case of 'switch // foo ; cas', we get an invalid parse tree (since 'cas' is not valid) but we indent it as if it