trilium icon indicating copy to clipboard operation
trilium copied to clipboard

(Bug report) Slow autocomplete

Open agentydragon opened this issue 4 years ago • 15 comments

Preflight Checklist

  • [X] I have searched the issue tracker for a bug report that matches the one I want to file, without success.

Trilium Version

0.48.6

What operating system are you using?

Ubuntu

What is your setup?

Local + server sync

Operating System Version

Pop!_OS 20.04

Expected Behavior

The dialog for adding a link should be responsive to keystrokes added to the page name (/ search string) as they are being added.

Actual Behavior

It looks like the UI of the dialog is blocked until DB search results are returned. If you type in, say, a single letter, and get many results, there's a noticeable delay between the keystroke and the dialog processing further input. This adds a bit of friction to linking notes.

Video: https://youtu.be/UTEzb7MYeoI

Additional Information

No response

agentydragon avatar Nov 24 '21 12:11 agentydragon

Hi, so on a certain level this is a fundamental problem - nodejs on the backend is single-threaded and when it's doing something CPU intensive (such as chewing through in-memory index) then it can't process any other CPU intensive request.

However, in your case this seems to be way slower than it should be. E.g. on my laptop and database with tens of thousands of notes I get these times:

19:51:05.047 Slow 200 GET /api/autocomplete?query=a&activeNoteId=g5Dv0LHYwV9t with 111318 bytes took 262ms
19:51:05.368 Slow 200 GET /api/autocomplete?query=aa&activeNoteId=g5Dv0LHYwV9t with 11106 bytes took 67ms
19:51:05.408 Slow 200 GET /api/autocomplete?query=aaa&activeNoteId=g5Dv0LHYwV9t with 2 bytes took 40ms
19:51:05.439 Slow 200 GET /api/autocomplete?query=aaaa&activeNoteId=g5Dv0LHYwV9t with 2 bytes took 29ms
19:51:05.658 Slow 200 GET /api/autocomplete?query=aaaaa&activeNoteId=g5Dv0LHYwV9t with 2 bytes took 24ms
19:51:05.865 Slow 200 GET /api/autocomplete?query=aaaaaa&activeNoteId=g5Dv0LHYwV9t with 2 bytes took 25ms
19:51:06.059 Slow 200 GET /api/autocomplete?query=aaaaaaa&activeNoteId=g5Dv0LHYwV9t with 2 bytes took 29ms

