Skip to content

performance degradation #2353

Closed
Closed

Description

@r10s

as reported recently in testing chat and what i could also reproduce on my not-so-fast-android phone (motog), there seemed to be a speed degration.

i first noticed that in the global search in my 3gb-database, that does all three expensive searches in a row; a simple search for the work "hello" on motog:

using core51 (using rusqlite) on android:

⏰ getContacts: 16ms
⏰ getChatlist: 466ms
⏰ searchMsgs: 959ms

and on core52 (using sqlx) on android:

⏰ getContacts: 34ms
⏰ getChatlist: 521ms
⏰ searchMsgs: 3007ms

on a desktop computer with far more memory, processor etc. this degration is not that huge, but still measureable: a simple search for the word "hello" using the same 3gb-database on repl (only doing searchMsgs):

using core51 (using rusqlite) on desktop:

⏰ searchMsgs: 170ms

using core52 (using sqlx) on desktop:

⏰ searchMsgs: 219ms

i run the test several times, this difference is very reproducible. as this difference is on a very fast machine, it is easily imaginable that that escalates when having far less resources as on low-end phones.

i also checked if the degration is really related to the sqlx-commit, any yes:
commit directly before getting sqlx in: 4dedc2d: 170ms
next commit with sqlx: 6bb5721 219ms

maybe a cache issue? as the degradation is not only on searchMsgs, i doubt there are some sql-commands not transferred correctly, but, of course, that is also worth checking.

