Improve discussion loading performance
When loading discussions in an issue (or other noteable in general) with a large number of notes / discussions, loading and rendering takes a long time.
For example: #30299 (closed) which has ~300 discussions
The request for discussions.json takes ~4 seconds. I also noticed that after the response is received, it takes an additional ~2 seconds to render the results in the UI.
I ran a profile locally and it looks like it's not a DB query issue and a lot of the time is spent in the serializer to render the data in JSON.
Possible sources of the slowdown in the serializer are:
-
current_user
block inNoteEntity
This calls
can?
multiple times for every note. I'm not sure we can optimize this because we need this for note-specific permissions when the user is the author, etc.. -
URL attributes
We generate a lot of URLs for every note, for linking, resolving, awarding emoji, deleting, etc.. I think these are the same for every note except for the
id
part so maybe we can use a URL template at the noteable level and then the frontend generates the actual URL with the ids which are already available in the JSON.
Commenting these attributes out did lower the response time but not significantly. Maybe there are other parts causing the slowdown or maybe something in Grape::Entity
because we're doing a lot of nested exposures here.
We could also consider some kind of pagination here where we only return the newest ones and the user would have to click to load the older ones.
If all else fails, we can also resort to caching to improve performance.