qt: Optimize string concatenation by default #313

pull hebasto wants to merge 2 commits into bitcoin-core:master from hebasto:210503-builder changing 9 files +31 −19
  1. hebasto commented at 10:13 am on May 3, 2021: member

    From Qt docs:

    … multiple uses of the [QString] ‘+’ operator usually means multiple memory allocations. When concatenating n substrings, where n > 2, there can be as many as n - 1 calls to the memory allocator.

    With this PR

    … the ‘+’ will automatically be performed as the QStringBuilder ‘%’ everywhere.

    The change in the src/Makefile.qt.include file does not justify submitting this PR into the main repo, IMHO.

  2. hebasto force-pushed on May 3, 2021
  3. hebasto force-pushed on May 3, 2021
  4. hebasto added the label Refactoring on May 3, 2021
  5. jarolrod commented at 4:48 am on May 5, 2021: member

    ACK b4e1480c02e845ab1df573d92acb814f62a172ea 🥃

    This simplifies and reduces the mental burden of having to think about “am I concatenating a string in the most efficient way possible?”

    I could not find any other occurrences of including QStringBuilder or occurrences of concatenating string with %.

  6. hebasto commented at 7:32 am on May 5, 2021: member

    @Talkless

    Could you look into this PR?

  7. Talkless commented at 5:44 pm on May 5, 2021: none

    It’s true that QtCreator, and Qt itself is built with that flag, but we might want to discuss if we want that “implicit magic”. There is some caveat with this kind of code:

     0#include <QString>
     1#include <QDebug>
     2
     3QString zoo() {
     4    return QString{"zoo"};
     5}
     6
     7int main()
     8{
     9    const auto foo = QString("bar") % QLatin1Char(' ') % zoo();
    10    qDebug() << foo; // crash because QStringBuilder<T...> expression template will have references to dead temporaries
    11}
    

    “maybe” it is possible to write similar code and “sometimes” not crash, IDK. Not sure if this is dangerous in any new code, i.e. if it will be missed by reviewers, if it will pass without “sometimes” crashing, etc. Although, we are talking about GUI code, so.. not sure.

    I personally still use % for “explicitness”, but as @jarolrod said, it removes burned of thinking about inefficient concatenation & stuff… And less annoying nit comments from me :D .

  8. in src/qt/optionsmodel.cpp:249 in b4e1480c02 outdated
    244@@ -244,7 +245,8 @@ static ProxySetting GetProxySetting(QSettings &settings, const QString &name)
    245 
    246 static void SetProxySetting(QSettings &settings, const QString &name, const ProxySetting &ip_port)
    247 {
    248-    settings.setValue(name, ip_port.ip + ":" + ip_port.port);
    249+    QString proxy = ip_port.ip + QLatin1Char(':') + ip_port.port;
    250+    settings.setValue(name, proxy);
    


    Talkless commented at 5:46 pm on May 5, 2021:
    This still could be one-liner with wrapping second argument in QString{} temporary, or this is assumed to have better readability? If so, const could be added.

    hebasto commented at 4:16 pm on May 15, 2021:
  9. in src/qt/peertablemodel.cpp:117 in b4e1480c02 outdated
    113@@ -114,7 +114,7 @@ QVariant PeerTableModel::data(const QModelIndex &index, int role) const
    114             return (qint64)rec->nodeStats.nodeid;
    115         case Address:
    116             // prepend to peer address down-arrow symbol for inbound connection and up-arrow for outbound connection
    117-            return QString(rec->nodeStats.fInbound ? "↓ " : "↑ ") + QString::fromStdString(rec->nodeStats.addrName);
    118+            return QString::fromStdString((rec->nodeStats.fInbound ? "↓ " : "↑ ") + rec->nodeStats.addrName);
    


    Talkless commented at 5:50 pm on May 5, 2021:
    What is motive for this change?

    hebasto commented at 4:05 pm on May 15, 2021:
    0qt/peertablemodel.cpp:117:20: error: no viable conversion from returned value of type 'QStringBuilder<typename QConcatenable<QString>::type, typename QConcatenable<QString>::type>' (aka 'QStringBuilder<QString, QString>') to function return type 'QVariant'
    1            return QString(rec->nodeStats.fInbound ? "↓ " : "↑ ") + QString::fromStdString(rec->nodeStats.addrName);
    2                   ^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
  10. in src/qt/transactiondesc.cpp:46 in b4e1480c02 outdated
    45             return tr("conflicted with a transaction with %1 confirmations").arg(-nDepth);
    46-        else if (nDepth == 0)
    47-            return tr("0/unconfirmed, %1").arg((inMempool ? tr("in memory pool") : tr("not in memory pool"))) + (status.is_abandoned ? ", "+tr("abandoned") : "");
    48-        else if (nDepth < 6)
    49+        } else if (nDepth == 0) {
    50+            QString abandoned;
    


    Talkless commented at 5:54 pm on May 5, 2021:
    Using ternary operator would allow marking it as const and avoiding re-initialization. Ir this is considered more readable?

    hebasto commented at 4:16 pm on May 15, 2021:
    Thanks! Updated.
  11. Talkless commented at 5:57 pm on May 5, 2021: none
    Concept ACK
  12. hebasto renamed this:
    qt, build: Optimize string concatenation by default
    Optimize string concatenation by default
    on May 9, 2021
  13. hebasto added the label Waiting for author on May 10, 2021
  14. hebasto commented at 3:49 pm on May 15, 2021: member

    @Talkless

    It’s true that QtCreator, and Qt itself is built with that flag, but we might want to discuss if we want that “implicit magic”.

    You are right! Discussion is a good mean to reach (rough) consensus about suggested changes.

    There is some caveat with this kind of code:

    You code snippet could be fixed by s/auto/QString/, and such caveats could be documented in https://github.com/bitcoin-core/bitcoin-devwiki/wiki/Developer-Notes-for-Qt-Code.

  15. hebasto removed the label Waiting for author on May 15, 2021
  16. qt, build: Optimize string concatenation
    The defined QT_USE_QSTRINGBUILDER macro means using the QStringBuilder
    for efficient string concatenation in all Qt code by default.
    3fd3a0fc87
  17. qt, refactor: Revert explicit including QStringBuilder a02c970eb0
  18. hebasto force-pushed on May 15, 2021
  19. hebasto commented at 4:15 pm on May 15, 2021: member

    Updated b4e1480c02e845ab1df573d92acb814f62a172ea -> a02c970eb001b456d74ddc30750fe8b55348ddac (pr313.02 -> pr313.03, diff):

  20. Talkless commented at 6:15 pm on May 19, 2021: none

    You code snippet could be fixed by s/auto/QString/

    Yes, but that’s the “danger”, as one needs to know these kinda caveats. Yes, docs could help.

  21. hebasto requested review from MarcoFalke on May 19, 2021
  22. Talkless approved
  23. Talkless commented at 6:46 pm on May 19, 2021: none
    utACK a02c970eb001b456d74ddc30750fe8b55348ddac, built successfully on Debian Sid with Qt 5.15.2, but did not check if any displayed strings are “wrong” after refactoring.
  24. jarolrod commented at 1:40 am on May 26, 2021: member

    ACK a02c970eb001b456d74ddc30750fe8b55348ddac

    Tested most of the strings on Qt 5.15.2 macOS 11.3

  25. laanwj commented at 12:41 pm on May 26, 2021: member

    Code review ACK a02c970eb001b456d74ddc30750fe8b55348ddac

    This simplifies and reduces the mental burden of having to think about “am I concatenating a string in the most efficient way possible?”

    I don’t think there is any place in bitcoin where concatenating Qt strings is actually a bottleneck, but agree with this perspective.

    It’s true that QtCreator, and Qt itself is built with that flag,

    That’s reassuring also in the sense that it probably won’t just go away.

  26. laanwj renamed this:
    Optimize string concatenation by default
    qt: Optimize string concatenation by default
    on May 26, 2021
  27. laanwj merged this on May 26, 2021
  28. laanwj closed this on May 26, 2021

  29. hebasto deleted the branch on May 26, 2021
  30. jonatack commented at 1:34 pm on May 26, 2021: contributor
    Approach ACK, this looks like a good idea indeed.
  31. sidhujag referenced this in commit dffb9c475a on May 27, 2021
  32. gwillen referenced this in commit 7db44ed938 on Jun 1, 2022
  33. bitcoin-core locked this on Aug 16, 2022

github-metadata-mirror

This is a metadata mirror of the GitHub repository bitcoin-core/gui. This site is not affiliated with GitHub. Content is generated from a GitHub metadata backup.
generated: 2024-12-28 17:20 UTC

This site is hosted by @0xB10C
More mirrored repositories can be found on mirror.b10c.me