Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

QJSValueList: Minor optimization #4767

Merged
merged 2 commits into from
May 24, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 7 additions & 6 deletions src/controllers/midi/midicontroller.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -266,12 +266,13 @@ void MidiController::processInputMapping(const MidiInputMapping& mapping,
}

QJSValue function = pEngine->wrapFunctionCode(mapping.control.item, 5);
QJSValueList args;
args << QJSValue(channel);
args << QJSValue(control);
args << QJSValue(value);
args << QJSValue(status);
args << QJSValue(mapping.control.group);
const auto args = QJSValueList{
channel,
control,
value,
status,
mapping.control.group,
};
if (!pEngine->executeFunction(function, args)) {
qCWarning(m_logBase) << "MidiController: Invalid script function"
<< mapping.control.item;
Expand Down
2 changes: 1 addition & 1 deletion src/controllers/scripting/controllerscriptenginebase.h
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class ControllerScriptEngineBase : public QObject {

virtual bool initialize();

bool executeFunction(QJSValue functionObject, const QJSValueList& arguments);
bool executeFunction(QJSValue functionObject, const QJSValueList& arguments = {});
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not sure if this has performance implications. Can you ensure this won't cause any problems because of the construction of a temporary or something? The QList default constructor does not give any guarantees.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Doesn't matter. Instead of creating default arguments in various places a single default parameter suffices.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default instance would be created anyway, either here or at the call site.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The default instance would be created anyway, either here or at the call site.

My understanding was that the instance would've been created unconditionally, even when a different Qlist was supplied. So while it doesn't matter in the cases where the default arg was created at the call site, I think it does in the cases where we create non-default QList and pass that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nope, then your understanding is wrong. The substitution happens at compile time, either or.

But it wouldn't matter anyway, because the default constructor of all container types does almost nothing. QList is no exception.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

It is only syntactic sugar and for convenience, no performance implications.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ok thanks for the clarification.


/// Shows a UI dialog notifying of a script evaluation error.
/// Precondition: QJSValue.isError() == true
Expand Down
4 changes: 2 additions & 2 deletions src/controllers/scripting/controllerscriptmoduleengine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ bool ControllerScriptModuleEngine::initialize() {
}

QJSValue initFunction = mod.property("init");
if (!executeFunction(initFunction, QJSValueList{})) {
if (!executeFunction(initFunction)) {
shutdown();
return false;
}
Expand All @@ -47,6 +47,6 @@ bool ControllerScriptModuleEngine::initialize() {
}

void ControllerScriptModuleEngine::shutdown() {
executeFunction(m_shutdownFunction, QJSValueList());
executeFunction(m_shutdownFunction);
ControllerScriptEngineBase::shutdown();
}
20 changes: 10 additions & 10 deletions src/controllers/scripting/legacy/controllerscriptenginelegacy.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -152,13 +152,12 @@ bool ControllerScriptEngineLegacy::initialize() {
wrapFunctionCode(functionName, 2)));
}

QJSValueList args;
if (m_pController) {
args << QJSValue(m_pController->getName());
} else { // m_pController is nullptr in tests.
args << QJSValue();
}
args << QJSValue(m_logger().isDebugEnabled());
// m_pController is nullptr in tests.
const auto controllerName = m_pController ? m_pController->getName() : QString{};
const auto args = QJSValueList{
controllerName,
m_logger().isDebugEnabled(),
};
if (!callFunctionOnObjects(m_scriptFunctionPrefixes, "init", args, true)) {
shutdown();
return false;
Expand All @@ -182,9 +181,10 @@ bool ControllerScriptEngineLegacy::handleIncomingData(const QByteArray& data) {
return false;
}

QJSValueList args;
args << m_pJSEngine->toScriptValue(data);
args << QJSValue(static_cast<uint>(data.size()));
const auto args = QJSValueList{
m_pJSEngine->toScriptValue(data),
static_cast<uint>(data.size()),
};

for (const QJSValue& function : std::as_const(m_incomingDataFunctions)) {
ControllerScriptEngineBase::executeFunction(function, args);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ class ControllerScriptEngineLegacy : public ControllerScriptEngineBase {
QJSValue wrapArrayBufferCallback(const QJSValue& callback);
bool callFunctionOnObjects(const QList<QString>& scriptFunctionPrefixes,
const QString&,
const QJSValueList& args = QJSValueList(),
const QJSValueList& args = {},
bool bFatalError = false);

QJSValue m_makeArrayBufferWrapperFunction;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -522,7 +522,7 @@ void ControllerScriptInterfaceLegacy::timerEvent(QTimerEvent* event) {
stopTimer(timerId);
}

m_pScriptEngineLegacy->executeFunction(timerTarget.callback, QJSValueList());
m_pScriptEngineLegacy->executeFunction(timerTarget.callback);
}

void ControllerScriptInterfaceLegacy::softTakeover(
Expand Down
9 changes: 5 additions & 4 deletions src/controllers/scripting/legacy/scriptconnection.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,10 +3,11 @@
#include "controllers/scripting/legacy/controllerscriptenginelegacy.h"

void ScriptConnection::executeCallback(double value) const {
QJSValueList args;
args << QJSValue(value);
args << QJSValue(key.group);
args << QJSValue(key.item);
const auto args = QJSValueList{
value,
key.group,
key.item,
};
QJSValue func = callback; // copy function because QJSValue::call is not const
QJSValue result = func.call(args);
if (result.isError()) {
Expand Down