Reduce queries for deriving proposal diffs and cache it - #1164
Open
unasuke wants to merge 2 commits into
Open
Conversation
PaperTrail deserializes a changeset through `item`, and its polymorphic `belongs_to` declares `inverse_of: false`, so every version loaded from the association queried for the proposal it already came from. The review screen reads the changeset of every version, so a proposal with 30 revisions issued 32 queries per request. Declaring `inverse_of` on the `has_many` drops that to 2, and cuts the cached render of a 30-revision proposal from 33.2ms to 13.1ms. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Diffy's html format highlights changes within a line by re-running diff at character granularity, spawning a diff process per changed chunk. That dominates the response: a proposal with 30 revisions took 1.2s to render, and larger histories timed out. Versions are immutable, so the markup can be cached indefinitely. The same proposal now renders in 12.8ms once warm, with the inline highlighting kept as is. Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
What
During our review process, we occasionally encountered request timeouts. The proposals causing timeouts had a lot of change histories. After creating similar data locally and conducting verification, we discovered that N+1 queries were being generated.
This pull request addresses the N+1 query issue, includes caching to prevent timeouts since the diff content remains unchanged.
Screenshots
before
after