Skip to content

Reduce repeated favorite queries while serializing API v3 project collections. - #24948

Open
haseebeqx wants to merge 1 commit into
opf:devfrom
haseebeqx:performance/fix-per-project-favorite-existance-check-in-project-api
Open

Reduce repeated favorite queries while serializing API v3 project collections. #24948
haseebeqx wants to merge 1 commit into
opf:devfrom
haseebeqx:performance/fix-per-project-favorite-existance-check-in-project-api

Conversation

@haseebeqx

Copy link
Copy Markdown
Contributor

Serializing GET /api/v3/projects calls Project#favorited_by? while building each project’s favorite links and favorited property. This resulted in a separate favorite existence query for every project in the collection.

In the profiled workload, 50 favorite existence queries were issued for a page containing 50+ projects.

Changes

  1. The project collection eager-loading wrapper now loads the current user’s favorite project IDs in one query for the complete page.
  2. favorited_by? uses the preloaded value when called for that user. Calls for another user, or without preloaded
    data, continue to delegate to the underlying project model.
  3. The project collection representer now always invokes its project-specific eager-loading wrapper and passes the
    current user to it.
  4. The offset-paginated collection makes the current user available while paginated records are being prepared.
  5. Added tests covering bulk loading, favorite-state accuracy, fallback behavior for other users, and collection integration.

Performance

Measured in production mode against:

GET /api/v3/projects?pageSize=50&offset=1

The dataset contained 62 active projects, with 50 returned per request. The observed values are medians from five runs per state in a local environment using a system-admin API key

Metric Before After Change
Median server duration 139.49 ms 89.86 ms -35.6%
Median SQL duration 16.18 ms 5.76 ms -64.4%
SQL events 58 9 -84.5%
Non-cached SQL events 58 9 -84.5%
Duplicate query fingerprints 49 0 -100.0%
Per-project favorite existence checks 50 0 -100.0%

Merge checklist

  • Added/updated tests
  • Added/updated documentation in Lookbook (patterns, previews, etc)
  • Tested major browsers (Chrome, Firefox, Edge, ...)

- optimize project collection serialization by bulk-loading the current user’s favorite state
- avoids repeated Favorite queries when rendering each project’s favor/disfavor links and favorited property
end

def eager_load_for_element_decorator?
true

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

Project loading uses custom wrapper logic beyond the representer’s declarative Rails eager-load configuration. Returning true ensures that wrapper always runs for the paginated project relation.

@haseebeqx

Copy link
Copy Markdown
Contributor Author

@oliverguenther Please review

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

Labels

None yet

Development

Successfully merging this pull request may close these issues.

1 participant