Skip to content

Commit

Permalink
Reduce memory by not calling QUrl::toString() multiple times
Browse files Browse the repository at this point in the history
Change-Id: I57ce25f4e20cac048ff507a8c195b83adc30040d
Reviewed-by: Roberto Raggi <roberto.raggi@nokia.com>
  • Loading branch information
Aaron Kennedy authored and Qt by Nokia committed Feb 20, 2012
1 parent 0856613 commit d1c2fb2
Show file tree
Hide file tree
Showing 13 changed files with 52 additions and 20 deletions.
2 changes: 1 addition & 1 deletion src/declarative/qml/qdeclarativecompiler.cpp
Expand Up @@ -1644,7 +1644,7 @@ int QDeclarativeCompiler::translationContextIndex()
{
if (cachedTranslationContextIndex == -1) {
// This code must match that in the qsTr() implementation
QString path = output->url.toString();
const QString &path = output->name;
int lastSlash = path.lastIndexOf(QLatin1Char('/'));
QString context = (lastSlash > -1) ? path.mid(lastSlash + 1, path.length()-lastSlash-5) :
QString();
Expand Down
1 change: 1 addition & 0 deletions src/declarative/qml/qdeclarativecontext.cpp
Expand Up @@ -464,6 +464,7 @@ void QDeclarativeContext::setBaseUrl(const QUrl &baseUrl)
Q_D(QDeclarativeContext);

d->data->url = baseUrl;
d->data->urlString = baseUrl.toString();
}

/*!
Expand Down
1 change: 1 addition & 0 deletions src/declarative/qml/qdeclarativecontext_p.h
Expand Up @@ -163,6 +163,7 @@ class Q_DECLARATIVE_EXPORT QDeclarativeContextData

// Context base url
QUrl url;
QString urlString;

// List of imports that apply to this context
QDeclarativeTypeNameCache *imports;
Expand Down
10 changes: 8 additions & 2 deletions src/declarative/qml/qdeclarativeimport.cpp
Expand Up @@ -171,10 +171,16 @@ QDeclarativeImports::~QDeclarativeImports()
/*!
Sets the base URL to be used for all relative file imports added.
*/
void QDeclarativeImports::setBaseUrl(const QUrl& url)
void QDeclarativeImports::setBaseUrl(const QUrl& url, const QString &urlString)
{
d->baseUrl = url;
d->base = url.toString();

if (urlString.isEmpty()) {
d->base = url.toString();
} else {
//Q_ASSERT(url.toString() == urlString);
d->base = urlString;
}
}

/*!
Expand Down
2 changes: 1 addition & 1 deletion src/declarative/qml/qdeclarativeimport_p.h
Expand Up @@ -80,7 +80,7 @@ class Q_DECLARATIVE_EXPORT QDeclarativeImports
~QDeclarativeImports();
QDeclarativeImports &operator=(const QDeclarativeImports &);

void setBaseUrl(const QUrl &url);
void setBaseUrl(const QUrl &url, const QString &urlString = QString());
QUrl baseUrl() const;

bool resolveType(const QString& type,
Expand Down
13 changes: 9 additions & 4 deletions src/declarative/qml/qdeclarativescript.cpp
Expand Up @@ -1287,20 +1287,25 @@ class ParserJsASTData
};
}

bool QDeclarativeScript::Parser::parse(const QByteArray &qmldata, const QUrl &url)
bool QDeclarativeScript::Parser::parse(const QByteArray &qmldata, const QUrl &url,
const QString &urlString)
{
clear();

const QString fileName = url.toString();
_scriptFile = fileName;
if (urlString.isEmpty()) {
_scriptFile = url.toString();
} else {
// Q_ASSERT(urlString == url.toString());
_scriptFile = urlString;
}

QTextStream stream(qmldata, QIODevice::ReadOnly);
#ifndef QT_NO_TEXTCODEC
stream.setCodec("UTF-8");
#endif
QString *code = _pool.NewString(stream.readAll());

data = new QDeclarativeScript::ParserJsASTData(fileName);
data = new QDeclarativeScript::ParserJsASTData(_scriptFile);

Lexer lexer(&data->engine);
lexer.setCode(*code, /*line = */ 1);
Expand Down
3 changes: 2 additions & 1 deletion src/declarative/qml/qdeclarativescript_p.h
Expand Up @@ -477,7 +477,8 @@ class Q_AUTOTEST_EXPORT Parser
Parser();
~Parser();

