Skip to content

Route outbound messages to their compilation by ID - #460

Open
mattlewis92 wants to merge 1 commit into
sass:mainfrom
mattlewis92:perf/route-outbound-messages
Open

mattlewis92 wants to merge 1 commit into
sass:mainfrom
mattlewis92:perf/route-outbound-messages

Conversation

@mattlewis92

Copy link
Copy Markdown

Every Dispatcher subscribes to the compiler's shared outbound message stream and filters it for its own compilation ID. Each message therefore runs once per in-flight compilation, so N concurrent compileStringAsync() calls cost O(N²) on the main thread. Bundlers start one compilation per stylesheet, often thousands at once.

MessageTransformer now routes each message to its compilation's subscriber with a Map lookup, and Dispatcher receives only its own compilation's messages. When the stream errors or completes, every compilation's subscription errors or completes, and so does any subscription made after that, as with the shared stream before.

Benchmark

N concurrent compileStringAsync() calls, each @use-ing 6 partials through an async custom importer. Both builds use the same 1.108.0 compiler, and the output is identical.

N main this PR
1,000 0.47 s 0.33 s
2,000 1.38 s 0.63 s
4,000 4.45 s 1.26 s
8,000 19.94 s 2.57 s

With at most 16 compilations in flight, both take about 2.1 s for 7,300 files.

Benchmark script
// node bench.mjs <path to dist/lib/index.js> <files>
import {pathToFileURL} from 'node:url';

const [entry, files] = process.argv.slice(2);
const partial = i => `
$color-${i}: hsl(${i * 30}, 50%, 50%);
$spacing-${i}: ${i + 1}px;
@function scale-${i}($n) { @return $n * $spacing-${i}; }
@mixin box-${i}($size) {
  padding: scale-${i}($size);
  margin: scale-${i}($size * 2);
  border: 1px solid $color-${i};
  &:hover { background: rgba($color-${i}, 0.5); }
}`;
const file = n => {
  const uses = Array.from({length: 6}, (_, k) => (n + k) % 10);
  return (
    uses.map(p => `@use 'partial-${p}' as p${p};`).join('\n') +
    uses
      .map(
        p => `.c${n}-${p} { @include p${p}.box-${p}(${(n % 7) + 1});
  .inner { color: p${p}.$color-${p}; width: p${p}.scale-${p}(10); } }`,
      )
      .join('\n')
  );
};
const importer = {
  async canonicalize(url) {
    return url.startsWith('partial-') ? new URL(`bench:${url}`) : null;
  },
  async load(url) {
    return {contents: partial(Number(url.pathname.split('-')[1])), syntax: 'scss'};
  },
};

const sass = await import(pathToFileURL(entry).href);
const compiler = await sass.initAsyncCompiler();
await compiler.compileStringAsync(file(0), {importers: [importer]});

const start = performance.now();
await Promise.all(
  Array.from({length: Number(files)}, (_, n) =>
    compiler.compileStringAsync(file(n), {importers: [importer]}),
  ),
);
console.log(`${((performance.now() - start) / 1000).toFixed(2)}s`);
await compiler.dispose();

Testing

  • npm test: new tests in message-transformer.test.ts for routing, reused compilation IDs, and errors and completion reaching every compilation.
  • npm run check and node test/after-compile-test.mjs pass.
  • I haven't run sass-spec's js-api-spec locally.

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.
@nex3

nex3 commented Nov 9, 2026

Copy link
Copy Markdown
Contributor

The principle seems sound. Just to confirm per our contribution guidelines, this PR was made without the use of LLMs?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants