Skip to content

armgit / orca

Public Imported from GitHub
0

Orca is the place for working with a fleet of agents (e.g. Grok Bot, Claude Code). Run any coding agent with your own subscription. Available on desktop, mobile and remote runtime.

perf(mobile): batch iOS push-dismissal ledger writes per page (STA-7366) (#36145)

Neil5121f25a5a8a
diff --git a/mobile/modules/orca-notification-dismissal/ios/OrcaNotificationDismissalModule.swift b/mobile/modules/orca-notification-dismissal/ios/OrcaNotificationDismissalModule.swift
index f86fe8bf891..a0b274d7e75 100644
--- a/mobile/modules/orca-notification-dismissal/ios/OrcaNotificationDismissalModule.swift
+++ b/mobile/modules/orca-notification-dismissal/ios/OrcaNotificationDismissalModule.swift
@@ -3,8 +3,8 @@ import ExpoModulesCore
public class OrcaNotificationDismissalModule: Module {
public func definition() -> ModuleDefinition {
Name("OrcaNotificationDismissal")
- AsyncFunction("remember") { (payload: [String: Any]) in
- if let identity = PushDismissalIdentity(payload) { PushDismissalLedger.shared.remember(identity) }
+ AsyncFunction("rememberAll") { (payloads: [[String: Any]]) in
+ PushDismissalLedger.shared.remember(payloads.compactMap(PushDismissalIdentity.init))
}
AsyncFunction("wasDismissed") { (payload: [String: Any]) -> Bool in
guard let identity = PushDismissalIdentity(payload) else { return false }
diff --git a/mobile/modules/orca-notification-dismissal/ios/PushDismissalLedger.swift b/mobile/modules/orca-notification-dismissal/ios/PushDismissalLedger.swift
index a147b09d599..37a9e93fd72 100644
--- a/mobile/modules/orca-notification-dismissal/ios/PushDismissalLedger.swift
+++ b/mobile/modules/orca-notification-dismissal/ios/PushDismissalLedger.swift
@@ -19,10 +19,10 @@ struct PushDismissalIdentity: Codable {
notificationSeq = seq.int64Value
}
- func matches(_ other: PushDismissalIdentity) -> Bool {
- hostFingerprint == other.hostFingerprint && notificationId == other.notificationId &&
- notificationEpoch == other.notificationEpoch
- }
+ struct Key: Hashable { let host: String; let id: String; let epoch: String }
+ var key: Key { Key(host: hostFingerprint, id: notificationId, epoch: notificationEpoch) }
+
+ func matches(_ other: PushDismissalIdentity) -> Bool { key == other.key }
}
final class PushDismissalLedger {
@@ -40,14 +40,30 @@ final class PushDismissalLedger {
}
func remember(_ identity: PushDismissalIdentity, now: TimeInterval = Date().timeIntervalSince1970) {
+ remember([identity], now: now)
+ }
+
+ // One read/encode/write per batch keeps catch-up pages linear in ledger size.
+ func remember(_ identities: [PushDismissalIdentity], now: TimeInterval = Date().timeIntervalSince1970) {
+ guard !identities.isEmpty else { return }
lock.lock(); defer { lock.unlock() }
let entries = read(now: now)
- let previous = entries.first { $0.identity.matches(identity) }
- let newest = (previous?.identity.notificationSeq ?? -1) > identity.notificationSeq
- ? previous!.identity : identity
+ var order: [PushDismissalIdentity.Key] = []
+ var newest: [PushDismissalIdentity.Key: PushDismissalIdentity] = [:]
+ for identity in identities {
+ if let current = newest[identity.key] {
+ if current.notificationSeq <= identity.notificationSeq { newest[identity.key] = identity }
+ } else {
+ order.append(identity.key); newest[identity.key] = identity
+ }
+ }
+ var kept: [Entry] = []
+ for entry in entries {
+ guard let batch = newest[entry.identity.key] else { kept.append(entry); continue }
+ if entry.identity.notificationSeq > batch.notificationSeq { newest[entry.identity.key] = entry.identity }
+ }
// Keep every live fence: count-based eviction lets delayed alerts reappear.
- let next = entries.filter { !$0.identity.matches(identity) } +
- [Entry(identity: newest, expiresAt: now + 86400)]
+ let next = kept + order.compactMap { key in newest[key].map { Entry(identity: $0, expiresAt: now + 86400) } }
if let data = try? JSONEncoder().encode(next) {
defaults.set(data, forKey: storageKey)
}
diff --git a/mobile/modules/orca-notification-dismissal/tests/PushDismissalLedgerChecks.swift b/mobile/modules/orca-notification-dismissal/tests/PushDismissalLedgerChecks.swift
index 04e1809ea29..9f6f6578e24 100644
--- a/mobile/modules/orca-notification-dismissal/tests/PushDismissalLedgerChecks.swift
+++ b/mobile/modules/orca-notification-dismissal/tests/PushDismissalLedgerChecks.swift
@@ -39,6 +39,18 @@ import Foundation
precondition(!reopened.contains(identity(2, "host-\(index % hosts)", "epoch-\(hosts)", "note-\(index)"), now: 86600))
}
}
+ let batch = PushDismissalLedger(defaults: defaults)
+ batch.remember(identity(5, "batch"), now: 300)
+ batch.remember([identity(4, "batch"), identity(7, "batch", "qa-epoch", "b"), identity(6, "batch", "qa-epoch", "b"), identity(1, "batch", "qa-epoch", "c")], now: 300)
+ batch.remember([PushDismissalIdentity](), now: 301)
+ let batchReopened = PushDismissalLedger(defaults: defaults)
+ precondition(batchReopened.contains(identity(5, "batch"), now: 301))
+ precondition(!batchReopened.contains(identity(6, "batch"), now: 301))
+ precondition(batchReopened.contains(identity(7, "batch", "qa-epoch", "b"), now: 301))
+ precondition(!batchReopened.contains(identity(8, "batch", "qa-epoch", "b"), now: 301))
+ precondition(batchReopened.contains(identity(1, "batch", "qa-epoch", "c"), now: 301))
+ precondition(batchReopened.contains(identity(5, "batch"), now: 86699))
+ precondition(!batchReopened.contains(identity(5, "batch"), now: 86700))
print("Native persisted fence: restart, ordering, identity isolation, expiry and invalid sequence checks passed")
}
}
diff --git a/mobile/src/notifications/native-push-dismissal.ts b/mobile/src/notifications/native-push-dismissal.ts
index a676c68e6dc..3f0e267ebfa 100644
--- a/mobile/src/notifications/native-push-dismissal.ts
+++ b/mobile/src/notifications/native-push-dismissal.ts
@@ -1,7 +1,7 @@
import type { OrcaPushPayload } from './push-payload'
export type NativeDismissal = {
- remember(payload: OrcaPushPayload): Promise<void>
+ rememberAll(payloads: readonly OrcaPushPayload[]): Promise<void>
wasDismissed(payload: OrcaPushPayload): Promise<boolean>
}
// Android and web use JavaScript storage; iOS requires the native ledger.
diff --git a/mobile/src/notifications/push-dismissal-native-races.test.ts b/mobile/src/notifications/push-dismissal-native-races.test.ts
index 3339e040d37..fd6e7f97aaa 100644
--- a/mobile/src/notifications/push-dismissal-native-races.test.ts
+++ b/mobile/src/notifications/push-dismissal-native-races.test.ts
@@ -13,13 +13,15 @@ const memory = vi.hoisted(() => new Map<string, string>())
const nativeLedger = vi.hoisted(() => new Map<string, number>())
vi.mock('./native-push-dismissal', () => ({
nativePushDismissal: {
- remember: vi.fn(async (payload) => {
- const key = JSON.stringify([
- payload.hostFingerprint,
- payload.notificationEpoch,
- payload.notificationId
- ])
- nativeLedger.set(key, Math.max(nativeLedger.get(key) ?? 0, payload.notificationSeq))
+ rememberAll: vi.fn(async (payloads) => {
+ for (const payload of payloads) {
+ const key = JSON.stringify([
+ payload.hostFingerprint,
+ payload.notificationEpoch,
+ payload.notificationId
+ ])
+ nativeLedger.set(key, Math.max(nativeLedger.get(key) ?? 0, payload.notificationSeq))
+ }
}),
wasDismissed: vi.fn(
async (payload) =>
@@ -83,6 +85,7 @@ it('keeps a batch in the native ledger and joins it before checking dismissal',
const pending = rememberPushDismissals([fence, other])
const secondDismissed = wasPushDismissed(other)
await pending
+ expect(nativePushDismissal!.rememberAll).toHaveBeenCalledTimes(1)
expect(await secondDismissed).toBe(true)
expect(await wasPushDismissed({ ...other, notificationSeq: 22 })).toBe(false)
expect(AsyncStorage.getItem).not.toHaveBeenCalled()
@@ -106,7 +109,7 @@ it('rechecks a negative native snapshot overtaken by a dismissal', async () => {
})
it('surfaces native write failures without switching storage or poisoning later operations', async () => {
- vi.mocked(nativePushDismissal!.remember).mockRejectedValueOnce(new Error('native failure'))
+ vi.mocked(nativePushDismissal!.rememberAll).mockRejectedValueOnce(new Error('native failure'))
await expect(rememberPushDismissal(fence)).rejects.toThrow('native failure')
expect(AsyncStorage.setItem).not.toHaveBeenCalled()
await rememberPushDismissal(fence)
diff --git a/mobile/src/notifications/push-dismissal-watermarks.ts b/mobile/src/notifications/push-dismissal-watermarks.ts
index 8dbb91e1eac..c2dc1952017 100644
--- a/mobile/src/notifications/push-dismissal-watermarks.ts
+++ b/mobile/src/notifications/push-dismissal-watermarks.ts
@@ -69,10 +69,7 @@ export async function rememberPushDismissals(payloads: readonly OrcaPushPayload[
}
return queueDismissalOperation(async () => {
if (nativePushDismissal) {
- for (const { payload } of valid) {
- await nativePushDismissal.remember(payload)
- }
- return
+ return nativePushDismissal.rememberAll(valid.map(({ payload }) => payload))
}
const entries = new Map((await readEntries()).map((entry) => [entry.key, entry]))
const expiresAt = Date.now() + RETENTION_MS