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

Optimize memory allocation on Telegram API calls #38

Open
3 tasks
Kaffeine opened this issue Sep 2, 2017 · 0 comments
Open
3 tasks

Optimize memory allocation on Telegram API calls #38

Kaffeine opened this issue Sep 2, 2017 · 0 comments
Milestone

Comments

@Kaffeine
Copy link
Owner

Kaffeine commented Sep 2, 2017

Generated Telegram API methods implementation can reserve the exact amount of memory in the output byte array.

The generated code should be readable, so:

  • The size calculation code line length should be limited to 120 symbols
  • It's OK for Strings and ByteArrays to treat length bytes as 4 (better than use ternary operators)
  • The addendums (except Strings and ByteArrays) should have inline comments (TLValue constant name for functions, argument/member name in all other cases; size bytes should be decorated as "4 /* size bytes */")

Commits:

  • Introduce sizeInBytes() const methods for all TLTypes
  • Introduce output.reserve() generation for all methods

Bonus:

  • Make sizeInBytes() constexpr if possible:
    • The type has only one tlType value
    • The type has no complex or Strings/ByteArrays members
    • E.g.: TLContact, TLContactSuggested, TLContactLink, TLContactBlocked, etc...

Expected diff would look like:

quint64 CTelegramConnection::accountCheckUsername(const QString &username)
{
    QByteArray output;
+   output.reserve(4 /* AccountCheckUsername */
+                  + username.size() + 4 /* size bytes */);
    CTelegramStream outputStream(&output, /* write */ true);
    outputStream << TLValue::AccountCheckUsername;
    outputStream << username;
    return sendEncryptedPackage(output);
}
...

quint64 CTelegramConnection::accountReportPeer(const TLInputPeer &peer,
                                               const TLReportReason &reason)
{
    QByteArray output;
+   output.reserve(4 /* AccountReportPeer */ + peer.sizeInBytes() + reason.sizeInBytes());
    CTelegramStream outputStream(&output, /* write */ true);
    outputStream << TLValue::AccountReportPeer;
    outputStream << peer;
    outputStream << reason;
    return sendEncryptedPackage(output);
}

quint64 CTelegramConnection::accountResetAuthorization(quint64 hash)
{
    QByteArray output;
+   output.reserve(4 /* AccountResetAuthorization */ + 8 /* hash */);
    CTelegramStream outputStream(&output, /* write */ true);
    outputStream << TLValue::AccountResetAuthorization;
    outputStream << hash;
    return sendEncryptedPackage(output);
}
quint64 CTelegramConnection::authBindTempAuthKey(quint64 permAuthKeyId, quint64 nonce,
                                                 quint32 expiresAt,
                                                 const QByteArray &encryptedMessage)
{
    QByteArray output;
+   output.reserve(4 /* AuthBindTempAuthKey */ + 8 /* permAuthKeyId */ + 8 /* nonce */
+                  + 4 /* expiresAt */
+                  + encryptedMessage.size() + 4 /* size bytes */);
    CTelegramStream outputStream(&output, /* write */ true);
    outputStream << TLValue::AuthBindTempAuthKey;
    outputStream << permAuthKeyId;
    outputStream << nonce;
    outputStream << expiresAt;
    outputStream << encryptedMessage;
    return sendEncryptedPackage(output);
}
@Kaffeine Kaffeine added this to the 0.3.0 milestone Sep 2, 2017
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant