Skip to content

Carry the message menu once per room instead of in every message - #357

Open
namespaceMarcello wants to merge 2 commits into
basecamp:mainfrom
namespaceMarcello:build-message-menus-from-one-template
Open

namespaceMarcello wants to merge 2 commits into
basecamp:mainfrom
namespaceMarcello:build-message-menus-from-one-template

Conversation

@namespaceMarcello

Copy link
Copy Markdown
Contributor

Every message is rendered with its options menu inside it: eight quick boost forms, new boost, reply, copy link and edit. On main a text message is 9,191 bytes of HTML and about 5,900 of them are that menu, the same for every message but for the message's id. It travels with each message: in the room page, in every page of older messages, and in the frame that Action Cable encodes and writes once per person in the room when a message is posted. That last cost is paid once per recipient, so it grows with the room.

This makes a message carry the button alone:

  • messages/_actions.html.erb renders the <details> and its <summary>. The menu moves to messages/_actions_menu.html.erb.
  • The room page renders the menu once, in messages/_actions_menu_template.html.erb: a <script type="text/template"> like the one pending messages use, with $messageId$ where the id goes.
  • messages_controller.js copies it into a message the first time its menu is opened. It does so on the toggle that popup#toggle already listens to, and ahead of it, because that one measures the menu to place it; this is also why the copy is synchronous rather than a lazily loaded frame. Arrow up, which clicks the edit link of your last message, builds the menu first.
  • A message with a file keeps rendering its own menu, since Download and Share point at the file.
  • cache_message_actions goes: what is left of a message's controls is a static button.

Once open, the menu is the same markup in the same place as before. Two things do change:

  • A copy link is built by the page it is copied from, so it follows the reader's origin. On main a message that arrived by broadcast carries the poster's.
  • A tab left open across the upgrade keeps the old script and has no template. Messages that arrive there afterwards show the button, but their menu doesn't open until the page reloads, which Turbo does on the next navigation because the importmap is tracked. Messages already on the page keep working.

#350 changes the same partials and cache_message_actions: it caches the rendering of the menu, where this takes the menu out of the message. Whichever lands second needs a rebase, and this branch will get it if it is the one.

Measurements

Counts, from the production image built from main at edbc779 and from this branch, a room page with 40 text messages:

main this PR
one message, as broadcast 9,191 bytes 3,309 bytes
room page 400,532 bytes 171,199 bytes
room page, gzip 15,885 bytes 11,897 bytes
elements in the room page's DOM 3,604 1,525

The template adds 5,863 bytes to the room page, once.

Timings, from our machine: a laptop with an AMD Ryzen 9 7940HX, Docker. The same two production images (Thruster, 3 Puma workers with 5 threads, 3 Resque workers, Redis inside) on 4 CPUs, the database on a Docker volume (ext4), and loadgen cable from basecamp/once-campfire-verification on 4 other CPUs: 1,000 people, each with the room page's subscriptions, in one open room. Median of 3 runs alternating main and this PR, lowest and highest in brackets; the paced phase is 30 messages.

1,000 people in the room main this PR
posting as fast as it goes: messages delivered to everyone per second 13.1 (12.5–13.4) 18.1 (17.8–18.8)
at 5 messages a second: until the last of the 1,000 has the message, p50 74 ms (67–75) 47 ms (44–48)
same, p99 168 ms (143–201) 104 ms (103–112)
at 5 messages a second: server CPU per message 502 ms (448–506) 416 ms (395–420)

In a browser the gain is smaller. Headless Chrome on the same machine, loading that room page from each image in turn, 15 loads each, every load in a fresh tab: the main thread is busy 375 ms per load on main (351–427) and 323 ms with this PR (292–381), the page has 1,379 event listeners against 939 and a JavaScript heap of 7.3 MB against 5.7 MB. DOMContentLoaded doesn't move (214 and 222 ms).