Could you look up these lines from your log for comparison? Also, your rough config (on the chance you're running on some Atom-grade CPU) and amount of notes you have?

zadam avatar Nov 24 '21 18:11 zadam

Here's a few trials:

Slow 200 GET /api/autocomplete?query=a&activeNoteId=46IQA250whjL with 179122 bytes took 24ms
Slow 200 GET /api/autocomplete?query=ab&activeNoteId=46IQA250whjL with 136893 bytes took 16ms
200 GET /api/autocomplete?query=abc&activeNoteId=46IQA250whjL with 4328 bytes took 4ms
200 GET /api/autocomplete?query=abcd&activeNoteId=46IQA250whjL with 331 bytes took 3ms
200 GET /api/autocomplete?query=abcde&activeNoteId=46IQA250whjL with 331 bytes took 1ms
200 GET /api/autocomplete?query=abcdef&activeNoteId=46IQA250whjL with 331 bytes took 5ms
200 GET /api/autocomplete?query=abcdefg&activeNoteId=46IQA250whjL with 331 bytes took 3ms
Slow 200 GET /api/autocomplete?query=f&activeNoteId=46IQA250whjL with 119887 bytes took 51ms
200 GET /api/autocomplete?query=fo&activeNoteId=46IQA250whjL with 103505 bytes took 9ms
200 GET /api/autocomplete?query=foo&activeNoteId=46IQA250whjL with 709 bytes took 2ms
200 GET /api/autocomplete?query=foob&activeNoteId=46IQA250whjL with 2 bytes took 2ms
200 GET /api/autocomplete?query=fooba&activeNoteId=46IQA250whjL with 2 bytes took 4ms
200 GET /api/autocomplete?query=foobar&activeNoteId=46IQA250whjL with 2 bytes took 6ms
Slow 200 GET /api/autocomplete?query=a&activeNoteId=46IQA250whjL with 179122 bytes took 42ms
200 GET /api/autocomplete?query=aa&activeNoteId=46IQA250whjL with 38447 bytes took 6ms
200 GET /api/autocomplete?query=aaa&activeNoteId=46IQA250whjL with 491 bytes took 2ms
200 GET /api/autocomplete?query=aaaa&activeNoteId=46IQA250whjL with 2 bytes took 5ms
200 GET /api/autocomplete?query=aaaaa&activeNoteId=46IQA250whjL with 2 bytes took 5ms
200 GET /api/autocomplete?query=aaaaaa&activeNoteId=46IQA250whjL with 2 bytes took 2ms
Slow 200 GET /api/autocomplete?query=a&activeNoteId=46IQA250whjL with 179122 bytes took 48ms
200 GET /api/autocomplete?query=aa&activeNoteId=46IQA250whjL with 38447 bytes took 2ms
200 GET /api/autocomplete?query=aaa&activeNoteId=46IQA250whjL with 491 bytes took 2ms
200 GET /api/autocomplete?query=aaaa&activeNoteId=46IQA250whjL with 2 bytes took 2ms
200 GET /api/autocomplete?query=aaaaa&activeNoteId=46IQA250whjL with 2 bytes took 2ms
200 GET /api/autocomplete?query=aaaaaa&activeNoteId=46IQA250whjL with 2 bytes took 4ms
200 GET /api/autocomplete?query=aaaaaaa&activeNoteId=46IQA250whjL with 2 bytes took 5ms
200 GET /api/autocomplete?query=aaaaaaaa&activeNoteId=46IQA250whjL with 2 bytes took 5ms

I have a Lenovo X1 Extreme 3rd generation, it's not a potato (i7-10850H @ 2.7 GHz, 32 GB RAM).

When I just recorded those logs, those timings look right, and neither did the dialog seem too sluggish. Maybe it might have something to do with caches not being warm yet? I don't know about any significant difference between a few hours back when I recorded the video, and now.

SELECT COUNT() FROM notes; returns 1702.

agentydragon avatar Nov 24 '21 19:11 agentydragon

Thanks for the details.

Hmm, I'm not sure. Cache gets loaded right after the startup, so it shouldn't be the cause. (and with your note count the whole cache load should be under 50ms anyway)

Sometimes I notice random slowdowns which I can't really replicate/explain. Not sure if it's some bug or perhaps some V8 related things like GC sweeps.

zadam avatar Nov 24 '21 20:11 zadam

Saw a noticeably slow autocomplete now:

Slow 200 GET /api/autocomplete?query=Open&activeNoteId=Y9ZdsD4Yrrjz with 4921 bytes took 27ms
Slow autocomplete took 123ms
Slow 200 GET /api/autocomplete?query=d&activeNoteId=Y9ZdsD4Yrrjz with 134577 bytes took 130ms
Slow 200 GET /api/autocomplete?query=do&activeNoteId=Y9ZdsD4Yrrjz with 76473 bytes took 40ms
Slow 200 GET /api/autocomplete?query=don&activeNoteId=Y9ZdsD4Yrrjz with 8562 bytes took 14ms
Slow 200 GET /api/autocomplete?query=done&activeNoteId=Y9ZdsD4Yrrjz with 1156 bytes took 50ms
200 GET /api/autocomplete?query=Done&activeNoteId=Y9ZdsD4Yrrjz with 1156 bytes took 8ms

In case it matters, Done and Open are "issue status" pages. Most issues in my "Trilium issue tracker" link to one of those.

agentydragon avatar Nov 29 '21 20:11 agentydragon

Just digging a little bit into Trilium's source, it looks to me like the slowness here might not be in the database - my logs with with those slow autocompletes do not contain the Slow query took ... line logged here: https://github.com/zadam/trilium/blob/b660238a4098b45c2c0f82b17a4dacc04ff14910/src/services/sql.js#L229

So maybe just a bit of JS optimization might help. Let me know if you have ideas, especially for how this could be profiled.

agentydragon avatar Feb 18 '22 10:02 agentydragon

The autocomplete is actually fully in memory and does not touch the database. It's probably going to be difficult to profile since it does not happen everytime.

zadam avatar Feb 18 '22 21:02 zadam

I can reproduce it quite consistently on my machine.

On Fri, 18 Feb 2022, 22:38 zadam, @.***> wrote:

The autocomplete is actually fully in memory. It's probably going to be difficult to profile since it does not happen everytime.

— Reply to this email directly, view it on GitHub https://github.com/zadam/trilium/issues/2375#issuecomment-1045217648, or unsubscribe https://github.com/notifications/unsubscribe-auth/AAFORDDIMVFPS5VGMZUO55LU32367ANCNFSM5IWABB2A . Triage notifications on the go with GitHub Mobile for iOS https://apps.apple.com/app/apple-store/id1477376905?ct=notification-email&mt=8&pt=524675 or Android https://play.google.com/store/apps/details?id=com.github.android&referrer=utm_campaign%3Dnotification-email%26utm_medium%3Demail%26utm_source%3Dgithub.

You are receiving this because you authored the thread.Message ID: @.***>

agentydragon avatar Feb 18 '22 22:02 agentydragon

I'd be willing to pay $300 for a fix - i.e., stopping Trilium from visibly freezing and not reacting to input for a very visible fraction of a second when running autocomplete in attributes/links on my database. Contact me at [email protected] if either @zadam or any other Trilium dev would be interested.

agentydragon avatar Apr 28 '22 08:04 agentydragon

Have you tried to use trilium in browser? Keep your client running and open http://127.0.0.1:37840/login with Chrome, see if it's still slow.

Nriver avatar Apr 29 '22 01:04 Nriver

@Nriver I'll give it a try. use within browser isn't ideal (e.g. browser eats Ctrl+W), but if it works, might be a workable workaround.

agentydragon avatar May 02 '22 04:05 agentydragon

Thanks @Nriver! So far, Trilium in browser seems to be working much better.

One issue with that is that some shortcuts don't seem to reliably work for me in the browser. In particular, Alt+A, Alt+P to open owned / promoted attributes, Ctrl+W to close Trilium tab. Surprisingly, Alt+R to add new relation does work. I've also sometimes been able to get Alt+P to work, seemingly depending on where exactly focus is.

If you wanna share your PayPal / other place I can use to send you money, I'd like to send you a partial bounty. You can send me the info to [email protected] if don't wanna publish it. Partial just because these shortcuts not working reliably for me makes it a bit of a pain to use Trilium in the browser.

Is Electron fundamentally limited in some way that using Trilium from a browser is not? Could the Electron client's autocomplete be made as fast as when I open Trilium in a browser?

agentydragon avatar May 03 '22 17:05 agentydragon

Is Electron fundamentally limited in some way that using Trilium from a browser is not? Could the Electron client's autocomplete be made as fast as when I open Trilium in a browser?

Electron is just a self-contained Chrome(ium) browser. My guess is that the browser version you are using is newer and with more javascript optimizations (or better compile options), so in that sense if Trilium were to ship with an updated Chrome it would get the same benefits. See http://man.hubwiz.com/docset/electron.docset/Contents/Resources/Documents/docs/development/upgrading-chromium.html

My Trilium javascript console says

process.versions
...
chrome: "96.0.4664.174"
electron: "16.1.0"
v8: "9.6.180.23-electron.0"
...

https://www.electronjs.org/releases/stable?page=5#16.1.0

Hmm that doesn't look too old, though

  • Jan 2022 https://chromium.googlesource.com/chromium/src/+/refs/tags/96.0.4664.174
  • Dec 2021 https://chromium.googlesource.com/v8/v8.git/+/refs/tags/9.6.180.23

Trilium also has some codepaths for browser vs. electron, not sure if those matter.

antoniotejada avatar May 04 '22 00:05 antoniotejada

Autocompletion works fast in both client and browser on my machine with Manjaro (a linux distribution based on Arch). I have two hypotheses.

  1. The gpu driver in Pop!_OS have some performance issue in rendering Electron app. I experienced that when I run some software on Manjaro, and solved that by using proprietary driver instead of open source driver.
  2. There are some system library have incompatibility with electron. A full system update and upgrade may help.

Nriver avatar May 04 '22 03:05 Nriver

I have also just upgraded my system before trying the browser workaround, and I have since not observed any freezes in the Electron app, either. So ... maybe some combination of (1) / (2) was the right explanation? I'll test it a bit longer, and maybe just close this if it looks solved.

agentydragon avatar May 04 '22 03:05 agentydragon

I was going to say that I am on fedora (which is supposed to be faster updates) and have thousands of notes but autocomplete works for me very quickly.. But i figured "it works on my machine" wasn't very helpful 😅

sigaloid avatar May 04 '22 10:05 sigaloid