discourse/app/serializers/topic_tracking_state_serializer.rb
Sam 49f0cc16ba
PERF: optimise serialization for topic tracking state (#20860)
This corrects two issues:

1. We were double serializing topic tracking state (as_json calls were not cached)
2. We were inefficiently serializing items by instantiating extra objects
2023-03-28 18:09:22 +11:00

28 lines
922 B
Ruby

# frozen_string_literal: true
class TopicTrackingStateSerializer < ApplicationSerializer
attributes :data, :meta
def data
serializer = TopicTrackingStateItemSerializer.new(nil, scope: scope, root: false)
# note we may have 1000 rows, avoiding serializer instansitation saves significant time
# for 1000 rows this takes it down from 10ms to 3ms on a reasonably fast machine
object.map do |item|
serializer.object = item
serializer.as_json
end
end
def meta
MessageBus.last_ids(
TopicTrackingState::LATEST_MESSAGE_BUS_CHANNEL,
TopicTrackingState::RECOVER_MESSAGE_BUS_CHANNEL,
TopicTrackingState::DELETE_MESSAGE_BUS_CHANNEL,
TopicTrackingState::DESTROY_MESSAGE_BUS_CHANNEL,
TopicTrackingState::NEW_MESSAGE_BUS_CHANNEL,
TopicTrackingState::UNREAD_MESSAGE_BUS_CHANNEL,
TopicTrackingState.unread_channel_key(scope.user.id),
)
end
end