Skip to content

Commit

Permalink
Speedup the V4 code generation.
Browse files Browse the repository at this point in the history
Skip discarded code and use QVarLengthArray instead of
QByteArray to store the bytecode.

Change-Id: I4c574e7a817595bc8942ed9a927e79339a2d7b40
Reviewed-on: http://codereview.qt.nokia.com/3760
Reviewed-by: Roberto Raggi <roberto.raggi@nokia.com>
Reviewed-by: Qt Sanity Bot <qt_sanity_bot@ovi.com>
  • Loading branch information
Roberto Raggi authored and Qt by Nokia committed Aug 30, 2011
1 parent fcc416d commit 0394323
Show file tree
Hide file tree
Showing 5 changed files with 55 additions and 31 deletions.
32 changes: 19 additions & 13 deletions src/declarative/qml/v4/qdeclarativev4compiler.cpp
Expand Up @@ -85,7 +85,7 @@ void QDeclarativeV4CompilerPrivate::trace(int line, int column)
currentBlockMask = 0x00000001;


for (int i = 0; i < blocks.size(); ++i) {
for (int i = 0; !_discarded && i < blocks.size(); ++i) {
IR::BasicBlock *block = blocks.at(i);
IR::BasicBlock *next = i + 1 < blocks.size() ? blocks.at(i + 1) : 0;
if (IR::Stmt *terminator = block->terminator()) {
Expand Down Expand Up @@ -137,8 +137,10 @@ void QDeclarativeV4CompilerPrivate::trace(int line, int column)
blockop.block(currentBlockMask);
gen(blockop);

foreach (IR::Stmt *s, block->statements)
s->accept(this);
foreach (IR::Stmt *s, block->statements) {
if (! _discarded)
s->accept(this);
}

qSwap(usedSubscriptionIdsChanged, usic);

Expand All @@ -148,7 +150,7 @@ void QDeclarativeV4CompilerPrivate::trace(int line, int column)
return;
}
currentBlockMask <<= 1;
} else {
} else if (! _discarded) {
const int adjust = bytecode.remove(blockopIndex);
// Correct patches
for (int ii = patchesCount; ii < patches.count(); ++ii)
Expand All @@ -164,13 +166,15 @@ void QDeclarativeV4CompilerPrivate::trace(int line, int column)
#endif


// back patching
foreach (const Patch &patch, patches) {
Instr &instr = bytecode[patch.offset];
instr.branchop.offset = patch.block->offset - patch.offset - instr.size();
}
if (! _discarded) {
// back patching
foreach (const Patch &patch, patches) {
Instr &instr = bytecode[patch.offset];
instr.branchop.offset = patch.block->offset - patch.offset - instr.size();
}

patches.clear();
patches.clear();
}
}

void QDeclarativeV4CompilerPrivate::trace(QVector<IR::BasicBlock *> *blocks)
Expand Down Expand Up @@ -1005,7 +1009,7 @@ int QDeclarativeV4CompilerPrivate::commitCompile()
int rv = committed.count();
committed.offsets << committed.bytecode.count();
committed.dependencies << usedSubscriptionIds;
committed.bytecode += bytecode.code();
committed.bytecode.append(bytecode.constData(), bytecode.size());
committed.data = data;
committed.exceptions = exceptions;
committed.subscriptionIds = subscriptionIds;
Expand Down Expand Up @@ -1280,8 +1284,10 @@ QByteArray QDeclarativeV4Compiler::program() const
}


QByteArray bytecode = bc.code();
bytecode += d->committed.bytecode;
QByteArray bytecode;
bytecode.reserve(bc.size() + d->committed.bytecode.size());
bytecode.append(bc.constData(), bc.size());
bytecode.append(d->committed.bytecode.constData(), d->committed.bytecode.size());

QByteArray data = d->committed.data;
while (data.count() % 4) data.append('\0');
Expand Down
8 changes: 2 additions & 6 deletions src/declarative/qml/v4/qdeclarativev4instruction.cpp
Expand Up @@ -512,6 +512,8 @@ void Instr::block(quint32 mask)

Bytecode::Bytecode()
{
d.reserve(8 * 1024);

#ifdef QML_THREADED_INTERPRETER
decodeInstr = QDeclarativeV4Bindings::getDecodeInstrTable();
#endif
Expand All @@ -530,12 +532,6 @@ void Bytecode::append(const Instr &instr)
d.append(it, instr.size());
}

void Bytecode::append(const QVector<Instr> &instrs)
{
foreach (const Instr &i, instrs)
append(i);
}