Tests

  • test/controllers/rooms_controller_test.rb: the room page has a button in every message, no menu in any, and one template; filled in for a message, the template's new boost and edit links point at that message and its frames, and it has Reply and no Download or Share. On main it fails on the menus found inside the messages.
  • test/controllers/messages/boosts_controller_test.rb: the quick boost test reads its forms from the template filled in for the message, and still posts one.
  • test/controllers/messages_controller_test.rb: a posted text message is broadcast with its button and no menu (fails on main); a posted file is broadcast with its own menu, checked link by link (passes on main too); the copy link test now covers the room's template under each origin, and a file's broadcast and regular read as before.
  • test/controllers/fragment_cache_test.rb: the test of the menu cache keeps its other half, the author rendered fresh after a creator edit.
  • test/system/message_menu_test.rb: a quick boost from the menu; a message has no menu until it is opened, gets one placed by popup#toggle on the first open, and still has one after closing and reopening (fails on main at the first step); a message with a file opens the menu it came with.
  • The existing system tests go through the new path: new boost, reply, edit, and arrow up to edit the last message.

Each of these was seen failing with one thing broken at a time: the copy without its guard, the action removed or placed after popup#toggle, arrow up without the copy, the id left unfilled, each of the three frame ids, the template missing from the page or given the file's buttons, a file's menu dropped, the copy link without its origin.

Validation in the container: bin/rubocop, bin/brakeman, bin/rails herb:check, bin/rails test (582 runs) and bin/rails test:system (30 runs) all pass with no failures.

Written by Claude Code (AI assistant) on behalf of @namespaceMarcello, who directs this work.

🤖 Generated with Claude Code

A message is rendered with its options menu inside it: eight quick
boost forms, new boost, reply, copy link and edit. The menu is about
two thirds of the message's HTML and is the same for every message but
for the message's id, yet it travels with each one: in the room page,
in every page of older messages, and in the frame that Action Cable
encodes and writes once per person in the room for each new message.

A message now carries the button alone. The room page carries the menu
once, as a template with $messageId$ where the id goes, and the
messages controller copies it into a message the first time its menu
is opened, or when arrow up asks for the edit link of the last message.
The copy happens on the same toggle that popup#toggle listens to, and
before it, because that one measures the menu to place it.

A message with a file still renders its own menu: Download and Share
point at the file. The cache of rendered menus goes, as what is left of
a message's controls is a static button. A copy link is now built by
the page it is copied from, so it follows the reader's origin where a
broadcast message used to carry the poster's.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017uAnU1dboBiJTZ23xmGpPe
Copilot AI balanced review requested due to automatic review settings October 10, 2026 09:46

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

🟡 Changes recommended

Text-message menus are broken on search results and standalone message pages because those views lack the required controller and template.

1 open finding
What changed in this PR

Moves shared message menus to a room-level template, reducing repeated HTML while preserving attachment-specific menus.

Changes:

  • Lazily inserts shared menus for text messages.
  • Retains inline menus for attachments.
  • Updates controller and system coverage.

[!TIP]
If you aren't ready for review, convert to a draft PR.
Click "Convert to draft" or run gh pr ready --undo.
Click "Ready for review" or run gh pr ready to reengage.

File Description
app/​helpers/​messages_helper.rb Removes action-menu fragment caching.
app/​javascript/​controllers/​messages_controller.js Instantiates menus from the shared template.
app/​views/​messages/​_actions.html.erb Reduces actions to a button and attachment menu.
app/​views/​messages/​_actions_menu.html.erb Defines reusable menu markup.
app/​views/​messages/​_actions_menu_template.html.erb Provides the room-level template.
app/​views/​messages/​_message.html.erb Removes the copy-link URL argument.
app/​views/​rooms/​show.html.erb Renders the shared menu template.
test/​controllers/​fragment_cache_test.rb Removes obsolete controls-cache assertions.
test/​controllers/​messages/​boosts_controller_test.rb Tests quick boosts through the template.
test/​controllers/​messages_controller_test.rb Covers broadcasts, attachments, and origins.
test/​controllers/​rooms_controller_test.rb Verifies one template per room.
test/​system/​message_menu_test.rb Covers lazy and attachment menus.
test/​test_helper.rb Includes the menu test helper.
test/​test_helpers/​message_menu_test_helper.rb Builds instantiated menus for assertions.

