perf: memoize room-list sorts + gate DM-preview listener (PERF-2/4/5)

PERF-2 (RoomMentionAutocomplete): the #-mention list did
`useAtomValue(allRoomsAtom).sort(...)` inline — `.sort()` MUTATED the shared
allRoomsAtom array in place (reordering it for ~27 other consumers) and re-ran
the O(N log N) getRoom compare every keystroke. Copy then memoize:
`useMemo(() => [...allRoomsList].sort(factoryRoomIdByActivity(mx)), ...)`.

PERF-4 (SearchFilters): the room-filter A-Z sort ran every render; wrap in
useMemo keyed on [searchResult, roomList, mx].

PERF-5 (useRoomLatestRenderedEvent + RoomNavItem): the hook registered a GLOBAL
client `Decrypted` listener for every nav item, but its result is only used for
DM rows. Add an `enabled` param (default true) that skips all work + listeners
when false; RoomNavItem passes `!!direct`. The only other caller keeps the
default.

Verified behavior-preserving by two review passes (PERF-2 also fixes a real
shared-atom mutation bug).

Co-Authored-By: Claude Opus 4.8 <noreply@anthropic.com>
This commit is contained in:
2026-07-18 21:05:39 -04:00
co-authored by Claude Opus 4.8
parent 8a15405189
commit 4708a17961
4 changed files with 27 additions and 6 deletions
@@ -152,7 +152,10 @@ function SelectRoomButton({ roomList, selectedRooms, onChange }: SelectRoomButto
getRoomNameStr,
SEARCH_OPTS,
);
const rooms = Array.from(searchResult?.items ?? roomList).sort(factoryRoomIdByAtoZ(mx));
const rooms = useMemo(
() => Array.from(searchResult?.items ?? roomList).sort(factoryRoomIdByAtoZ(mx)),
[searchResult, roomList, mx],
);
const virtualizer = useVirtualizer({
count: rooms.length,
+3 -1
View File
@@ -698,7 +698,9 @@ function RoomNavItem_({
);
const hasDraft = useAtomValue(hasDraftAtom);
const latestEvent = useRoomLatestRenderedEvent(room);
// Only DM rows render this preview — pass `direct` so non-DM nav items don't
// register the global decryption listener (PERF-5).
const latestEvent = useRoomLatestRenderedEvent(room, !!direct);
const dmPreview = (() => {
if (!direct || !latestEvent) return null;
const type = latestEvent.getType();