Optimize QScriptEngine Repeat Action

I am trying to optimize QScriptEngine operations in one of my functions.

The function is called executeCustomJSOperation , and it executes the same JS code in multiple files. However, each file must change a global variable named $xmlData . The basicaly function loads an XML file into memory using the $xmlData variable, and then always uses the same javascript code ( jsString ) to edit this XML file using javascript. As a result, the $xmlData variable is updated with the edited XML again.

I have 2.5 acceleration, using only OpenMP parallel for in the for loop, which processes each XML file. But now I do not know how to continue to further improve this speed.

The code is as follows:

 // allows user to echo js variables to check them in terminal using cout QScriptValue echo(QScriptContext *context, QScriptEngine *engine) { std::cout << context->argument(0).toString().toUtf8().constData() << std::endl; return ""; } void executeCustomJSOperation(const QString &jsString, const QStringList &filesToProcess){ QString rexmlString, jsxmlString; QFile rexmlfile(":/resources/libs/rexml.js"); // load javascript libraries as strings to memory QFile jsxmlfile(":/resources/libs/jsxml.js"); rexmlfile.open(QFile::ReadOnly | QFile::Text); jsxmlfile.open(QFile::ReadOnly | QFile::Text); rexmlString=QTextStream(&rexmlfile).readAll(); jsxmlString=QTextStream(&jsxmlfile).readAll(); // Process all XmlFiles #pragma omp parallel for // 2.5 speedup in my pc for(int i=0; i<filesToProcess.size(); i++){ QString currXmlFileString; QScriptEngine engine; QScriptValue engineResult; // Add echo function so user can debug the code QScriptValue echoFunction = engine.newFunction(echo); engine.globalObject().setProperty("echo", echoFunction); engine.evaluate(rexmlString); // load js libraries in js engine engine.evaluate(jsxmlString); QFile currXmlFile(filesToProcess[i]); currXmlFileString=QTextStream(&currXmlFile).readAll(); currXmlFile.close(); // close reading engine.globalObject().setProperty("$xmlData",currXmlFileString); engine.evaluate("main(); function main() {"+jsString+"}"); // main function allows to use return to exit prematurely from user code engineResult=engine.globalObject().property("$xmlData"); QTextStream(&currXmlFile) << engineResult.toString(); // retreive the modified xml by javascript and save it to the file } } 

Do you find it possible to optimize this code? If in doubt, please ask.

0
source share
2 answers

Why do you create / initialize a separate QScriptEngine for each iteration? I suggest moving everything to your line

 engine.evaluate(jsxmlString); 

outside for()-loop .

True, this will complicate the WRT process. Essentially, you need to configure n worker threads and create one script engine for each thread (not for the file). To get started, a simple single-threaded version should give you a first idea of ​​what to expect from acceleration, and if it's worth it.

Of course, if your JS code is truly single-user, only QScriptProgram is your only hope for optimization. Again, you configured a limited number of workflows, each with its own QScriptProgram (and one QScriptEngine per iteration, as in your current code).

+1
source

You can build a QScriptProgram , put all the JS code in it, and evaluate it using QScriptEngine::evaluate . This can speed up execution, as JS code parsing will only be performed once. However, QScriptProgram not documented as reentrant or thread safe, so you cannot be sure that it will work correctly on multiple threads, even if each thread uses its own QScriptProgram object.

+1
source

Source: https://habr.com/ru/post/1206979/


All Articles