bool parse(const QByteArray &data, const QUrl &url = QUrl());
bool parse(const QByteArray &data, const QUrl &url = QUrl(),
const QString &urlString = QString());

QList<TypeReference*> referencedTypes() const;

Expand Down
23 changes: 18 additions & 5 deletions src/declarative/qml/qdeclarativetypeloader.cpp
Expand Up @@ -387,6 +387,18 @@ QUrl QDeclarativeDataBlob::finalUrl() const
return m_finalUrl;
}

/*!
Returns the finalUrl() as a string.
*/
QString QDeclarativeDataBlob::finalUrlString() const
{
Q_ASSERT(isCompleteOrError() || (m_manager && m_manager->m_thread->isThisThread()));
if (m_finalUrlString.isEmpty())
m_finalUrlString = m_finalUrl.toString();

return m_finalUrlString;
}

/*!
Return the errors on this blob.
Expand Down Expand Up @@ -1508,12 +1520,12 @@ void QDeclarativeTypeData::completed()

void QDeclarativeTypeData::dataReceived(const QByteArray &data)
{
if (!scriptParser.parse(data, finalUrl())) {
if (!scriptParser.parse(data, finalUrl(), finalUrlString())) {
setError(scriptParser.errors());
return;
}

m_imports.setBaseUrl(finalUrl());
m_imports.setBaseUrl(finalUrl(), finalUrlString());

foreach (const QDeclarativeScript::Import &import, scriptParser.imports()) {
if (import.type == QDeclarativeScript::Import::File && import.qualifier.isEmpty()) {
Expand Down Expand Up @@ -1569,8 +1581,8 @@ void QDeclarativeTypeData::compile()
QDeclarativeProfilerService::startRange(QDeclarativeProfilerService::Compiling);

m_compiledData = new QDeclarativeCompiledData(typeLoader()->engine());
m_compiledData->url = m_imports.baseUrl();
m_compiledData->name = m_compiledData->url.toString();
m_compiledData->url = finalUrl();
m_compiledData->name = finalUrlString();
QDeclarativeProfilerService::rangeLocation(QDeclarativeProfilerService::Compiling, QUrl(m_compiledData->name),1,1);
QDeclarativeProfilerService::rangeData(QDeclarativeProfilerService::Compiling, m_compiledData->name);

Expand Down Expand Up @@ -1817,7 +1829,7 @@ void QDeclarativeScriptBlob::dataReceived(const QByteArray &data)
QDeclarativeScript::Parser::JavaScriptMetaData metadata =
QDeclarativeScript::Parser::extractMetaData(m_source);

m_imports.setBaseUrl(finalUrl());
m_imports.setBaseUrl(finalUrl(), finalUrlString());

m_pragmas = metadata.pragmas;

Expand Down Expand Up @@ -1881,6 +1893,7 @@ void QDeclarativeScriptBlob::done()
QDeclarativeEngine *engine = typeLoader()->engine();
m_scriptData = new QDeclarativeScriptData();
m_scriptData->url = finalUrl();
m_scriptData->urlString = finalUrlString();
m_scriptData->importCache = new QDeclarativeTypeNameCache();

for (int ii = 0; !isError() && ii < m_scripts.count(); ++ii) {
Expand Down
3 changes: 3 additions & 0 deletions src/declarative/qml/qdeclarativetypeloader_p.h
Expand Up @@ -113,6 +113,7 @@ class Q_DECLARATIVE_PRIVATE_EXPORT QDeclarativeDataBlob : public QDeclarativeRef

QUrl url() const;
QUrl finalUrl() const;
QString finalUrlString() const;

QList<QDeclarativeError> errors() const;

Expand Down Expand Up @@ -166,6 +167,7 @@ class Q_DECLARATIVE_PRIVATE_EXPORT QDeclarativeDataBlob : public QDeclarativeRef

QUrl m_url;
QUrl m_finalUrl;
mutable QString m_finalUrlString;

// List of QDeclarativeDataBlob's that are waiting for me to complete.
QList<QDeclarativeDataBlob *> m_waitingOnMe;
Expand Down Expand Up @@ -355,6 +357,7 @@ class Q_AUTOTEST_EXPORT QDeclarativeScriptData : public QDeclarativeCleanup,
~QDeclarativeScriptData();

QUrl url;
QString urlString;
QDeclarativeTypeNameCache *importCache;
QList<QDeclarativeScriptBlob *> scripts;
QDeclarativeScript::Object::ScriptBlock::Pragmas pragmas;
Expand Down
5 changes: 3 additions & 2 deletions src/declarative/qml/qdeclarativevme.cpp
Expand Up @@ -420,6 +420,7 @@ QObject *QDeclarativeVME::run(QList<QDeclarativeError> *errors,
CTXT = new QDeclarativeContextData;
CTXT->isInternal = true;
CTXT->url = COMP->url;
CTXT->urlString = COMP->name;
CTXT->imports = COMP->importCache;
CTXT->imports->addref();
CTXT->setParent(parentCtxt);
Expand Down Expand Up @@ -1085,8 +1086,7 @@ void QDeclarativeScriptData::initialize(QDeclarativeEngine *engine)

// If compilation throws an error, a surrounding v8::TryCatch will record it.
v8::Local<v8::Script> program = v8engine->qmlModeCompile(m_programSource.constData(),
m_programSource.length(), url.toString(),
1);
m_programSource.length(), urlString, 1);
if (program.IsEmpty())
return;

Expand Down Expand Up @@ -1122,6 +1122,7 @@ v8::Persistent<v8::Object> QDeclarativeVME::run(QDeclarativeContextData *parentC
else
ctxt->isPragmaLibraryContext = parentCtxt->isPragmaLibraryContext;
ctxt->url = script->url;
ctxt->urlString = script->urlString;

// For backward compatibility, if there are no imports, we need to use the
// imports from the parent context. See QTBUG-17518.
Expand Down
5 changes: 3 additions & 2 deletions src/declarative/qml/qdeclarativevmemetaobject.cpp
Expand Up @@ -796,8 +796,9 @@ v8::Handle<v8::Function> QDeclarativeVMEMetaObject::method(int index)
// XXX We should evaluate all methods in a single big script block to
// improve the call time between dynamic methods defined on the same
// object
v8methods[index] = QDeclarativeExpressionPrivate::evalFunction(ctxt, object, body, bodyLength,
ctxt->url.toString(),
v8methods[index] = QDeclarativeExpressionPrivate::evalFunction(ctxt, object, body,
bodyLength,
ctxt->urlString,
data->lineNumber);
}

Expand Down
2 changes: 1 addition & 1 deletion src/declarative/qml/v4/qv4bindings.cpp
Expand Up @@ -299,7 +299,7 @@ void QV4Bindings::run(Binding *binding, QDeclarativePropertyPrivate::WriteFlags
trace.addDetail("Line", binding->line);
trace.addDetail("Column", binding->column);

QDeclarativeBindingProfiler prof(context->url.toString(), binding->line, binding->column);
QDeclarativeBindingProfiler prof(context->urlString, binding->line, binding->column);

if (binding->updating) {
QString name;
Expand Down
2 changes: 1 addition & 1 deletion src/quick/util/qdeclarativeconnections.cpp
Expand Up @@ -277,7 +277,7 @@ void QDeclarativeConnections::connectSignals()
if (ddata) {
ctxtdata = ddata->outerContext;
if (ctxtdata && !ctxtdata->url.isEmpty())
location = ddata->outerContext->url.toString();
location = ddata->outerContext->urlString;
}

QDeclarativeExpression *expression = ctxtdata ?
Expand Down

0 comments on commit d1c2fb2

Please sign in to comment.