From a5f53dccb7cab0bf4d5292dbad5e624690bb4a3b Mon Sep 17 00:00:00 2001 From: Maxime Coste Date: Wed, 25 Jul 2018 20:58:07 +1000 Subject: [PATCH] Micro optimize command parsing by avoiding utf8 decoding Balanced quoted parsing does not need to decode utf8, neither does unquoted word parsing. This improves startup time a bit, helping for issue #2152 --- src/command_manager.cc | 18 ++++++++---------- 1 file changed, 8 insertions(+), 10 deletions(-) diff --git a/src/command_manager.cc b/src/command_manager.cc index 7c6c669d..91b5fb82 100644 --- a/src/command_manager.cc +++ b/src/command_manager.cc @@ -105,8 +105,8 @@ QuotedResult parse_quoted(Reader& reader, Codepoint delimiter) return {str, false}; } -QuotedResult parse_quoted_balanced(Reader& reader, Codepoint opening_delimiter, - Codepoint closing_delimiter) +QuotedResult parse_quoted_balanced(Reader& reader, char opening_delimiter, + char closing_delimiter) { kak_assert(utf8::codepoint(utf8::previous(reader.pos, reader.str.begin()), reader.str.end()) == opening_delimiter); @@ -114,16 +114,16 @@ QuotedResult parse_quoted_balanced(Reader& reader, Codepoint opening_delimiter, auto start = reader.pos; while (reader) { - const Codepoint c = *reader; + const char c = *reader.pos; if (c == opening_delimiter) ++level; else if (c == closing_delimiter and level-- == 0) { auto content = reader.substr_from(start); - ++reader; + ++reader.pos; return {content.str(), true}; } - ++reader; + ++reader.pos; } return {reader.substr_from(start).str(), false}; } @@ -132,15 +132,14 @@ String parse_unquoted(Reader& reader) { auto beg = reader.pos; String str; - bool was_antislash = false; while (reader) { - const Codepoint c = *reader; + const char c = *reader.pos; if (is_command_separator(c) or is_horizontal_blank(c)) { str += reader.substr_from(beg); - if (was_antislash) + if (reader.pos != reader.str.begin() and *(reader.pos - 1) == '\\') { str.back() = c; beg = reader.pos+1; @@ -148,8 +147,7 @@ String parse_unquoted(Reader& reader) else return str; } - was_antislash = c == '\\'; - ++reader; + ++reader.pos; } if (beg < reader.str.end()) str += reader.substr_from(beg);