🧠 Review effort: Balanced


💡 Add a code-review agent skill or configure MCP servers for context-aware, tailored reviews. Learn more in the docs.

Comment on lines +12 to +13
<% if message.attachment? %>
<%= render "messages/actions_menu", message_id: message.id, room: message.room, attachment: message.attachment %>

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.

The menu does depend on the room page's controller and template, and those two views have neither. Neither of them puts the button within reach, though, on main or here:

  • Search results hide a message's actions altogether: .searches .message__actions { display: none !important; visibility: hidden !important; } in sidebar.css.
  • messages/show is the source of a message's edit frame: the redirect after an update and the editor's close link both load it inside the frame. Opened on its own, the message stays visibility: hidden, because a message only becomes visible once the messages or the search results controller formats it (.message--formatted), and that page has neither.

So no click gets to an empty <details> there. 10795f5 adds a system test that holds both pages to it: the message is there and its options button can't be reached. It fails if the search rule is removed, or if a message is made visible without being formatted, which is when those pages would need the template too.

Written by Claude Code (AI assistant) on behalf of @namespaceMarcello, who directs this work.

A message's menu is copied from the template its room page carries, by
that page's messages controller. Search results and the page of a
single message render messages without either, and rely on the button
not being reachable there: search results hide a message's actions,
and a message opened on its own is never formatted into view.

Hold both pages to that, so that showing the button on either one
fails here and points at the template.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_017uAnU1dboBiJTZ23xmGpPe
@namespaceMarcello

Copy link
Copy Markdown
Contributor Author

A second browser measurement, of gestures rather than of a page load. The description's figures are for a room page opened in a fresh tab, where the gain is small. Switching room, reloading and scrolling back repeat the per-message work without the tab's fixed cost, and there it shows more.

Setup. From our machine: a laptop with an AMD Ryzen 9 7940HX, Docker. The production images of main at edbc779 and of this branch at 27d7059 (the head adds a test only), on 4 CPUs. Five people, each in a headless Chrome 155 of their own on other CPUs, in rooms of 100 text messages; the page shows 40. One of the five is timed, from the click or the scroll to the next animation frame after every message on the page is formatted. HTTP/1.1 over Docker's bridge. "Simulated phone" is Chrome's CPU throttling at 4× plus 40 ms on every HTTP request, for the timed person only. Each figure is the median of 5 repetitions in one run; two figures are two runs.

main this PR main, simulated phone this PR, simulated phone
switch to a room with a new message 176, 179 ms 128, 113 ms 511 ms 389, 358 ms
switch to a room with nothing new 154, 143 ms 117, 132 ms 538 ms 353, 372 ms
reload the app, warm cache 217, 200 ms 152, 144 ms 952 ms 661, 607 ms
scroll to the top: the older page is in 117, 117 ms 79, 74 ms 381 ms 265, 269 ms

What a switch to a room with a new message costs the main thread:

main this PR main, simulated phone this PR, simulated phone
script 86, 87 ms 42, 37 ms 290 ms 189, 170 ms
layout and style 42, 41 ms 42, 40 ms 200 ms 206, 181 ms
long tasks 97, 97 ms 54, 0 ms 491 ms 298, 238 ms

The server answers the room page in about 30 ms on both images; the difference is the browser's. In this scenario the page has 3,903 elements on main and 1,824 with this PR, and in a sampling profile Stimulus takes 58 ms of a switch on main and 28 ms with this PR (231 and 136 ms on the simulated phone). Layout and style don't move: a closed menu has no layout.

What doesn't change: your own message appearing after Enter (11 and 10 ms), the message reaching the other four, typing notices, boosts and the unread dot, all under 120 ms on both.

What this does not show. Headless Chrome on Linux, not Safari, Firefox or a real phone: the throttling is an emulation. A local network. Text-only messages. Five repetitions per figure, and one run only for main on the simulated phone.

The scenario (Selenium driving the five browsers, a small in-page stopwatch, Chrome's Performance and Profiler domains) can be posted here.

Written by Claude Code (AI assistant) on behalf of @namespaceMarcello, who directs this work.

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.

2 participants