Skip to content

Commit

Permalink
LMail: free all allocations on Close()
Browse files Browse the repository at this point in the history
Don't wait for the Lua GC to eventually collect the IncomingMail
instance; the GC doesn't know about these allocations and will not
consider them in the decision whether to collect.

This change can save a lot of memory on a busy relay.
  • Loading branch information
MaxKellermann committed Mar 11, 2024
1 parent d49c4fc commit 5936a08
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 0 deletions.
1 change: 1 addition & 0 deletions debian/changelog
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
cm4all-qrelay (0.30) unstable; urgency=low

* free memory immediately after email has been processed
* catch logging errors

--
Expand Down
1 change: 1 addition & 0 deletions src/LMail.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,7 @@ class IncomingMail : public MutableMail {

int Close(lua_State *) {
auto_close = nullptr;
Free();
return 0;
}

Expand Down
11 changes: 11 additions & 0 deletions src/MutableMail.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,17 @@ struct MutableMail : QmqpMail {
explicit MutableMail(AllocatedArray<std::byte> &&_buffer)
:buffer(_buffer) {}

/**
* Clear this object and free all C++ heap allocations.
*/
void Free() noexcept {
*static_cast<QmqpMail *>(this) = {};
buffer = nullptr;
sender_buffer.clear();
headers.clear();
account.clear();
}

bool Parse() {
return QmqpMail::Parse({(const std::byte *)buffer.data(), buffer.size()});
}
Expand Down

0 comments on commit 5936a08

Please sign in to comment.