You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
{{ message }}
Repository navigation
slow get_fresh_msg_cnt() as reason for chatlist scrolling slowness #1879
Meanwhile I have around 320 chats and > 18000 messages in database.
When wiping the chatlist up and down it stucks significantly. This is a bad user experience meanwhile.
I suspect that this is caused by the big number of chats and/or the size of the database.
Is there any chance to get it flowing and smooth again?
what does this mean in seconds? 10 seconds? 0.5 seconds? and how often and when does it appear? does it get better over time?
however, i do not think there is a general way for improving that.
i personally have much more chats/messages and things are fine on a quite old moto g3, however, with a more recent android, but i suspect, that this is the issue.
you can try to profile, why the chatlist is so slow for you, eg. logging the time before/after the call to the core to see if it is on android- or on rust-side.
To give you an impression:
When DC is opened it is more worse. If You wipe immediately down it stucks for 2-3s then it moves for some chats, then it stucks for 2s then it moves on ... If You want to move far below it hangs all few chats ... for approx 2s.
It seems as there is much activity in background and the list is loading some content of the chats which are coming in view.
When it is open and you move slowly it stucks only very short for 0.5s or so.
I mean, there is the first sentence of last message of each chat which is shown in chatlist entries. Maybe this is the reason for the behaviour? When is this loaded?
@r10s can you give me a hint what happens between the UI and the core while moving the chatlist? Are the chatlist's entries which will come into the view loaded dynamically?
can you give me a hint what happens between the UI and the core while moving the chatlist? Are the chatlist's entries which will come into the view loaded dynamically?
yip, like that. the chatlist is a so called "virtual list", items are created as needed. what might be, however, is that the issue is unreleated to the chatlist and that there is something blocking, for whatever reason. in theory, core should return immediately, however, in practice, we had issues there from time to time. but it was getting better with the last updates :)
i would try to get the database on another second device, possibly a faster one. that might led to some ideas about the reasons. it is hard to tell from the current information :)
When moving carefully up or down then there is a short stop with every new list entry which comes into the view. Regardless if moving up or down. Maybe this is a hint?
Where is the code point when a new entry gets in view? Is then the name of the chat and its last message loaded dynamically?
Yes, it is every list entry. I strongly point to the database too, but why?
I can imagine a similar case while developing uuencode! There were some big messages into database which slowed down some functions dramatically. Maybe this is a similar case?!
Let you know ...
I think getChat() should be fast as this is retrieving only a small structure by primary key of the chat's table. However there is no index on chat_id.
But getSummary() which is done by Chatlist::get_summary() I think is the more expensive function. It loads the chat and a message from db.
Added some time measurement code into both functions and will check.
let t1 = Instant::now();
code to measure ...
info!(context, "chatlist - get_summary(): {} ms", t1.elapsed().as_millis());
But one word to the performance in general:
With the switch from c-core to core-rust 0.500 -> 1.2.1 IMHO it felt like the performance was only half than before.
Whole app feels significantly slower now. Maybe you don't see this on a modern device but at my old phone it's obviously clear.
08-28 00:48:03.002 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 2 ms
08-28 00:48:03.642 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 1 ms
08-28 00:48:03.642 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 1 ms
08-28 00:48:04.353 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 2 ms
08-28 00:48:04.353 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 0 ms
08-28 00:48:05.074 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 0 ms
08-28 00:48:05.074 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 1 ms
08-28 00:48:06.015 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 1 ms
08-28 00:48:06.015 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 1 ms
08-28 00:48:07.977 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 1 ms
08-28 00:48:07.977 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 1 ms
08-28 00:48:09.058 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 1 ms
08-28 00:48:09.058 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 2 ms
08-28 00:48:10.139 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 2 ms
08-28 00:48:10.139 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 1 ms
08-28 00:48:11.330 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 1 ms
08-28 00:48:11.330 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 1 ms
08-28 00:48:12.651 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 3 ms
08-28 00:48:12.661 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 1 ms
08-28 00:48:15.934 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 3 ms
08-28 00:48:15.934 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 0 ms
08-28 00:48:16.615 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 1 ms
08-28 00:48:16.615 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 2 ms
08-28 00:48:17.446 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 4 ms
08-28 00:48:17.446 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 1 ms
08-28 00:48:18.076 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 1 ms
08-28 00:48:18.076 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 1 ms
08-28 00:48:18.727 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 2 ms
08-28 00:48:18.727 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 1 ms
08-28 00:48:19.528 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 0 ms
08-28 00:48:19.528 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 1 ms
08-28 00:48:19.698 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 1 ms
The timings seems to be ok? As there are only small load time for every entry this should not be the cause of the slow interface.
But on the other hand the DC now feels another bit slower than without time logging!
I think loading the data from database itself seems not to be the constraint (regarding the timings measured).
But on the other hand it makes me suspicious that I can see and feel a significant slow down on chatlist behaviour when this time logging is enabled ! 2ms or even 10ms per chatlist entry cannot explain that.
You can move the list as slow as you want, but with every new upcoming list entry there is a short stop in list moving.
I (quite) don't see this with another install at same device. But this install only has a small db and only very few chats.
I think, to verify the behaviour I will create the opposite now: Empty the two functions so that they will not do the database query (and no log). => If then the movement is smoth we are back to the database ;-)
@r10s
I don't saw a difference while testing and in daily use.
Every new chat which comes in view shows the short stop when getFreshMsgCount() is active. I'm wondering too, why all the remaining code doesn't drag the speed down (as You suspected), but only this simple function! - at first view ;-)
Next step I'll verify how this query behaves in detail and then try to improve it. Maybe by an appropriate index. I'm not sure if this really mitigates query time.
My experience regarding database querys points in the direction that a missing index has terrible consequences. On the other hand an index really needs to match to the query to get successful.
We'll see.
The best success for now is, that it's found now :-)
By the way: I'm quite sure, that this issue is the root cause to have so much battery drain when working with DC!
This is the key section from open function in sql.rs
if dbversion < 63 {
info!(context, "[migration] v63");
sql.execute("UPDATE chats SET grpid='' WHERE type=100", NO_PARAMS)?;
sql.set_raw_config_int(context, "dbversion", 63)?;
}
// cs: this is the fix
info!(context, "[fix for deltachat/deltachat-android#1602] speed up get_fresh_msg_count()");
sql.execute("CREATE INDEX if not exists msgs_multi on msgs (state, hidden, chat_id)", NO_PARAMS)?;
changed the title [-]chatlist stucks significantly while wiping up and down[/-][+]slow get_fresh_msg_cnt() as reason for chatlist scrolling slowness[/+]on Sep 2, 2020
@r10s
Thanks for quick merging. I think all DC users will feel this improvement in daily use :-)
But do we have a similar case somewhere in the app? What about the chatlist? We should have a look into all often used queries related index constraints.
But do we have a similar case somewhere in the app? What about the chatlist? We should have a look into all often used queries related index constraints.
Activity
r10s commentedon Aug 26, 2020
what does this mean in seconds? 10 seconds? 0.5 seconds? and how often and when does it appear? does it get better over time?
however, i do not think there is a general way for improving that.
i personally have much more chats/messages and things are fine on a quite old moto g3, however, with a more recent android, but i suspect, that this is the issue.
you can try to profile, why the chatlist is so slow for you, eg. logging the time before/after the call to the core to see if it is on android- or on rust-side.
gerryfrancis commentedon Aug 26, 2020
@csb0730 Do the lags occur in the recent version 1.12.4, too?
csb0730 commentedon Aug 26, 2020
To give you an impression:
When DC is opened it is more worse. If You wipe immediately down it stucks for 2-3s then it moves for some chats, then it stucks for 2s then it moves on ... If You want to move far below it hangs all few chats ... for approx 2s.
It seems as there is much activity in background and the list is loading some content of the chats which are coming in view.
When it is open and you move slowly it stucks only very short for 0.5s or so.
I mean, there is the first sentence of last message of each chat which is shown in chatlist entries. Maybe this is the reason for the behaviour? When is this loaded?
@r10s can you give me a hint what happens between the UI and the core while moving the chatlist? Are the chatlist's entries which will come into the view loaded dynamically?
csb0730 commentedon Aug 26, 2020
I did not try 12.4 (for information)
r10s commentedon Aug 26, 2020
yip, like that. the chatlist is a so called "virtual list", items are created as needed. what might be, however, is that the issue is unreleated to the chatlist and that there is something blocking, for whatever reason. in theory, core should return immediately, however, in practice, we had issues there from time to time. but it was getting better with the last updates :)
i would try to get the database on another second device, possibly a faster one. that might led to some ideas about the reasons. it is hard to tell from the current information :)
csb0730 commentedon Aug 26, 2020
Ok more details:
When moving carefully up or down then there is a short stop with every new list entry which comes into the view. Regardless if moving up or down. Maybe this is a hint?
Where is the code point when a new entry gets in view? Is then the name of the chat and its last message loaded dynamically?
r10s commentedon Aug 26, 2020
if it is every list entry - can you reproduce that also with a different account? maybe sth. is wrong with your database.
should be
ConversationListAdapter::onBindViewHolder().csb0730 commentedon Aug 26, 2020
Yes, it is every list entry. I strongly point to the database too, but why?
I can imagine a similar case while developing uuencode! There were some big messages into database which slowed down some functions dramatically. Maybe this is a similar case?!
Let you know ...
csb0730 commentedon Aug 26, 2020
This is the code:
I think the expensive operation is
dcChatlist.getSummary(i, chat)?r10s commentedon Aug 27, 2020
this, and getChat() one line before accesses the database. typically this is fast, however.
csb0730 commentedon Aug 27, 2020
I think getChat() should be fast as this is retrieving only a small structure by primary key of the chat's table. However there is no index on chat_id.
But getSummary() which is done by Chatlist::get_summary() I think is the more expensive function. It loads the chat and a message from db.
Added some time measurement code into both functions and will check.
I hope this will show the problem.
csb0730 commentedon Aug 27, 2020
But one word to the performance in general:
With the switch from c-core to core-rust 0.500 -> 1.2.1 IMHO it felt like the performance was only half than before.
Whole app feels significantly slower now. Maybe you don't see this on a modern device but at my old phone it's obviously clear.
csb0730 commentedon Aug 27, 2020
This is an excerpt from performance log:
08-28 00:48:03.002 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 2 ms
08-28 00:48:03.642 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 1 ms
08-28 00:48:03.642 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 1 ms
08-28 00:48:04.353 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 2 ms
08-28 00:48:04.353 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 0 ms
08-28 00:48:05.074 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 0 ms
08-28 00:48:05.074 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 1 ms
08-28 00:48:06.015 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 1 ms
08-28 00:48:06.015 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 1 ms
08-28 00:48:07.977 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 1 ms
08-28 00:48:07.977 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 1 ms
08-28 00:48:09.058 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 1 ms
08-28 00:48:09.058 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 2 ms
08-28 00:48:10.139 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 2 ms
08-28 00:48:10.139 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 1 ms
08-28 00:48:11.330 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 1 ms
08-28 00:48:11.330 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 1 ms
08-28 00:48:12.651 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 3 ms
08-28 00:48:12.661 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 1 ms
08-28 00:48:15.934 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 3 ms
08-28 00:48:15.934 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 0 ms
08-28 00:48:16.615 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 1 ms
08-28 00:48:16.615 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 2 ms
08-28 00:48:17.446 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 4 ms
08-28 00:48:17.446 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 1 ms
08-28 00:48:18.076 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 1 ms
08-28 00:48:18.076 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 1 ms
08-28 00:48:18.727 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 2 ms
08-28 00:48:18.727 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 1 ms
08-28 00:48:19.528 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 0 ms
08-28 00:48:19.528 4167 4167 I DeltaChat: ThreadId(4) src/chatlist.rs:337: chatlist - get_summary(): 1 ms
08-28 00:48:19.698 4167 4167 I DeltaChat: ThreadId(4) src/chat.rs:496: chat - load_from_db(): 1 ms
The timings seems to be ok? As there are only small load time for every entry this should not be the cause of the slow interface.
But on the other hand the DC now feels another bit slower than without time logging!
r10s commentedon Aug 27, 2020
so, the timings seems to be okay, or?
maybe it is just a bit slow device. tiny changes make the things directly recognizable slower as there is no more "power" left ;)
csb0730 commentedon Aug 28, 2020
I think loading the data from database itself seems not to be the constraint (regarding the timings measured).
But on the other hand it makes me suspicious that I can see and feel a significant slow down on chatlist behaviour when this time logging is enabled ! 2ms or even 10ms per chatlist entry cannot explain that.
You can move the list as slow as you want, but with every new upcoming list entry there is a short stop in list moving.
I (quite) don't see this with another install at same device. But this install only has a small db and only very few chats.
I think, to verify the behaviour I will create the opposite now: Empty the two functions so that they will not do the database query (and no log). => If then the movement is smoth we are back to the database ;-)
14 remaining items
csb0730 commentedon Sep 1, 2020
@r10s
I don't saw a difference while testing and in daily use.
Every new chat which comes in view shows the short stop when getFreshMsgCount() is active. I'm wondering too, why all the remaining code doesn't drag the speed down (as You suspected), but only this simple function! - at first view ;-)
Next step I'll verify how this query behaves in detail and then try to improve it. Maybe by an appropriate index. I'm not sure if this really mitigates query time.
My experience regarding database querys points in the direction that a missing index has terrible consequences. On the other hand an index really needs to match to the query to get successful.
We'll see.
The best success for now is, that it's found now :-)
By the way: I'm quite sure, that this issue is the root cause to have so much battery drain when working with DC!
csb0730 commentedon Sep 1, 2020
Ok, results:
I'm testing with a backup db (dbversion=65, 24000 msgs, size 121MB) at my PC with sqlitebrowser.
query is always the same (from get_fresh_msg_count())
original db unchanged:
query time => ~110 ms
added a single index for hidden column:
CREATE INDEX msgs_index7 ON msgs (hidden)query time => ~170 ms !!!
drop msgs_index7 and added multi column index:
CREATE INDEX msgs_index8 ON msgs (state, hidden, chat_id)query time => 1-2 ms !!!
I think it's clear now what has to be done :-)
csb0730 commentedon Sep 1, 2020
I'm just testing the first build. => I'm badly impressed by this progress!!!
Never saw DC so responsive :-)))
The chatlist is flying and the whole app is much quicker when moving around the chats :-)
csb0730 commentedon Sep 1, 2020
What did I do at the end:
This is the key section from open function in sql.rs
See the CREATE INDEX line above. => That's all !
:-)
csb0730 commentedon Sep 1, 2020
@r10s Finally this issue belongs to the core. Maybe You can move it?
Would you add the key lines to the code? I do not have current master to do a PR.
We should not wait with this fix ;-)
r10s commentedon Sep 2, 2020
i moved the issue, thanks a lot for profiling!
i'll also try to reproduce this with my database.
[-]chatlist stucks significantly while wiping up and down[/-][+]slow get_fresh_msg_cnt() as reason for chatlist scrolling slowness[/+]r10s commentedon Sep 2, 2020
i did a fix at #1881 - thanks again, @csb0730!
just in case your are are searching for a next challenge - there is also the lag of the emoji selector - hoping this is also slow on your phone ;)
csb0730 commentedon Sep 2, 2020
@r10s
Thanks for quick merging. I think all DC users will feel this improvement in daily use :-)
But do we have a similar case somewhere in the app? What about the chatlist? We should have a look into all often used queries related index constraints.
PS: will have a look at next challenge ;-)
r10s commentedon Sep 3, 2020
the chatlist is created by a single query to get all ids and for each id in view, the chat-information is read by that id - so that should be already comparable fast to the new speed of get_fresh_msg_count().
same for the message list inside a chat.
of course, as we've just learnt, there might be unexpected bottlenecks :)
but in general, i think, it is a good first attempt to have a closer look at the things that are actually slow.
that's great!
csb0730 commentedon Sep 5, 2020
checked all remaining queries but found no further constraints.
I think that's it for the moment :-)
r10s commentedon Sep 5, 2020
thanks a lot for checking!