From a81fdda7f2a440b7e9edcb4c2f9dda2601e2eaa9 Mon Sep 17 00:00:00 2001 From: Matt Lewis Date: Fri, 6 Nov 2026 18:30:06 +0000 Subject: [PATCH] Route outbound messages to their compilation by ID Each Dispatcher filtered the shared outbound message stream for its own compilation ID, so every message cost O(in-flight compilations) and a batch of N concurrent compilations cost O(N^2). MessageTransformer now routes each message to its compilation's subscriber with a Map lookup. --- lib/src/compiler/utils.ts | 2 +- lib/src/dispatcher.ts | 6 +- lib/src/message-transformer.test.ts | 95 ++++++++++++++++++++++++++++- lib/src/message-transformer.ts | 50 ++++++++++++++- 4 files changed, 145 insertions(+), 8 deletions(-) diff --git a/lib/src/compiler/utils.ts b/lib/src/compiler/utils.ts index be82c740..56385670 100644 --- a/lib/src/compiler/utils.ts +++ b/lib/src/compiler/utils.ts @@ -60,7 +60,7 @@ export function createDispatcher( ): Dispatcher { return new Dispatcher( compilationId, - messageTransformer.outboundMessages$, + messageTransformer.outboundMessagesFor(compilationId), message => messageTransformer.writeInboundMessage(message), handlers, ); diff --git a/lib/src/dispatcher.ts b/lib/src/dispatcher.ts index 5bfc95db..9b8c526c 100644 --- a/lib/src/dispatcher.ts +++ b/lib/src/dispatcher.ts @@ -73,9 +73,7 @@ export class Dispatcher { constructor( private readonly compilationId: number, - private readonly outboundMessages$: Observable< - [number, proto.OutboundMessage] - >, + private readonly outboundMessages$: Observable, private readonly writeInboundMessage: ( message: [number, proto.InboundMessage], ) => void, @@ -87,8 +85,6 @@ export class Dispatcher { this.outboundMessages$ .pipe( - filter(([compilationId]) => compilationId === this.compilationId), - map(([, message]) => message), mergeMap(message => { const result = this.handleOutboundMessage(message); return result instanceof Promise diff --git a/lib/src/message-transformer.test.ts b/lib/src/message-transformer.test.ts index d6b84fc8..837322da 100644 --- a/lib/src/message-transformer.test.ts +++ b/lib/src/message-transformer.test.ts @@ -2,7 +2,7 @@ // MIT-style license that can be found in the LICENSE file or at // https://opensource.org/licenses/MIT. -import {Observable, Subject} from 'rxjs'; +import {Observable, Subject, Subscription} from 'rxjs'; import * as varint from 'varint'; import {create, toBinary} from '@bufbuild/protobuf'; @@ -107,4 +107,97 @@ describe('message transformer', () => { }); }); }); + + describe('outboundMessagesFor', () => { + let protobufs$: Subject; + + beforeEach(() => { + protobufs$ = new Subject(); + messages = new MessageTransformer(protobufs$, () => {}); + }); + + function sendLogEvent(compilationId: number, text: string): void { + protobufs$.next( + Uint8Array.from([ + ...varint.encode(compilationId), + ...toBinary( + proto.OutboundMessageSchema, + create(proto.OutboundMessageSchema, { + message: {case: 'logEvent', value: {message: text}}, + }), + ), + ]), + ); + } + + function collect(compilationId: number): { + texts: string[]; + subscription: Subscription; + } { + const texts: string[] = []; + const subscription = messages + .outboundMessagesFor(compilationId) + .subscribe(message => + texts.push( + (message.message.value as proto.OutboundMessage_LogEvent).message, + ), + ); + return {texts, subscription}; + } + + it("emits only the compilation's messages", () => { + const first = collect(1); + const second = collect(2); + + sendLogEvent(1, 'a'); + sendLogEvent(2, 'b'); + sendLogEvent(3, 'c'); + sendLogEvent(1, 'd'); + + expect(first.texts).toEqual(['a', 'd']); + expect(second.texts).toEqual(['b']); + }); + + it('routes a reused compilation ID to its new subscriber', () => { + const previous = collect(1); + sendLogEvent(1, 'a'); + previous.subscription.unsubscribe(); + + const next = collect(1); + sendLogEvent(1, 'b'); + + expect(previous.texts).toEqual(['a']); + expect(next.texts).toEqual(['b']); + }); + + it('errors every compilation when a message fails to decode', () => { + const errors: unknown[] = []; + for (const compilationId of [1, 2]) { + messages + .outboundMessagesFor(compilationId) + .subscribe({error: error => errors.push(error)}); + } + + protobufs$.next(Buffer.from([-1])); + messages + .outboundMessagesFor(3) + .subscribe({error: error => errors.push(error)}); + + expect(errors).toHaveLength(3); + }); + + it('completes every compilation when the compiler exits', () => { + let completed = 0; + for (const compilationId of [1, 2]) { + messages + .outboundMessagesFor(compilationId) + .subscribe({complete: () => completed++}); + } + + protobufs$.complete(); + messages.outboundMessagesFor(3).subscribe({complete: () => completed++}); + + expect(completed).toBe(3); + }); + }); }); diff --git a/lib/src/message-transformer.ts b/lib/src/message-transformer.ts index a33af0bd..93ee5c20 100644 --- a/lib/src/message-transformer.ts +++ b/lib/src/message-transformer.ts @@ -2,7 +2,7 @@ // MIT-style license that can be found in the LICENSE file or at // https://opensource.org/licenses/MIT. -import {Observable, Subject} from 'rxjs'; +import {Observable, Subject, Subscriber} from 'rxjs'; import {map} from 'rxjs/operators'; import {fromBinary, toBinary} from '@bufbuild/protobuf'; import * as varint from 'varint'; @@ -32,15 +32,63 @@ export class MessageTransformer { */ readonly outboundMessages$ = this.outboundMessagesInternal$.pipe(); + // The subscriber to each compilation's OutboundMessages, by compilation ID. + // Routing by ID keeps the cost of each message independent of the number of + // compilations in flight. + private readonly compilationSubscribers = new Map< + number, + Subscriber + >(); + + // Set once `outboundMessages$` errors or completes, to end subscriptions + // made after that the same way. + private endSubscriber?: (subscriber: Subscriber) => void; + constructor( private readonly outboundProtobufs$: Observable, private readonly writeInboundProtobuf: (buffer: Uint8Array) => void, ) { + this.outboundMessagesInternal$.subscribe({ + next: ([compilationId, message]) => + this.compilationSubscribers.get(compilationId)?.next(message), + error: error => this.end(subscriber => subscriber.error(error)), + complete: () => this.end(subscriber => subscriber.complete()), + }); + this.outboundProtobufs$ .pipe(map(decode)) .subscribe(this.outboundMessagesInternal$); } + /** + * The OutboundMessages for `compilationId`. Errors or completes when + * `outboundMessages$` does. Each compilation ID supports one subscriber at a + * time. + */ + outboundMessagesFor(compilationId: number): Observable { + return new Observable(subscriber => { + if (this.endSubscriber) { + this.endSubscriber(subscriber); + return; + } + this.compilationSubscribers.set(compilationId, subscriber); + return () => { + if (this.compilationSubscribers.get(compilationId) === subscriber) { + this.compilationSubscribers.delete(compilationId); + } + }; + }); + } + + private end( + endSubscriber: (subscriber: Subscriber) => void, + ): void { + this.endSubscriber = endSubscriber; + for (const subscriber of [...this.compilationSubscribers.values()]) { + endSubscriber(subscriber); + } + } + /** * Converts the inbound `compilationId` and `message` to a protocol buffer. */