From a759ad2e2b9e7bb2da76fe0e44df429050be6ef9 Mon Sep 17 00:00:00 2001 From: jm-clius Date: Thu, 23 Oct 2025 14:55:05 +0100 Subject: [PATCH] fix: buffer optimisation, backwards compatible senderId --- packages/sds/src/index.ts | 6 ++++ .../sds/src/message_channel/repair/buffers.ts | 29 +++++++++++-------- 2 files changed, 23 insertions(+), 12 deletions(-) diff --git a/packages/sds/src/index.ts b/packages/sds/src/index.ts index 8841e99477..d228616bde 100644 --- a/packages/sds/src/index.ts +++ b/packages/sds/src/index.ts @@ -18,4 +18,10 @@ export { type MessageId } from "./message_channel/index.js"; +/** + * @deprecated Use ParticipantId instead. SenderId has been renamed to ParticipantId + * to better reflect that it represents a channel participant, not just a message sender. + */ +export type SenderId = import("./message_channel/index.js").ParticipantId; + export { BloomFilter }; diff --git a/packages/sds/src/message_channel/repair/buffers.ts b/packages/sds/src/message_channel/repair/buffers.ts index 9d48c61baf..6f014c257e 100644 --- a/packages/sds/src/message_channel/repair/buffers.ts +++ b/packages/sds/src/message_channel/repair/buffers.ts @@ -212,21 +212,26 @@ export class IncomingRepairBuffer { * Removes and returns ready entries */ public getReady(currentTime: number): HistoryEntry[] { - const ready: HistoryEntry[] = []; - const remaining: IncomingBufferEntry[] = []; - - for (const item of this.items) { - if (item.tResp <= currentTime) { - ready.push(item.entry); - log.info(`Repair for ${item.entry.messageId} is ready to be sent`); - } else { - // Since array is sorted, all remaining entries are not ready - remaining.push(item); + // Find cutoff point - first item with tResp > currentTime + // Since array is sorted, all items before this are ready + let cutoff = 0; + for (let i = 0; i < this.items.length; i++) { + if (this.items[i].tResp > currentTime) { + cutoff = i; + break; } + // If we reach the end, all items are ready + cutoff = i + 1; } - // Keep only non-ready entries - this.items = remaining; + // Extract ready items and log them + const ready = this.items.slice(0, cutoff).map((item) => { + log.info(`Repair for ${item.entry.messageId} is ready to be sent`); + return item.entry; + }); + + // Keep only items after cutoff + this.items = this.items.slice(cutoff); return ready; }