Cache boosts with their message instead of one by one

Every boost had its own fragment, nested inside the message fragment that
already holds it. With a cold cache, a read and a write per boost plus a
read_multi and a write_multi per message: a room page of 40 messages with
120 boosts takes ~410 ms instead of ~290 ms. Redis runs without persistence
(config/redis.conf), so every page is cold after a restart.

A boost touches its message, so adding or removing one rewrites the message
fragment anyway. The inner fragments only paid off when a message with many
boosts was drawn again, and there they hid a lookup per booster. The boosters
are now loaded with the boosts, one query per message.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0142qgjggdJ2KDdGk7RF9Xm9
This commit is contained in:
Marcello Costagliola
2026-10-05 16:40:49 +02:00
parent c73ea37e0c
commit c761763c8c
4 changed files with 60 additions and 19 deletions
@@ -13,6 +13,15 @@ class Messages::BoostsControllerTest < ActionDispatch::IntegrationTest
assert_select ".message__boost-inline a.boost__action[data-action='soft-keyboard#open']"
end
test "index looks up the boosters of all boosts at once" do
@message.boosts.create! booster: users(:jason), content: "🎉"
queries_with_two_boosts = count_queries { get message_boosts_url(@message) }
boost = @message.boosts.create! booster: users(:kevin), content: "👀"
assert_equal queries_with_two_boosts, count_queries { get message_boosts_url(@message) }
assert_select "#" + dom_id(boost)
end
test "create" do
assert_turbo_stream_broadcasts [ @message.room, :messages ], count: 1 do
assert_difference -> { @message.boosts.count }, 1 do
@@ -30,4 +39,12 @@ class Messages::BoostsControllerTest < ActionDispatch::IntegrationTest
end
end
end
private
def count_queries(&block)
count = 0
counter = ->(*, payload) { count += 1 unless payload[:name] == "SCHEMA" || payload[:cached] }
ActiveSupport::Notifications.subscribed(counter, "sql.active_record", &block)
count
end
end