From afb54fe253ded94152575b23539552aa8f4ad384 Mon Sep 17 00:00:00 2001 From: Kent Hansen Date: Thu, 15 Apr 2010 12:09:30 +0200 Subject: Speed up qsTr() by caching the translation context qsTr() uses QFileInfo::baseName() to determine the translation context from a URL. The problem is that creating a QFileInfo object (and thus a file engine object), as well as processing the URL to determine the base name part, is very expensive. By caching the last translation URL and context, qsTr() becomes 5x faster. Only the most recent URL is cached, because a script's text will be translated all at once (as the script is evaluated), so for scripts with multiple translated strings only the first qsTr() call will cause a cache miss. The performance could be improved even further by getting rid of the QFileInfo dependency altogether; created QTBUG-9939 for that since it's a more risky change. Task-number: QTBUG-6908 Reviewed-by: Olivier Goffart --- src/script/api/qscriptengine.cpp | 14 +++++++++++++- src/script/api/qscriptengine_p.h | 7 ++++++- 2 files changed, 19 insertions(+), 2 deletions(-) diff --git a/src/script/api/qscriptengine.cpp b/src/script/api/qscriptengine.cpp index 58c8d83..f732907 100644 --- a/src/script/api/qscriptengine.cpp +++ b/src/script/api/qscriptengine.cpp @@ -792,6 +792,7 @@ JSC::JSValue JSC_HOST_CALL functionQsTr(JSC::ExecState *exec, JSC::JSObject*, JS if ((args.size() > 2) && !args.at(2).isNumber()) return JSC::throwError(exec, JSC::GeneralError, "qsTranslate(): third argument (n) must be a number"); #ifndef QT_NO_QOBJECT + QScriptEnginePrivate *engine = scriptEngineFromExec(exec); JSC::UString context; // The first non-empty source URL in the call stack determines the translation context. { @@ -799,7 +800,7 @@ JSC::JSValue JSC_HOST_CALL functionQsTr(JSC::ExecState *exec, JSC::JSObject*, JS while (frame) { if (frame->codeBlock() && frame->codeBlock()->source() && !frame->codeBlock()->source()->url().isEmpty()) { - context = QFileInfo(frame->codeBlock()->source()->url()).baseName(); + context = engine->translationContextFromUrl(frame->codeBlock()->source()->url()); break; } frame = frame->callerFrame()->removeHostCallFrameFlag(); @@ -921,6 +922,8 @@ QScriptEnginePrivate::QScriptEnginePrivate() activeAgent = 0; agentLineNumber = -1; processEventsInterval = -1; + cachedTranslationUrl = JSC::UString(); + cachedTranslationContext = JSC::UString(); JSC::setCurrentIdentifierTable(oldTable); } @@ -3307,6 +3310,15 @@ bool QScriptEnginePrivate::hasDemarshalFunction(int type) const return info && (info->demarshal != 0); } +JSC::UString QScriptEnginePrivate::translationContextFromUrl(const JSC::UString &url) +{ + if (url != cachedTranslationUrl) { + cachedTranslationContext = QFileInfo(url).baseName(); + cachedTranslationUrl = url; + } + return cachedTranslationContext; +} + /*! \internal */ diff --git a/src/script/api/qscriptengine_p.h b/src/script/api/qscriptengine_p.h index 5c2007f..fd47208 100644 --- a/src/script/api/qscriptengine_p.h +++ b/src/script/api/qscriptengine_p.h @@ -63,6 +63,7 @@ #include "RegExpObject.h" #include "SourceProvider.h" #include "Structure.h" +#include "UString.h" #include "JSGlobalObject.h" #include "JSValue.h" @@ -73,7 +74,6 @@ namespace JSC typedef ExecState CallFrame; class JSCell; class JSGlobalObject; - class UString; } @@ -298,6 +298,8 @@ public: static inline QScriptDeclarativeClass *declarativeClass(JSC::JSValue); static inline QScriptDeclarativeClass::Object *declarativeObject(JSC::JSValue); + JSC::UString translationContextFromUrl(const JSC::UString &); + #ifndef QT_NO_QOBJECT JSC::JSValue newQObject(QObject *object, QScriptEngine::ValueOwnership ownership = QScriptEngine::QtOwnership, @@ -367,6 +369,9 @@ public: QScriptValue abortResult; bool inEval; + JSC::UString cachedTranslationUrl; + JSC::UString cachedTranslationContext; + QSet importedExtensions; QSet extensionsBeingImported; -- cgit v0.12