Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up GET /api/v3/site endpoint #4245

Merged
merged 9 commits into from
Dec 12, 2023
Merged

Speed up GET /api/v3/site endpoint #4245

merged 9 commits into from
Dec 12, 2023

Conversation

Nutomic
Copy link
Member

@Nutomic Nutomic commented Dec 11, 2023

Adds two commits:

  • Make db queries for GET /api/v3/site in parallel using try_join. This required changes to db methods to avoid overlapping &mut borrows of the same value. Luckily these methods are only used in few other places.
  • Cache site response. Most of the data in GetSiteResponse is independent from the user account, so we can easily cache it across requests.

cc @dullbananas

@kroese
Copy link
Contributor

kroese commented Dec 11, 2023

Great!! But in my case all the slowdown is caused by a single line:

let admins = PersonView::admins(&mut context.pool()).await?;

which is untouched in your commit.

Do you have any clues why something as simple as retrieving the admins would take almost a full second?

CommunityModeratorView::for_person(context.inner_pool(), person_id),
LocalUserLanguage::read(context.inner_pool(), local_user_id)
)
.with_lemmy_type(LemmyErrorType::SystemErrLogin)?;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Here it would be much better to read all this data in a single SQL query, but it also seems harder to implement.

PersonBlockView::for_person(context.inner_pool(), person_id),
CommunityModeratorView::for_person(context.inner_pool(), person_id),
LocalUserLanguage::read(context.inner_pool(), local_user_id)
)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of changing the functions to use ActualDbPool, you can replace this with:

lemmy_db_schema::try_join_with_pool!(pool => (
       |pool| CommunityFollowerView::for_person(pool, person_id),
       |pool| CommunityBlockView::for_person(pool, person_id),
       |pool| InstanceBlockView::for_person(pool, person_id),
       |pool| PersonBlockView::for_person(pool, person_id),
       |pool| CommunityModeratorView::for_person(pool, person_id),
       |pool| LocalUserLanguage::read(pool, local_user_id)
     ))

Pool variable must be defined first because the macro only accepts an identifier:

let pool = context.pool();

The queries for building site_response can also run in parallel.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nice I didnt see that macro, works perfectly.

@dessalines dessalines enabled auto-merge (squash) December 12, 2023 14:41
@dessalines dessalines merged commit 30d5886 into main Dec 12, 2023
1 of 2 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants