Skip to content

Reduce queries for deriving proposal diffs and cache it - #1164

Open
unasuke wants to merge 2 commits into
rubycentral:mainfrom
unasuke:proposal-diff-timeout
Open

Reduce queries for deriving proposal diffs and cache it#1164
unasuke wants to merge 2 commits into
rubycentral:mainfrom
unasuke:proposal-diff-timeout

Conversation

@unasuke

@unasuke unasuke commented Aug 3, 2026

Copy link
Copy Markdown
Contributor

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

image

after

image

unasuke and others added 2 commits August 3, 2026 14:10
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>
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.

1 participant