From 2d95c353bf6b40fb4f3ecb5ee59fd2ebbbc36846 Mon Sep 17 00:00:00 2001 From: 7w1 Date: Thu, 16 Jul 2026 16:11:32 -0500 Subject: [PATCH] optimize room data subscriptions --- .changeset/improve-sliding-sync.md | 1 + src/app/hooks/useSlidingSyncActiveRoom.ts | 9 +- src/client/slidingSync.test.ts | 124 +++++++++++++++- src/client/slidingSync.ts | 173 +++++++++++++++++++--- 4 files changed, 277 insertions(+), 30 deletions(-) diff --git a/.changeset/improve-sliding-sync.md b/.changeset/improve-sliding-sync.md index 2f5c321d0..e4569ce8f 100644 --- a/.changeset/improve-sliding-sync.md +++ b/.changeset/improve-sliding-sync.md @@ -7,6 +7,7 @@ Improve sliding sync. - Make sliding sync easier to enable during login or loading and from settings. - Move the sliding sync toggle from Experimental to the bottom of General settings. - Start faster and restore rooms and spaces sooner. +- Reduce unnecessary background room data. - Improve room details, memberships, presence, and unread indicators. - Make startup loading smoother for both sliding and classic sync. - It's fast now :3 diff --git a/src/app/hooks/useSlidingSyncActiveRoom.ts b/src/app/hooks/useSlidingSyncActiveRoom.ts index 66f9e9a1c..a46e56b45 100644 --- a/src/app/hooks/useSlidingSyncActiveRoom.ts +++ b/src/app/hooks/useSlidingSyncActiveRoom.ts @@ -84,10 +84,15 @@ export const useSlidingSyncRouteRooms = (): void => { if (!manager || resolvingRoom || resolvingSpace) return undefined; const activeRoomIds = [...new Set([spaceId, roomId].filter(Boolean))] as string[]; - activeRoomIds.forEach((activeRoomId) => manager.subscribeToRoom(activeRoomId)); + manager.setActiveRoomSubscriptions(activeRoomIds); - return () => activeRoomIds.forEach((activeRoomId) => manager.unsubscribeFromRoom(activeRoomId)); + return undefined; }, [manager, resolvingRoom, resolvingSpace, roomId, spaceId]); + + useEffect(() => { + if (!manager) return undefined; + return () => manager.setActiveRoomSubscriptions([]); + }, [manager]); }; export const useSlidingSyncSpaceSubscriptions = (): void => { diff --git a/src/client/slidingSync.test.ts b/src/client/slidingSync.test.ts index b718fdc7b..e3f8b8d7e 100644 --- a/src/client/slidingSync.test.ts +++ b/src/client/slidingSync.test.ts @@ -94,13 +94,13 @@ function fireLifecycle(state: SlidingSyncState, response: unknown = {}) { lifecycle?.(state, response); } -function fireRoomData(roomId: string) { +function fireRoomData(roomId: string, data: Record = {}) { const roomDataHandler = mocks.slidingSyncInstance.on.mock.calls .toReversed() .find(([event]) => event === SlidingSyncEvent.RoomData)?.[1] as | ((dataRoomId: string, data: unknown) => void) | undefined; - roomDataHandler?.(roomId, {}); + roomDataHandler?.(roomId, data); } describe('SlidingSyncManager initial request', () => { @@ -109,6 +109,7 @@ describe('SlidingSyncManager initial request', () => { const lists = mocks.slidingSyncConstructorArgs?.[1] as Map; const joined = lists.get('joined'); + const updates = lists.get('updates'); const defaultSubscription = mocks.slidingSyncConstructorArgs?.[2] as { timeline_limit: number; }; @@ -118,6 +119,12 @@ describe('SlidingSyncManager initial request', () => { expect(joined?.required_state).toHaveLength(8); expect(joined?.required_state).toContainEqual([EventType.RoomJoinRules, '']); expect(joined?.required_state).not.toContainEqual(['m.space.child', '*']); + expect(updates).toMatchObject({ + ranges: [[0, 29]], + timeline_limit: 1, + required_state: [[EventType.RoomMember, '$ME']], + filters: { is_invite: false }, + }); expect(defaultSubscription.timeline_limit).toBe(30); expect(mocks.slidingSyncConstructorArgs?.[4]).toBe(45000); }); @@ -230,9 +237,78 @@ describe('SlidingSyncManager initial request', () => { rangeEnd: 29, }); }); + + it('keeps full lightweight event coverage after narrowing the detailed joined list', () => { + const listRanges = new Map([ + ['joined', [[0, 29]]], + ['updates', [[0, 29]]], + ]); + const manager = makeManager(makeMockMx()); + mocks.slidingSyncInstance.getListData.mockImplementation((key: string) => + key === 'joined' || key === 'updates' ? ({ joinedCount: 45 } as never) : null + ); + mocks.slidingSyncInstance.getListParams.mockImplementation( + (key: string) => ({ ranges: listRanges.get(key) ?? [[0, 29]] }) as never + ); + mocks.slidingSyncInstance.setListRanges.mockImplementation((key, ranges) => { + if (key === 'joined' || key === 'updates') { + listRanges.set(key, ranges as [number, number][]); + } + }); + manager.attach(); + + fireLifecycle(SlidingSyncState.Complete); + expect(listRanges.get('joined')).toEqual([[0, 44]]); + expect(listRanges.get('updates')).toEqual([[0, 44]]); + + fireLifecycle(SlidingSyncState.Complete); + fireLifecycle(SlidingSyncState.Complete); + expect(listRanges.get('joined')).toEqual([[0, 2]]); + expect(listRanges.get('updates')).toEqual([[0, 44]]); + }); + + it('keeps detailed coverage when the homeserver does not provide the updates list', () => { + let joinedRange: [number, number][] = [[0, 29]]; + const manager = makeManager(makeMockMx()); + mocks.slidingSyncInstance.getListData.mockImplementation((key: string) => + key === 'joined' ? ({ joinedCount: 45 } as never) : null + ); + mocks.slidingSyncInstance.getListParams.mockImplementation((key: string) => + key === 'joined' ? ({ ranges: joinedRange } as never) : ({ ranges: [[0, 29]] } as never) + ); + mocks.slidingSyncInstance.setListRanges.mockImplementation((key, ranges) => { + if (key === 'joined') joinedRange = ranges as [number, number][]; + }); + manager.attach(); + + fireLifecycle(SlidingSyncState.Complete); + fireLifecycle(SlidingSyncState.Complete); + + expect(joinedRange).toEqual([[0, 44]]); + }); }); describe('SlidingSyncManager room subscription coordination', () => { + it('replaces route subscriptions atomically without cycling the retained space', () => { + const manager = makeManager(makeMockMx()); + const spaceId = '!space:example.com'; + const oldRoomId = '!old:example.com'; + const newRoomId = '!new:example.com'; + + manager.setActiveRoomSubscriptions([spaceId, oldRoomId]); + mocks.slidingSyncInstance.modifyRoomSubscriptions.mockClear(); + + manager.setActiveRoomSubscriptions([spaceId, newRoomId]); + + expect(mocks.slidingSyncInstance.modifyRoomSubscriptions).toHaveBeenCalledOnce(); + expect(mocks.slidingSyncInstance.modifyRoomSubscriptions).toHaveBeenCalledWith( + new Set([spaceId, newRoomId]) + ); + expect(manager.isRoomActive(spaceId)).toBe(true); + expect(manager.isRoomActive(oldRoomId)).toBe(false); + expect(manager.isRoomActive(newRoomId)).toBe(true); + }); + it('reports loading until the subscribed room returns data', () => { const manager = makeManager(makeMockMx()); const roomId = '!slow:example.com'; @@ -281,6 +357,44 @@ describe('SlidingSyncManager room subscription coordination', () => { ); }); + it('keeps space subscriptions active for future hierarchy changes', async () => { + const manager = makeManager(makeMockMx()); + const roomId = '!space:example.com'; + (manager as unknown as { listsFullyLoaded: boolean }).listsFullyLoaded = true; + manager.attach(); + + manager.setSpaceSubscriptions([roomId]); + expect(mocks.slidingSyncInstance.modifyRoomSubscriptions).toHaveBeenLastCalledWith( + new Set([roomId]) + ); + + fireRoomData(roomId); + await Promise.resolve(); + expect(mocks.slidingSyncInstance.modifyRoomSubscriptions).toHaveBeenLastCalledWith( + new Set([roomId]) + ); + }); + + it('hydrates sidebar state once for rooms first seen after startup', async () => { + const manager = makeManager(makeMockMx()); + const roomId = '!new:example.com'; + ( + manager as unknown as { initialListHydrationCompleted: boolean } + ).initialListHydrationCompleted = true; + manager.attach(); + + fireRoomData(roomId, { initial: true, notification_count: 0, highlight_count: 0 }); + await Promise.resolve(); + expect(mocks.slidingSyncInstance.useCustomSubscription).toHaveBeenLastCalledWith( + roomId, + 'sidebar_room' + ); + + fireRoomData(roomId, { initial: true }); + await Promise.resolve(); + expect(mocks.slidingSyncInstance.modifyRoomSubscriptions).toHaveBeenLastCalledWith(new Set()); + }); + it('removes image-pack subscriptions which are no longer configured', () => { const manager = makeManager(makeMockMx()); @@ -359,9 +473,9 @@ describe('SlidingSyncManager local membership reconciliation', () => { }); it('keeps cached rooms when authoritative membership cannot be fetched', async () => { - const getJoinedRooms = vi.fn<() => Promise<{ joined_rooms: string[] }>>().mockRejectedValue( - new Error('offline') - ); + const getJoinedRooms = vi + .fn<() => Promise<{ joined_rooms: string[] }>>() + .mockRejectedValue(new Error('offline')); const manager = makeManager(makeMockMx({ getJoinedRooms })); const internals = manager as unknown as { reconcileSidebarCacheMembership: () => void; diff --git a/src/client/slidingSync.ts b/src/client/slidingSync.ts index 98289188b..5b090dfce 100644 --- a/src/client/slidingSync.ts +++ b/src/client/slidingSync.ts @@ -30,16 +30,19 @@ const debugLog = createDebugLogger('slidingSync'); export const LIST_JOINED = 'joined'; export const LIST_INVITES = 'invites'; +export const LIST_UPDATES = 'updates'; export const LIST_SEARCH = 'search'; export const LIST_ROOM_SEARCH = 'room_search'; export const LIST_SPACE = 'space'; const LIST_TIMELINE_LIMIT = 1; const LIST_PAGE_SIZE = 30; +const STEADY_STATE_DETAILED_ROOMS = 3; const DEFAULT_POLL_TIMEOUT_MS = 45000; const LIST_SORT_ORDER = ['by_recency', 'by_name']; const ACTIVE_ROOM_SUBSCRIPTION_KEY = 'active_room'; +const SIDEBAR_ROOM_SUBSCRIPTION_KEY = 'sidebar_room'; const SPACE_SUBSCRIPTION_KEY = 'space'; const IMAGE_PACK_SUBSCRIPTION_KEY = 'image_packs'; const ACTIVE_ROOM_TIMELINE_LIMIT = 30; @@ -146,6 +149,11 @@ const buildSpaceSubscription = (): MSC3575RoomSubscription => ({ required_state: SPACE_REQUIRED_STATE, }); +const buildSidebarRoomSubscription = (): MSC3575RoomSubscription => ({ + timeline_limit: LIST_TIMELINE_LIMIT, + required_state: buildListRequiredState(), +}); + const buildLists = (): Map => { const lists = new Map(); const listRequiredState = buildListRequiredState(); @@ -166,6 +174,14 @@ const buildLists = (): Map => { filters: { is_invite: true }, }); + lists.set(LIST_UPDATES, { + ranges: [[0, LIST_PAGE_SIZE - 1]], + sort: LIST_SORT_ORDER, + timeline_limit: LIST_TIMELINE_LIMIT, + required_state: [[EventType.RoomMember, MSC3575_STATE_KEY_ME]], + filters: { is_invite: false }, + }); + return lists; }; @@ -181,12 +197,16 @@ export class SlidingSyncManager { private readonly activeRoomSubscriptions = new Set(); + private readonly sidebarRoomSubscriptions = new Set(); + private readonly spaceSubscriptions = new Set(); private deferredSpaceSubscriptions = new Set(); private readonly imagePackRoomSubscriptions = new Set(); + private roomSubscriptionSyncQueued = false; + private readonly roomTimelineLimit: number; private readonly sidebarCache: SlidingSyncSidebarCache; @@ -218,6 +238,8 @@ export class SlidingSyncManager { private listsFullyLoaded = false; + private initialListHydrationCompleted = false; + private sidebarCacheReconciled = false; private sidebarCacheReconciliationQueued = false; @@ -288,6 +310,10 @@ export class SlidingSyncManager { ACTIVE_ROOM_SUBSCRIPTION_KEY, buildUnencryptedSubscription(roomTimelineLimit) ); + this.slidingSync.addCustomSubscription( + SIDEBAR_ROOM_SUBSCRIPTION_KEY, + buildSidebarRoomSubscription() + ); this.slidingSync.addCustomSubscription( IMAGE_PACK_SUBSCRIPTION_KEY, buildImagePackSubscription() @@ -355,7 +381,7 @@ export class SlidingSyncManager { const currentCount = listData?.joinedCount ?? 0; const previousCount = this.previousListCounts.get(key) ?? 0; - totalRoomCount += currentCount; + if (key !== LIST_UPDATES) totalRoomCount += currentCount; if (currentCount !== previousCount) { changes[key] = { @@ -431,11 +457,35 @@ export class SlidingSyncManager { if (member.membership !== KnownMembership.Leave && member.membership !== KnownMembership.Ban) return; this.sidebarCache.removeRoom(member.roomId); - if (!this.activeRoomSubscriptions.has(member.roomId)) return; - this.unsubscribeFromRoom(member.roomId); + const removedSpaceSubscription = this.spaceSubscriptions.delete(member.roomId); + const removedSidebarSubscription = this.sidebarRoomSubscriptions.delete(member.roomId); + if (this.activeRoomSubscriptions.has(member.roomId)) { + this.unsubscribeFromRoom(member.roomId); + } else if (removedSpaceSubscription || removedSidebarSubscription) { + this.queueRoomSubscriptionSync(); + } }; - this.onCacheRoomData = (roomId, data) => this.sidebarCache.cacheRoom(roomId, data); + this.onCacheRoomData = (roomId, data) => { + this.sidebarCache.cacheRoom(roomId, data); + + if (!this.initialListHydrationCompleted || this.hydratingSidebarCache) return; + + let subscriptionsChanged = false; + const completedSidebarHydration = this.sidebarRoomSubscriptions.delete(roomId); + if (completedSidebarHydration) subscriptionsChanged = true; + + if ( + data.initial === true && + !completedSidebarHydration && + !this.activeRoomSubscriptions.has(roomId) + ) { + this.sidebarRoomSubscriptions.add(roomId); + subscriptionsChanged = true; + } + + if (subscriptionsChanged) this.queueRoomSubscriptionSync(); + }; this.onCacheAccountData = (event) => this.sidebarCache.cacheAccountData(event); for (const roomId of options.initialRoomIds ?? []) { @@ -623,6 +673,7 @@ export class SlidingSyncManager { private expandListsByPage(): void { if (!this.initialSyncCompleted) return; + if (this.initialListHydrationCompleted) return; let allListsComplete = true; let expandedAny = false; @@ -711,13 +762,14 @@ export class SlidingSyncManager { if (allListsComplete) { if (!this.listsFullyLoaded) { this.listsFullyLoaded = true; + this.initialListHydrationCompleted = true; this.reconcileSidebarCacheMembership(); globalThis.setTimeout(() => this.flushDeferredSubscriptions(), 0); + this.applySteadyStateListRanges(); log.log(`Sliding Sync all lists fully loaded for ${this.mx.getUserId()}`); - const totalRooms = this.listKeys.reduce( - (sum, key) => sum + (this.slidingSync.getListData(key)?.joinedCount ?? 0), - 0 - ); + const totalRooms = + (this.slidingSync.getListData(LIST_JOINED)?.joinedCount ?? 0) + + (this.slidingSync.getListData(LIST_INVITES)?.joinedCount ?? 0); const listsLoadedMs = this.attachTime != null ? Math.round(performance.now() - this.attachTime) : 0; Sentry.metrics.distribution('sable.sync.lists_loaded_ms', listsLoadedMs, { @@ -750,6 +802,34 @@ export class SlidingSyncManager { } } + private applySteadyStateListRanges(): void { + const joinedList = this.slidingSync.getListParams(LIST_JOINED); + const currentEnd = getListEndIndex(joinedList); + const steadyStateEnd = Math.min(currentEnd, STEADY_STATE_DETAILED_ROOMS - 1); + if (steadyStateEnd < 0 || steadyStateEnd === currentEnd) return; + + const joinedCount = this.slidingSync.getListData(LIST_JOINED)?.joinedCount ?? 0; + const updatesCount = this.slidingSync.getListData(LIST_UPDATES)?.joinedCount ?? 0; + const updatesConfirmedEnd = this.confirmedListRangeEnds.get(LIST_UPDATES) ?? -1; + if (updatesCount !== joinedCount || updatesConfirmedEnd < joinedCount - 1) { + debugLog.warn('sync', 'Kept detailed joined list fully covered: updates list unavailable', { + joinedCount, + updatesCount, + updatesConfirmedEnd, + }); + return; + } + + this.slidingSync.setListRanges(LIST_JOINED, [[0, steadyStateEnd]]); + this.requestedListRangeEnds.set(LIST_JOINED, steadyStateEnd); + debugLog.info('sync', 'Reduced detailed joined list to steady-state window', { + previousEnd: currentEnd, + newEnd: steadyStateEnd, + retainedDetailedRooms: steadyStateEnd + 1, + updatesCoverageEnd: updatesConfirmedEnd, + }); + } + public ensureListRegistered(listKey: string, updateArgs: PartialSlidingSyncRequest): MSC3575List { let list = this.slidingSync.getListParams(listKey); if (!list) { @@ -826,7 +906,13 @@ export class SlidingSyncManager { if (membership === KnownMembership.Leave) { this.sidebarCache.removeRoom(roomId); - this.unsubscribeFromRoom(roomId); + const removedSpaceSubscription = this.spaceSubscriptions.delete(roomId); + const removedSidebarSubscription = this.sidebarRoomSubscriptions.delete(roomId); + if (this.activeRoomSubscriptions.has(roomId)) { + this.unsubscribeFromRoom(roomId); + } else if (removedSpaceSubscription || removedSidebarSubscription) { + this.queueRoomSubscriptionSync(); + } } } @@ -838,9 +924,19 @@ export class SlidingSyncManager { this.setSpaceSubscriptions(spaces); } + private queueRoomSubscriptionSync(): void { + if (this.disposed || this.roomSubscriptionSyncQueued) return; + this.roomSubscriptionSyncQueued = true; + globalThis.queueMicrotask(() => { + this.roomSubscriptionSyncQueued = false; + if (!this.disposed) this.syncRoomSubscriptions(); + }); + } + private syncRoomSubscriptions(): void { const desiredSubscriptions = new Set([ ...this.activeRoomSubscriptions, + ...this.sidebarRoomSubscriptions, ...this.spaceSubscriptions, ...this.imagePackRoomSubscriptions, ]); @@ -848,6 +944,8 @@ export class SlidingSyncManager { desiredSubscriptions.forEach((roomId) => { if (this.activeRoomSubscriptions.has(roomId)) { this.slidingSync.useCustomSubscription(roomId, ACTIVE_ROOM_SUBSCRIPTION_KEY); + } else if (this.sidebarRoomSubscriptions.has(roomId)) { + this.slidingSync.useCustomSubscription(roomId, SIDEBAR_ROOM_SUBSCRIPTION_KEY); } else if (this.spaceSubscriptions.has(roomId)) { this.slidingSync.useCustomSubscription(roomId, SPACE_SUBSCRIPTION_KEY); } else { @@ -919,17 +1017,12 @@ export class SlidingSyncManager { this.roomSubscriptionStatusListeners.get(roomId)?.forEach((listener) => listener(loading)); } - public subscribeToRoom(roomId: string): void { - if (this.disposed) return; - if (this.activeRoomSubscriptions.has(roomId)) return; + private addActiveRoomSubscription(roomId: string): boolean { + if (this.activeRoomSubscriptions.has(roomId)) return false; const room = this.mx.getRoom(roomId); const isEncrypted = this.mx.isRoomEncrypted(roomId); this.activeRoomSubscriptions.add(roomId); this.pauseUnconfirmedListExpansion(); - this.syncRoomSubscriptions(); - Sentry.metrics.gauge('sable.sync.active_subscriptions', this.activeRoomSubscriptions.size, { - attributes: { transport: 'sliding' }, - }); log.log(`Sliding Sync active room subscription added: ${roomId}`); debugLog.info('sync', 'Room subscription requested (sliding)', { encrypted: isEncrypted, @@ -981,11 +1074,11 @@ export class SlidingSyncManager { this.pendingRoomDataListeners.set(roomId, onFirstRoomData); this.slidingSync.on(SlidingSyncEvent.RoomData, onFirstRoomData); this.notifyRoomSubscriptionStatus(roomId, true); + return true; } - public unsubscribeFromRoom(roomId: string): void { - if (this.disposed) return; - if (!this.activeRoomSubscriptions.has(roomId)) return; + private removeActiveRoomSubscription(roomId: string): boolean { + if (!this.activeRoomSubscriptions.has(roomId)) return false; const pendingListener = this.pendingRoomDataListeners.get(roomId); if (pendingListener) { this.slidingSync.removeListener(SlidingSyncEvent.RoomData, pendingListener); @@ -994,15 +1087,49 @@ export class SlidingSyncManager { this.roomDataAwaitingSyncCompletion.delete(roomId); this.notifyRoomSubscriptionStatus(roomId, false); this.activeRoomSubscriptions.delete(roomId); - this.syncRoomSubscriptions(); - Sentry.metrics.gauge('sable.sync.active_subscriptions', this.activeRoomSubscriptions.size, { - attributes: { transport: 'sliding' }, - }); log.log(`Sliding Sync active room subscription removed: ${roomId}`); debugLog.info('sync', 'Room subscription removed (sliding)', { remainingSubscriptions: this.activeRoomSubscriptions.size, syncCycle: this.syncCount, }); + return true; + } + + private reportActiveSubscriptionCount(): void { + Sentry.metrics.gauge('sable.sync.active_subscriptions', this.activeRoomSubscriptions.size, { + attributes: { transport: 'sliding' }, + }); + } + + public setActiveRoomSubscriptions(roomIds: Iterable): void { + if (this.disposed) return; + const next = new Set(roomIds); + let changed = false; + + this.activeRoomSubscriptions.forEach((roomId) => { + if (!next.has(roomId)) changed = this.removeActiveRoomSubscription(roomId) || changed; + }); + next.forEach((roomId) => { + if (!this.activeRoomSubscriptions.has(roomId)) { + changed = this.addActiveRoomSubscription(roomId) || changed; + } + }); + + if (!changed) return; + this.syncRoomSubscriptions(); + this.reportActiveSubscriptionCount(); + } + + public subscribeToRoom(roomId: string): void { + if (this.disposed || !this.addActiveRoomSubscription(roomId)) return; + this.syncRoomSubscriptions(); + this.reportActiveSubscriptionCount(); + } + + public unsubscribeFromRoom(roomId: string): void { + if (this.disposed || !this.removeActiveRoomSubscription(roomId)) return; + this.syncRoomSubscriptions(); + this.reportActiveSubscriptionCount(); } public setSpaceSubscriptions(roomIds: Iterable): void {