Loader: Avoid Q_FOREACH
ClosedPublic

Authored by loh.tar on Nov 19 2018, 9:34 PM.

Diff Detail

Repository
R246 Sonnet
Lint
Automatic diff as part of commit; lint not applicable.
Unit
Automatic diff as part of commit; unit tests not applicable.
loh.tar created this revision.Nov 19 2018, 9:34 PM
Restricted Application added a project: Frameworks. · View Herald TranscriptNov 19 2018, 9:34 PM
loh.tar requested review of this revision.Nov 19 2018, 9:34 PM
davidedmundson accepted this revision.Nov 19 2018, 9:49 PM
This revision is now accepted and ready to land.Nov 19 2018, 9:49 PM

@davidedmundson Wow, that was fast :-)

I have now done a search for more Q_FOREACH/foreach. Please let me know if you want each as own diff or all in once.

data/parsetrigrams.cpp
src/core/guesslanguage.cpp
src/plugins/hspell/hspelldict.cpp
src/plugins/hunspell/hunspellclient.cpp
src/plugins/voikko/voikkodict.cpp
smartins added inline comments.
src/core/loader.cpp
273

will languages() detach ?

.I have now done a search for more Q_FOREACH/foreach. Please let me know if you want each as own diff or all in once

Can be all at once, but still go via review. There are some foreach / for differences and pitfalls to be wary of.

loh.tar added inline comments.Nov 20 2018, 3:49 PM
src/core/loader.cpp
273

Probably, yes. But Qt docu always says, "thanks to implicit sharing copying a container is very fast"

Is copy previous into a const var a benefit (as I have seen recently)? Don't think so. Or would iterate by index avoid that? (It's a QStringList) I don't know. Guess the problem remains. You need to keep a copy.

I think I read something like, "Don't worry when using a function return value", but can't find it anymore. So, perhaps it was my conclusion from here: http://doc.qt.io/qt-5/qtglobal.html#qAsConst

Reads similar to me: https://www.kdab.com/goodbye-q_foreach/

Let me know what you like to see, will do it.

davidedmundson added inline comments.Nov 20 2018, 3:56 PM
src/core/loader.cpp
273

Probably, yes. But Qt docu always says, "thanks to implicit sharing copying a container is very fast"

That's mixing up docs.

foreach() always does a const copy of the list. This is a cheap implicitly shared copy.
when we loop we're never detaching this list because it's const.

for() does not
So when we loop if we call begin() we might get the non-const version. If this list is used elsewhere that means we detach and that causes a deep-copy. This is expensive.

In this specific case language() returns a new QStringList (.keys() generates a new list) so I /think/ it is fine,

but it's definitely ambiguous. A qAsConst would make sense here.

loh.tar added inline comments.Nov 20 2018, 7:08 PM
src/core/loader.cpp
273

A qAsConst would make sense here.

In which way? This is not working:

for (const QString &langCode : qAsConst(languages()))

So you like to see this?

const QStringList constLanguages = languages();
for (const QString &langCode : constLanguages) {

@davidedmundson
May I ask how does this here will progress? You seemed to request a change which was not clear to me, but it's anyway "green flagged".

cfeck added a subscriber: cfeck.Nov 30 2018, 4:53 PM

Is your full name really "loh tar", spelled with lower case letters? It looked strange on the list of names of all committers.

Well, neither of that. My "online me" is loh.tar but that was rejected by Phabricator, so the dot had to go. Lastly used ngraham uppercase letters, what looks even more strange to me :-)

Should you intend to commit this, thanks in advance! May you look at D17055 too?

cfeck added a comment.Nov 30 2018, 8:33 PM

I do not know if there is a written rule that we require commits with real names (for legal reasons), but I cannot remember we didn't ask contributors for their real name for contributions.

..but I cannot remember we didn't ask contributors for their real name for contributions.

Um, what? I was asked. I hope it's ok as it is.

@cfeck wrote:

It looked strange on the list of names of all committers.

A look at your page indicates that you are doing some public relations? Should you copy&paste some data from an auto generated list, feel free to remove me.

We started to accept his patches for other frameworks, I think there is no harm in doing so, here, too. Real name or not.

This revision was automatically updated to reflect the committed changes.