qdoc: fix one of the most expensive loops in Qt

QQmlJS::Engine::comments() returns a QList<QQmlJ::AST::SourceLocation> by value.
The QList is horribly inefficient, but that will be topic of a separate patch.

The loop in QmlMarkupVisitor did not store the result of comments() in a local
variable, it called engine->comments() whenever it referenced it, which was
_three_ times per loop iteration. Two of those references applied op[] to
the rvalue engine->comments(), which, being mutable, detaches. _Twice_ per
loop, with a QList that heap-allocates its elements!.

And that was followed by a similar loop.

Fix by using a local const copy of the list to iterate over.

The loop termination condition also looks fishy (j is used to index into
the comments, but is not checked against comments.size()), but apparently
qdoc works fine with it so far, so don't try to fix.

The copy of QQmlJS in QtDeclarative is not affected by this (qdoc-specific
code).

Change-Id: I133c35dc9293609dfb8ad633e2d82399223b508b
Reviewed-by: Olivier Goffart (Woboq GmbH) <ogoffart@woboq.com>
This commit is contained in:
Marc Mutz 2015-06-20 12:42:46 +02:00
parent 55655abfaf
commit cb60c5c66a

View File

@ -54,21 +54,22 @@ QmlMarkupVisitor::QmlMarkupVisitor(const QString &source,
// Merge the lists of locations of pragmas and comments in the source code. // Merge the lists of locations of pragmas and comments in the source code.
int i = 0; int i = 0;
int j = 0; int j = 0;
while (i < engine->comments().length() && j < pragmas.length()) { const QList<QQmlJS::AST::SourceLocation> comments = engine->comments();
if (engine->comments()[i].offset < pragmas[j].offset) { while (i < comments.size() && j < pragmas.length()) {
if (comments[i].offset < pragmas[j].offset) {
extraTypes.append(Comment); extraTypes.append(Comment);
extraLocations.append(engine->comments()[i]); extraLocations.append(comments[i]);
++i; ++i;
} else { } else {
extraTypes.append(Pragma); extraTypes.append(Pragma);
extraLocations.append(engine->comments()[j]); extraLocations.append(comments[j]);
++j; ++j;
} }
} }
while (i < engine->comments().length()) { while (i < comments.size()) {
extraTypes.append(Comment); extraTypes.append(Comment);
extraLocations.append(engine->comments()[i]); extraLocations.append(comments[i]);
++i; ++i;
} }