From 8025a859f43dfdbed0c4e8160277e62011315af3 Mon Sep 17 00:00:00 2001 From: Erwan Leboucher Date: Thu, 23 Jul 2026 17:54:27 +0200 Subject: [PATCH] perf: reuse the SDK singleton PushProcessor instead of allocating per call new PushProcessor(room.client) was allocated per room on every sync cycle (in the getUnreadInfo fallback path) and per component mount in RoomTimeline/ThreadDrawer/notifications. The SDK already exposes a cached singleton at mx.pushProcessor (client.ts:1293). Element Web uses the same pattern. Replace all five allocations and drop the now-unused imports. --- src/app/features/room/RoomTimeline.tsx | 4 ++-- src/app/features/room/ThreadDrawer.tsx | 3 +-- src/app/pages/client/BackgroundNotifications.tsx | 3 +-- src/app/pages/client/ClientNonUIFeatures.tsx | 3 +-- src/app/utils/room.ts | 3 +-- 5 files changed, 6 insertions(+), 10 deletions(-) diff --git a/src/app/features/room/RoomTimeline.tsx b/src/app/features/room/RoomTimeline.tsx index 4e7ef1850..152691c86 100644 --- a/src/app/features/room/RoomTimeline.tsx +++ b/src/app/features/room/RoomTimeline.tsx @@ -12,7 +12,7 @@ import { import type { Editor } from 'slate'; import { useAtomValue, useSetAtom, useStore } from 'jotai'; import type { Room } from '$types/matrix-sdk'; -import { PushProcessor, Direction, EventType } from '$types/matrix-sdk'; +import { Direction, EventType } from '$types/matrix-sdk'; import classNames from 'classnames'; import type { VListHandle } from 'virtua'; import { VList } from 'virtua'; @@ -428,7 +428,7 @@ export function RoomTimeline({ const optionalSpace = useSpaceOptionally(); const roomParents = useAtomValue(roomToParentsAtom); const imagePackRooms = useImagePackRooms(room.roomId, roomParents); - const pushProcessor = useMemo(() => new PushProcessor(mx), [mx]); + const pushProcessor = mx.pushProcessor; const parseMemberEvent = useMemberEventParser(); const replyDraftAtom = useMemo(() => roomIdToReplyDraftAtomFamily(room.roomId), [room.roomId]); diff --git a/src/app/features/room/ThreadDrawer.tsx b/src/app/features/room/ThreadDrawer.tsx index c7726fabc..6bd36d44d 100644 --- a/src/app/features/room/ThreadDrawer.tsx +++ b/src/app/features/room/ThreadDrawer.tsx @@ -7,7 +7,6 @@ import { Direction, MatrixEvent, MatrixEventEvent, - PushProcessor, ReceiptType, RelationType, RoomEvent, @@ -137,7 +136,7 @@ export function ThreadDrawer({ room, threadRootId, onClose, overlay }: ThreadDra (userId: string) => jotaiStore.get(profilesCacheAtom)[userId], [jotaiStore] ); - const pushProcessor = useMemo(() => new PushProcessor(mx), [mx]); + const pushProcessor = mx.pushProcessor; const useAuthentication = useMediaAuthentication(); const mentionClickHandler = useMentionClickHandler(room.roomId); const settingsLinkBaseUrl = useSettingsLinkBaseUrl(); diff --git a/src/app/pages/client/BackgroundNotifications.tsx b/src/app/pages/client/BackgroundNotifications.tsx index be8cdc92c..c54b6ecc5 100644 --- a/src/app/pages/client/BackgroundNotifications.tsx +++ b/src/app/pages/client/BackgroundNotifications.tsx @@ -7,7 +7,6 @@ import { MatrixEventEvent, RoomEvent, SyncState, - PushProcessor, EventType, } from '$types/matrix-sdk'; @@ -327,7 +326,7 @@ export function BackgroundNotifications() { }); } - const pushProcessor = new PushProcessor(mx); + const pushProcessor = mx.pushProcessor; const handleAccountData = (event: MatrixEvent) => { if (event.getType() === (EventType.Direct as string)) { diff --git a/src/app/pages/client/ClientNonUIFeatures.tsx b/src/app/pages/client/ClientNonUIFeatures.tsx index b39ec1512..4cd4aa384 100644 --- a/src/app/pages/client/ClientNonUIFeatures.tsx +++ b/src/app/pages/client/ClientNonUIFeatures.tsx @@ -11,7 +11,6 @@ import { getPresenceSyncManager } from '$client/initMatrix'; import { MatrixEvent, MatrixEventEvent, - PushProcessor, RoomEvent, SetPresence, SyncState, @@ -339,7 +338,7 @@ function MessageNotifications() { }, []); useEffect(() => { - const pushProcessor = new PushProcessor(mx); + const pushProcessor = mx.pushProcessor; // Track encrypted events that should skip focus check when decrypted (because we // already checked focus when the encrypted event arrived, and want to use that // original state rather than re-checking after decryption completes). diff --git a/src/app/utils/room.ts b/src/app/utils/room.ts index 922a6bf91..7b8243554 100644 --- a/src/app/utils/room.ts +++ b/src/app/utils/room.ts @@ -16,7 +16,6 @@ import { EventTimeline, EventType, NotificationCountType, - PushProcessor, PushRuleActionName, RelationType, MsgType, @@ -427,7 +426,7 @@ export const getUnreadInfo = (room: Room, options?: UnreadInfoOptions): UnreadIn const liveEvents = room.getLiveTimeline().getEvents(); let fallbackTotal = 0; let fallbackHighlight = 0; - const pushProcessor = new PushProcessor(room.client); + const pushProcessor = room.client.pushProcessor; for (let i = liveEvents.length - 1; i >= 0; i -= 1) { const event = liveEvents[i]; if (!event) break;