int Bytecode::remove(int offset)
{
const Instr *instr = (const Instr *) (d.begin() + offset);
Expand Down
13 changes: 10 additions & 3 deletions src/declarative/qml/v4/qdeclarativev4instruction_p.h
Expand Up @@ -56,6 +56,7 @@
#include <QtCore/qglobal.h>
#include <QtCore/qbytearray.h>
#include <QtCore/qvector.h>
#include <QtCore/qvarlengtharray.h>

QT_BEGIN_HEADER

Expand Down Expand Up @@ -414,21 +415,27 @@ class Bytecode
public:
Bytecode();

QByteArray code() const { return d; }
const char *constData() const { return d.constData(); }
int size() const { return d.size(); }
int count() const { return d.count(); }
void clear() { d.clear(); }
bool isEmpty() const { return d.isEmpty(); }
void append(const Instr &instr);
void append(const QVector<Instr> &instrs);

template <typename _It>
void append(_It it, _It last)
{
for (; it != last; ++it)
append(*it);
}

int remove(int index);

const Instr &operator[](int offset) const;
Instr &operator[](int offset);

private:
QByteArray d;
QVarLengthArray<char, 4 * 1024> d;
#ifdef QML_THREADED_INTERPRETER
void **decodeInstr;
#endif
Expand Down
29 changes: 21 additions & 8 deletions src/declarative/qml/v4/qdeclarativev4irbuilder.cpp
Expand Up @@ -136,12 +136,12 @@ QDeclarativeV4IRBuilder::operator()(QDeclarativeJS::IR::Module *module,
return discarded?0:function;
}

bool QDeclarativeV4IRBuilder::buildName(QStringList &name,
bool QDeclarativeV4IRBuilder::buildName(QList<QStringRef> &name,
AST::Node *node,
QList<AST::ExpressionNode *> *nodes)
{
if (node->kind == AST::Node::Kind_IdentifierExpression) {
name << static_cast<AST::IdentifierExpression*>(node)->name.toString();
name << static_cast<AST::IdentifierExpression*>(node)->name;
if (nodes) *nodes << static_cast<AST::IdentifierExpression*>(node);
} else if (node->kind == AST::Node::Kind_FieldMemberExpression) {
AST::FieldMemberExpression *expr =
Expand All @@ -150,7 +150,7 @@ bool QDeclarativeV4IRBuilder::buildName(QStringList &name,
if (!buildName(name, expr->base, nodes))
return false;

name << expr->name.toString();
name << expr->name;
if (nodes) *nodes << expr;
} else {
return false;
Expand Down Expand Up @@ -683,6 +683,11 @@ bool QDeclarativeV4IRBuilder::visit(AST::FieldMemberExpression *ast)
return false;
}

bool QDeclarativeV4IRBuilder::preVisit(AST::Node *ast)
{
return ! _discard;
}

bool QDeclarativeV4IRBuilder::visit(AST::NewMemberExpression *)
{
return false;
Expand All @@ -695,14 +700,22 @@ bool QDeclarativeV4IRBuilder::visit(AST::NewExpression *)

bool QDeclarativeV4IRBuilder::visit(AST::CallExpression *ast)
{
QStringList names;
QList<QStringRef> names;
QList<AST::ExpressionNode *> nameNodes;

names.reserve(4);
nameNodes.reserve(4);

if (buildName(names, ast->base, &nameNodes)) {
//ExprResult base = expression(ast->base);
const QString id = names.join(QLatin1String("."));
const quint32 line = nameNodes.last()->firstSourceLocation().startLine;
const quint32 column = nameNodes.last()->firstSourceLocation().startColumn;
IR::Expr *base = _block->NAME(id, line, column);
QString id;
for (int i = 0; i < names.size(); ++i) {
if (! i)
id += QLatin1Char('.');
id += names.at(i);
}
const AST::SourceLocation loc = nameNodes.last()->firstSourceLocation();
IR::Expr *base = _block->NAME(id, loc.startLine, loc.startColumn);

IR::ExprList *args = 0, **argsInserter = &args;
for (AST::ArgumentList *it = ast->arguments; it; it = it->next) {
Expand Down
4 changes: 3 additions & 1 deletion src/declarative/qml/v4/qdeclarativev4irbuilder_p.h
Expand Up @@ -115,6 +115,8 @@ class QDeclarativeV4IRBuilder : public QDeclarativeJS::AST::Visitor

void implicitCvt(ExprResult &expr, QDeclarativeJS::IR::Type type);

virtual bool preVisit(QDeclarativeJS::AST::Node *ast);

// QML
virtual bool visit(QDeclarativeJS::AST::UiProgram *ast);
virtual bool visit(QDeclarativeJS::AST::UiImportList *ast);
Expand Down Expand Up @@ -220,7 +222,7 @@ class QDeclarativeV4IRBuilder : public QDeclarativeJS::AST::Visitor
virtual bool visit(QDeclarativeJS::AST::DebuggerStatement *ast);

private:
bool buildName(QStringList &name, QDeclarativeJS::AST::Node *node,
bool buildName(QList<QStringRef> &name, QDeclarativeJS::AST::Node *node,
QList<QDeclarativeJS::AST::ExpressionNode *> *nodes);
void discard();

Expand Down

0 comments on commit 0394323

Please sign in to comment.