Skip to content

slow get_fresh_msg_cnt() as reason for chatlist scrolling slowness #1879

Closed

Description

@csb0730
  • Delta Chat Version: 1.2.1, Android 4.1

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?

Activity

  1. r10s commented on Aug 26, 2020

    @r10s
    Contributor

    it stucks significantly

    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.

  2. gerryfrancis commented on Aug 26, 2020

    @gerryfrancis
    Contributor

    @csb0730 Do the lags occur in the recent version 1.12.4, too?

  3. csb0730 commented on Aug 26, 2020

    @csb0730
    Author

    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?

  4. csb0730 commented on Aug 26, 2020

    @csb0730
    Author

    I did not try 12.4 (for information)

  5. r10s commented on Aug 26, 2020

    @r10s
    Contributor

    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 :)

  6. csb0730 commented on Aug 26, 2020

    @csb0730
    Author

    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?

  7. r10s commented on Aug 26, 2020

    @r10s
    Contributor

    When moving carefully up or down then there is a short stop with every new list entry which comes into the view

    if it is every list entry - can you reproduce that also with a different account? maybe sth. is wrong with your database.

    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?

    should be ConversationListAdapter::onBindViewHolder().

  8. csb0730 commented on Aug 26, 2020

    @csb0730
    Author

    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 ...

  9. csb0730 commented on Aug 26, 2020

    @csb0730
    Author

    This is the code:

     public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int i) {
        ViewHolder holder = (ViewHolder)viewHolder;
        DcChat chat = dcContext.getChat(dcChatlist.getChatId(i));
        DcLot summary = dcChatlist.getSummary(i, chat);
        holder.getItem().bind(dcContext.getThreadRecord(summary, chat), dcChatlist.getMsgId(i), summary, glideRequests, locale, batchSet, batchMode);
      }
    

    I think the expensive operation is dcChatlist.getSummary(i, chat)?

  10. r10s commented on Aug 27, 2020

    @r10s
    Contributor

    this, and getChat() one line before accesses the database. typically this is fast, however.

  11. csb0730 commented on Aug 27, 2020

    @csb0730
    Author

    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());
    

    I hope this will show the problem.

  12. csb0730 commented on Aug 27, 2020

    @csb0730
    Author

    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.

  13. csb0730 commented on Aug 27, 2020

    @csb0730
    Author

    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!

  14. r10s commented on Aug 27, 2020

    @r10s
    Contributor

    so, the timings seems to be okay, or?

    But on the other hand the DC now feels another bit slower than without time logging!

    maybe it is just a bit slow device. tiny changes make the things directly recognizable slower as there is no more "power" left ;)

  15. csb0730 commented on Aug 28, 2020

    @csb0730
    Author

    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 ;-)

  16. 14 remaining items

  17. csb0730 commented on Sep 1, 2020

    @csb0730
    Author

    @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!

  18. csb0730 commented on Sep 1, 2020

    @csb0730
    Author

    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())

     SELECT COUNT(*)
                   FROM msgs
                  WHERE state=10
                    AND hidden=0
                    AND chat_id=20   <== several id's tested
    
    1. original db unchanged:
      query time => ~110 ms

    2. added a single index for hidden column:
      CREATE INDEX msgs_index7 ON msgs (hidden)
      query time => ~170 ms !!!

    3. 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 :-)

  19. csb0730 commented on Sep 1, 2020

    @csb0730
    Author

    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 :-)

  20. csb0730 commented on Sep 1, 2020

    @csb0730
    Author

    What did I do at the end:

    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)?;
    

    See the CREATE INDEX line above. => That's all !

    :-)

  21. csb0730 commented on Sep 1, 2020

    @csb0730
    Author

    @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 ;-)

  22. r10s commented on Sep 2, 2020

    @r10s
    Contributor

    i moved the issue, thanks a lot for profiling!

    i'll also try to reproduce this with my database.

  23. self-assigned this
    on Sep 2, 2020
  24. 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
  25. r10s commented on Sep 2, 2020

    @r10s
    Contributor

    We should not wait with this fix ;-)

    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 ;)

  26. csb0730 commented on Sep 2, 2020

    @csb0730
    Author

    @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 ;-)

  27. r10s commented on Sep 3, 2020

    @r10s
    Contributor

    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.

    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.

    PS: will have a look at next challenge ;-)

    that's great!

  28. csb0730 commented on Sep 5, 2020

    @csb0730
    Author

    checked all remaining queries but found no further constraints.
    I think that's it for the moment :-)

  29. r10s commented on Sep 5, 2020

    @r10s
    Contributor

    thanks a lot for checking!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type

Projects

No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions