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

improved score openning speed by optimisation the searchTemplateForInstrNameList function #25971

Merged
merged 1 commit into from
Jan 3, 2025
Merged
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
27 changes: 18 additions & 9 deletions src/engraving/dom/instrtemplate.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -853,19 +853,28 @@ const InstrumentTemplate* searchTemplateForInstrNameList(const std::list<String>
{
const InstrumentTemplate* bestMatch = nullptr; // default if no matches
int bestMatchStrength = 0; // higher for better matches
for (const InstrumentGroup* g : instrumentGroups) {
for (const InstrumentTemplate* it : g->instrumentTemplates) {
for (const String& name : nameList) {
if (name.isEmpty() || it->useDrumset != useDrumset) {

for (String name : nameList) {
if (name.isEmpty()) {
continue;
}

if (!caseSensitive) {
name = name.toLower();
}
Comment on lines +857 to +864
Copy link
Contributor

@shoogle shoogle Jan 3, 2025

Choose a reason for hiding this comment

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

This might help too by avoiding copying strings unnecessarily:

for (const String& name : nameList) {
    if (name.isEmpty()) {
        continue;
    }

    const String& normalizedName = caseSensitive ? name : name.toLower();
    StaffName instrName(normalizedName);

StaffName instrName(name);

for (const InstrumentGroup* g : instrumentGroups) {
for (const InstrumentTemplate* it : g->instrumentTemplates) {
if (it->useDrumset != useDrumset) {
continue;
}

String trackName = it->trackName;
Copy link
Contributor

Choose a reason for hiding this comment

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

Avoiding another string copy:

const String& trackName = caseSensitive ? it->trackName : it->trackName.toLower();

Copy link
Contributor Author

@handrok handrok Jan 3, 2025

Choose a reason for hiding this comment

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

String trackName = it->trackName;
muse::String is designed like QString from QT. It has move semantic. Copy of muse::String object doesn't lead to allocate memory. Only just pointer to shared memory is copied. Allocating memory is happening only if muse::String data is changed.

const String& trackName = it->trackName.toLower(); that code doesn't improve performance. memory is allocated inside of toLower() method. And toLower() creates local variable and return it. So memory was allocated anyway and one copy of muse::String object is happened. But as I said copying of muse::String is not a big problem. It has move semantic and copying muse::String is not much bigger then using of reference. sizeof(muse::String) is 16 bytes which is double of sizeof of reference. I think it is not a big deal. Instead of copy 8 bytes I copy 16 bytes here. And increase pointer count inside muse::String

Copy link
Contributor Author

Choose a reason for hiding this comment

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

But I cant change it if you insist.

Copy link
Contributor

Choose a reason for hiding this comment

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

No, I'm happy with it. Thanks for the explanation!

StaffNameList longNames = it->longNames;
StaffNameList shortNames = it->shortNames;
String instrName = name;

if (!caseSensitive) {
instrName = instrName.toLower();
trackName = trackName.toLower();
for (StaffName& n : longNames) {
n.setName(n.name().toLower());
Expand All @@ -876,9 +885,9 @@ const InstrumentTemplate* searchTemplateForInstrNameList(const std::list<String>
}

int matchStrength = 0
+ (4 * (trackName == instrName ? 1 : 0)) // most weight to track name since there are fewer duplicates
+ (2 * (muse::contains(longNames, StaffName(instrName)) ? 1 : 0))
+ (1 * (muse::contains(shortNames, StaffName(instrName)) ? 1 : 0)); // least weight to short name
+ (4 * (trackName == name ? 1 : 0)) // most weight to track name since there are fewer duplicates
+ (2 * (muse::contains(longNames, instrName) ? 1 : 0))
+ (1 * (muse::contains(shortNames, instrName) ? 1 : 0)); // least weight to short name
const int perfectMatchStrength = 7;
assert(matchStrength <= perfectMatchStrength);
if (matchStrength > bestMatchStrength) {
Expand Down