Activity

  1. added
    bug
    on Apr 16, 2021
  2. link2xt commented on Apr 16, 2021

    @link2xt
    Collaborator

    Could be the problem is somewhere within this complexity: transact-rs/sqlx@5295ff1 Rather arbitrary wakeup timers etc. I don't really understand.

    Makes sense to add a benchmark adding 1000 messages to the database and benchmarking search for all of them in random order.

  3. r10s commented on Apr 16, 2021

    @r10s
    ContributorAuthor

    additional things checked:

    • retested: now, core52 is even slower on my system: currently core51 needs 166ms (roughly unchanges) and core52 needs 350ms (slower) - maybe i have less memory somehow - and this affects core52 more?

    • initially i retested because of using only one connection as suggested by @link2xt on irc: using only one connection takes 206ms - so that is faster than 10 connections, but still slower than core51

    • PRAGMA cache_size returns -2000 for both (that means the cache size is limited to 2048000 bytes of memory)

    • adding LIMIT 1 to search_msgs: 162ms (core51, roughly unchanged) vs 227ms (core52, i retried, without limit, that are still 350ms)

    • update to new sqlx as of 69dae4c does not make a difference

    • running the search_msgs() query in sqlite3 commandline tool results in
      Run Time: real 0.126 user 0.104146 sys 0.020781
      the user part seem to be the printing, so we have 126ms for querying core51 adds 40ms for $things, core52 adds 224 (referring to the last measurements) - on 10x times slower machines, this becomes a real issue

  4. link2xt commented on Apr 16, 2021

    @link2xt
    Collaborator

    I patched my repl as follows:

    diff --cc examples/repl/cmdline.rs
    index 61e289b4,82c4e657..00000000
    --- a/examples/repl/cmdline.rs
    +++ b/examples/repl/cmdline.rs
    @@@ -898,10 -899,15 +898,15 @@@ pub async fn cmdline(context: Context, 
                      None
                  };
      
    +             let time_start = std::time::SystemTime::now();
     -            let msglist = context.search_msgs(chat, arg1).await;
     +            let msglist = context.search_msgs(chat, arg1).await?;
    +             let time_needed = std::time::SystemTime::now()
    +                 .duration_since(time_start)
    +                 .unwrap_or_default();
      
    -             log_msglist(&context, &msglist).await?;
    +             //log_msglist(&context, &msglist).await?;
                  println!("{} messages.", msglist.len());
    +             println!("{:?} to create this list", time_needed);
              }
              "draft" => {
                  ensure!(sel_chat.is_some(), "No chat selected.");

    And consistently get alternating timings:

    > listmsgs hello
    > 131 messages.
    182.321763ms to create this list
    > listmsgs hello
    > 131 messages.
    81.777799ms to create this list
    > listmsgs hello
    > 131 messages.
    184.175344ms to create this list
    > listmsgs hello
    > 131 messages.
    81.282844ms to create this list
    > listmsgs hello
    > 131 messages.
    181.654437ms to create this list
    > listmsgs hello
    > 131 messages.
    73.209812ms to create this list
    > listmsgs hello
    > 131 messages.
    173.671883ms to create this list
    > listmsgs hello
    > 131 messages.
    72.353225ms to create this list
    > listmsgs hello
    > 131 messages.
    183.194884ms to create this list
    > listmsgs hello
    > 131 messages.
    74.495374ms to create this list
    > listmsgs hello
    > 131 messages.
    181.752891ms to create this list
    > listmsgs hello
    > 131 messages.
    105.806821ms to create this list
    > listmsgs hello
    > 131 messages.
    175.98922ms to create this list
    > listmsgs hello
    > 131 messages.
    107.024515ms to create this list
    > listmsgs hello
    > 131 messages.
    88.309487ms to create this list
    > listmsgs hello
    > 131 messages.
    162.344638ms to create this list
    > listmsgs hello
    > 131 messages.
    79.041662ms to create this list
    > listmsgs hello
    > 131 messages.
    174.579933ms to create this list
    > listmsgs hello
    > 131 messages.
    68.742556ms to create this list
    > listmsgs hello
    > 131 messages.
    178.641076ms to create this list
    > listmsgs hello
    > 131 messages.
    91.934622ms to create this list
    > listmsgs hello
    > 131 messages.
    181.358206ms to create this list
    > listmsgs hello
    > 131 messages.
    81.293996ms to create this list
    > listmsgs hello
    > 131 messages.
    179.22445ms to create this list
    > listmsgs hello
    > 131 messages.
    106.363615ms to create this list
    > listmsgs hello
    > 131 messages.
    197.946377ms to create this list
    > listmsgs hello
    > 131 messages.
    91.338257ms to create this list
    > listmsgs hello
    > 131 messages.
    178.339919ms to create this list
    > listmsgs hello
    > 131 messages.
    81.41003ms to create this list
    
  5. link2xt commented on Apr 17, 2021

    @link2xt
    Collaborator

    Enabling shared cache reduces the latency to ~80-130ms for me, enabling it requires this addition to API: transact-rs/sqlx#1177

    With shared cache enabled on both pools:

    > listmsgs hello
    > 131 messages.
    87.368351ms to create this list
    > listmsgs hello
    > 131 messages.
    83.15953ms to create this list
    > listmsgs hello
    > 131 messages.
    81.081135ms to create this list
    > listmsgs hello
    > 131 messages.
    77.157051ms to create this list
    > listmsgs hello
    > 131 messages.
    84.980555ms to create this list
    > listmsgs hello
    > 131 messages.
    105.771714ms to create this list
    > listmsgs hello
    > 131 messages.
    105.86991ms to create this list
    > listmsgs hello
    > 131 messages.
    79.603644ms to create this list
    > listmsgs hello
    > 131 messages.
    86.864016ms to create this list
    > listmsgs hello
    > 131 messages.
    100.318086ms to create this list
    > listmsgs hello
    > 131 messages.
    78.140345ms to create this list
    > listmsgs hello
    > 131 messages.
    82.260042ms to create this list
    > listmsgs hello
    > 131 messages.
    76.612552ms to create this list
    > listmsgs hello
    > 131 messages.
    77.921539ms to create this list
    > listmsgs hello
    > 131 messages.
    87.595609ms to create this list
    > listmsgs hello
    > 131 messages.
    81.382482ms to create this list
    

    This solves the issue for me.

    About 10ms is lost to serialization of sqlite3_step, see issue transact-rs/sqlx#1175. It's not critical.

  6. link2xt commented on Apr 17, 2021

    @link2xt
    Collaborator

    We didn't use shared cache with rusqlite if I didn't miss something, but probably it was not needed due to different architecture (no dedicated worker thread to execute sqlite3_step). I am not going to try to enable it on rusqlite and compare because we already have sqlite3 utility with .timer command as a baseline.

  7. link2xt commented on Apr 17, 2021

    @link2xt
    Collaborator

    Shared cache breaks tests :(

    #2355 makes the errors a bit more meaningful, the problem is usually error returned from database: database table is locked: msgs.

    PRAGMA read_uncommitted=1; fixed the things, #2356 fixes this bug (but approve #2355 first)

  8. r10s commented on Apr 17, 2021

    @r10s
    ContributorAuthor

    just a quick note:
    178ms rusqlite, 220ms sqlx-with-#2356, 380ms sqlx-without-#2356

  9. gerryfrancis commented on Apr 17, 2021

    @gerryfrancis
    Contributor

    @r10s Did you consider to apply #2355 before #2356, as @link2xt recommended in his PR?

  10. link2xt commented on Apr 17, 2021

    @link2xt
    Collaborator

    @gerryfrancis #2355 is included into #2356, it's basically code cleanup.

  11. link2xt commented on Apr 17, 2021

    @link2xt
    Collaborator

    I think 220ms vs 178ms is not bad, the question is how much the fix reduces the original problem of searchMsgs taking 3 seconds on old android. If it reduces to ~1s..1.5s it should be ok, the rest comes from per-row overhead (transact-rs/sqlx#1175) and should be fixed by adding LIMIT to avoid returning too many rows anyway.

  12. r10s commented on Apr 17, 2021

    @r10s
    ContributorAuthor

    this will speed up basically all requests a bit, right?

    i will check out android later, if we really get down to ~1s..1.5s, this is fine, and yes a LIMIT makes sense. additionally, we can also improve things by a better debouncing as on ios, doing searchMsgs only when there is no newer search etc.

  13. link2xt commented on Apr 17, 2021

    @link2xt
    Collaborator

    this will speed up basically all requests a bit, right?

    Shared cache should speed up everything, right. As far as I understand this is needed to avoid overheads resulting from sqlx not pinning SQLite structures to threads.

  14. r10s commented on Apr 17, 2021

    @r10s
    ContributorAuthor

    so, i tried #2356 on the same android phone with the same db as in the initial post - the result is pretty much the same as for core51 \o/ great!

    ⏰ getContacts: 19ms
    ⏰ getChatlist: 530ms
    ⏰ searchMsgs: 1054ms

    i retested some times, skipped the first one that is a bit slower, but they're almost always the same.
    i rechecked with rusqlite also just now, got 13ms/517ms/1004ms there in average, so, no difference to worry about (esp. as future improvements of sqlx might be even go beyond that and overall design is better iiuc)

    but i have to admit, i did not understand all the changes yet :)

  15. r10s commented on Apr 18, 2021

    @r10s
    ContributorAuthor

    for reference: just tested ios overnight - there is really some boost with improved sqlx, global search is roughly double as fast as before.
    the search for "hallo" in the 3gb db i am using for testing takes 2ms/86ms/149ms (search for contacts/chatlist/messages on iphone7)

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

Metadata

Metadata

Assignees

No one assigned

    Labels

    bug

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions