From 3babea74b2acfae64c9db2a42f33ce2c4cfad445 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Aur=C3=A9lien=20Brooke?= Date: Fri, 18 Jul 2025 12:00:31 +0200 Subject: moc: add lexem() functions that return QByteArrayViews In many cases, the result of the lexem() functions is only used as a view and is not stored. To avoid constructing thousands of temporary QByteArray instances, introduce lexemView() and unquotedLexemView() that return a QByteArrayView instead. We need to carefuly remove .constData() calls on previously-QByteArrays, because QByteArrayViews may not be NUL-terminated, so we must transfer the size information to e.g. QString::fromLocal8bit(). This saves only about 3 % of temporary allocs but enables further optimizations. Change-Id: I55e389f7602ac42d3e2f83b38161286d5c17bb48 Reviewed-by: Fabian Kosmale --- src/tools/moc/preprocessor.cpp | 15 +++++++-------- 1 file changed, 7 insertions(+), 8 deletions(-) (limited to 'src/tools/moc/preprocessor.cpp') diff --git a/src/tools/moc/preprocessor.cpp b/src/tools/moc/preprocessor.cpp index fd2d196a7a5..77c2c66ecdd 100644 --- a/src/tools/moc/preprocessor.cpp +++ b/src/tools/moc/preprocessor.cpp @@ -190,7 +190,7 @@ Symbols Preprocessor::tokenize(const QByteArray& input, int lineNum, Preprocesso const QByteArray newString = '\"' - + symbols.constLast().unquotedLexem() + + symbols.constLast().unquotedLexemView() + input.mid(lexem - begin + 1, data - lexem - 2) + '\"'; symbols.last() = Symbol(symbols.constLast().lineNum, @@ -650,7 +650,7 @@ Symbols Preprocessor::macroExpandIdentifier(Preprocessor *that, SymbolStack &sym const Symbols &arg = arguments.at(index); QByteArray stringified; for (const Symbol &sym : arg) - stringified += sym.lexem(); + stringified += sym.lexemView(); stringified.replace('"', "\\\""); stringified.prepend('"'); @@ -967,9 +967,8 @@ int PP_Expression::primary_expression() test(PP_RPAREN); } else { next(); - const QByteArray &lex = lexem(); - auto lexView = QByteArrayView(lex); - if (lex.endsWith('L')) + auto lexView = lexemView(); + if (lexView.endsWith('L')) lexView.chop(1); value = lexView.toInt(nullptr, 0); } @@ -1120,7 +1119,7 @@ void Preprocessor::preprocess(const QByteArray &filename, Symbols &preprocessed) QByteArray include; bool local = false; if (test(PP_STRING_LITERAL)) { - local = lexem().startsWith('\"'); + local = lexemView().startsWith('\"'); include = unquotedLexem(); } else continue; @@ -1322,7 +1321,7 @@ void Preprocessor::parseDefineArguments(Macro *m) if (t == PP_RPAREN) break; if (t != PP_IDENTIFIER) { - QByteArray l = lexem(); + QByteArrayView l = lexemView(); if (l == "...") { m->isVariadic = true; arguments += Symbol(symbol().lineNum, PP_IDENTIFIER, "__VA_ARGS__"); @@ -1346,7 +1345,7 @@ void Preprocessor::parseDefineArguments(Macro *m) break; if (t == PP_COMMA) continue; - if (lexem() == "...") { + if (lexemView() == "...") { //GCC extension: #define FOO(x, y...) x(y) // The last argument was already parsed. Just mark the macro as variadic. m->isVariadic = true; -- cgit v1.2.3