Skip to content

Reuse message records across binds of the same conversation cursor - #2277

Draft
mpretty-cyro wants to merge 1 commit into
session-foundation:devfrom
mpretty-cyro:fix/conversation-record-cache
Draft

mpretty-cyro wants to merge 1 commit into
session-foundation:devfrom
mpretty-cyro:fix/conversation-record-cache

Conversation

@mpretty-cyro

@mpretty-cyro mpretty-cyro commented Nov 27, 2026 •

Copy link
Copy Markdown
Collaborator

Contributor checklist

  • I have tested my contribution on these devices:
  • Virtual device: Pixel 6 profile, API 37 (arm64), 2 cores / 2 GB, play flavour, qa
  • My contribution is fully baked and ready to be merged as is
  • I ensure that all the open issues my contribution fixes are mentioned in the commit message of my first commit using the Fixes #1234 syntax

Description

Slow frames in the conversation screen. Every message bind built the full MessageRecord for its own row, then again for the rows before and after it. getItemViewType built it once more. Building a record parses its attachment, reaction and content JSON and calls getRecipientSync, which isn't cached. A screen of rows therefore built about four records per row on every rebind, and the cursor-swap notifyDataSetChanged rebinds every visible row on each database change.

ConversationAdapter now caches records by position for the life of a cursor. A bind builds only rows it hasn't seen yet. Rebinds that keep the cursor (selection, search, the last-sent marker) reuse all of them. A new cursor clears the cache, because the same position may now be a different row. MessageRecord has no setters, so sharing one between binds is safe.

One difference: a rebind that keeps the cursor now shows the sender details from when the row was first built. A sender's details changing without a cursor reload is already not refreshed for most rows, and the next message, reaction or thread change reloads the cursor.

Confidence: high that it removes the repeated work. The size of the gain on real devices isn't measured.

Testing: a local build of 1.33.9 with only this change and the album change (#2278) on top, against plain 1.33.9, on the same Note to Self (about 100 texts, one long message, about 20 image messages). I profiled the main thread with simpleperf while running the same scripted scroll:

  • getItemViewType: 9.3–10.0% of main-thread CPU before, 0.4–0.5% after.
  • getRecipientSync: 3.8–4.4% before, 0.6–0.7% after.
  • All record building now takes 1.6–1.8%.
  • A temporary counter (not in this PR) showed 1,132 record lookups during one scroll, with 1,084 served from the cache.

Frame stats from dumpsys gfxinfo didn't change measurably, because the emulator's rendering dominates every frame.

I also checked that selecting messages, deleting selected messages on this device, receiving new messages afterwards, and the long-press menu all behave as before.

Each bind built the full MessageRecord for its row and both neighbours, plus once more for getItemViewType, and every record parses its attachment, reaction and content JSON and looks up its sender without a cache. A screen of rows built about four records per row on every rebind. Records are now cached by position for the life of a cursor, so a bind builds only rows it hasn't seen, and rebinds that keep the cursor reuse them all. A new cursor clears the cache.

@mpretty-cyro mpretty-cyro left a comment

Copy link
Copy Markdown
Collaborator Author

Choose a reason for hiding this comment

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

.

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