Move most of RelayPool away from the Main Thread

This is a large refactor that aims to improve performance by offloading
RelayPool computations into a separate actor outside the main thread.

This should reduce congestion on the main thread and thus improve UI
performance.

Also, the internal subscription callback mechanism was changed to use
AsyncStreams to prevent race conditions newly found in that area of the
code.

Changelog-Fixed: Added performance improvements to timeline scrolling
Signed-off-by: Daniel D’Aquino <daniel@daquino.me>
This commit is contained in:
Daniel D’Aquino
2025-10-10 14:12:30 -07:00
parent 7c1594107f
commit 991a4a86e6
50 changed files with 602 additions and 451 deletions

View File

@@ -310,7 +310,10 @@ public func nscript_nostr_cmd(interp: UnsafeMutablePointer<wasm_interp>?, cmd: I
func nscript_add_relay(script: NostrScript, relay: String) -> Bool {
guard let url = RelayURL(relay) else { return false }
let desc = RelayPool.RelayDescriptor(url: url, info: .readWrite, variant: .ephemeral)
return (try? script.pool.add_relay(desc)) != nil
// Interacting with RelayPool needs to be done asynchronously, thus we cannot return the answer synchronously
// return (try? await script.pool.add_relay(desc)) != nil
Task { try await script.pool.add_relay(desc) }
return true
}
@@ -344,9 +347,7 @@ public func nscript_pool_send_to(interp: UnsafeMutablePointer<wasm_interp>?, pre
return 0
}
DispatchQueue.main.async {
script.pool.send_raw(.custom(req_str), to: [to_relay_url], skip_ephemeral: false)
}
Task { await script.pool.send_raw(.custom(req_str), to: [to_relay_url], skip_ephemeral: false) }
return 1;
}
@@ -354,9 +355,7 @@ public func nscript_pool_send_to(interp: UnsafeMutablePointer<wasm_interp>?, pre
func nscript_pool_send(script: NostrScript, req req_str: String) -> Int32 {
//script.test("pool_send: '\(req_str)'")
DispatchQueue.main.sync {
script.pool.send_raw(.custom(req_str), skip_ephemeral: false)
}
Task { await script.pool.send_raw(.custom(req_str), skip_ephemeral: false) }
return 1;
}