Currently, it always passes around a chat_id and contact_id, and then loads them from the database repeatedly. Instead, they should be loaded only once, and then the struct should be passed around. As a bonus, this should also make the code a bit simpler.
This will create quite a diff though because the chat_id and contact_id are used in so many places. So,
it's nothing that can be done in an hour or so
it should be done for Chat and Contact in two PRs, not one big PR that tries to do both at once.
When I measured it quite a while ago, this were the results I saw in the flamegraph for a rather simple message (e.g. 20% of the time in receive_imf is spent on Chat::load_from_db()):
Chat::load_from_db(): 20% (The same chat is loaded 3 times from the db) - 14% easy improvement
Message::load_from_db(): 8% (The parent message is loaded 1 time from the db) - no easy improvement
Contact::load_from_db(): 10% (the "from" contact is loaded 8 times from the db) - 9% easy improvement
Probably it would be more precise to build in timing logs into the code and then measuring on a real device.
Loading from_id only once is likely the easiest. With chat_id I have not checked, but generally assignment of messages to chats is complicated by itself, so this could be a next step.
receive_imf could have some easy speed wins:
Currently, it always passes around a chat_id and contact_id, and then loads them from the database repeatedly. Instead, they should be loaded only once, and then the struct should be passed around. As a bonus, this should also make the code a bit simpler.
This will create quite a diff though because the
chat_id
andcontact_id
are used in so many places. So,Chat
andContact
in two PRs, not one big PR that tries to do both at once.When I measured it quite a while ago, this were the results I saw in the flamegraph for a rather simple message (e.g. 20% of the time in receive_imf is spent on Chat::load_from_db()):
Probably it would be more precise to build in timing logs into the code and then measuring on a real device.