perf: Map/Set/WeakMap store code is ~2.7x slower than Node (string-keyed get/set + new Map(map) phase 3.7x)

オープン
#14,978 コメント 0 件 リアクション 0 件 担当者 0 名 GitHub で見る

メンテナーはふだん 1 日以内に返信

まだ誰も着手していません。

評価

難易度
4/5
見積もり時間
3〜5日
初心者へのやさしさ
45/100
issue の種類
バグ
明瞭さ
おおむね明確
活発さ
活発
技術スタック
node.js, rust, typescript

調査の方向性

Start by reproducing the benchmark in bench.ts with the listed Perry and Node commands, then inspect how Perry implements Map, Set, and WeakMap operations and new Map(map). Focus on the events phase, which accounts for most of the slowdown. Done means improving the reported timings while preserving the matching result digest; the issue does not identify the implementation files or tests.

索引モデルが issue の本文から書いたものです。

説明

area:runtime-gc effort:L impact:med type:perf

Summary

Map/Set-heavy store code is about 2.7x slower than Node: entities in Maps keyed by string ids, copy-on-write entries with an occasional new Map(map) copy, and a WeakMap memo. The worst phase is events (200 K string-keyed get/set, WeakMap get/set, every 256th event a 2,000-entry new Map(map)): 403 ms vs 108 ms (3.7x). Deriving Sets (for...of over a Map, Set.add/delete) takes 46 vs 11 ms with facts on, and 69 ms with facts off. Related: #10697 (string-keyed Map).

Reproduction

No dependencies.

bench.ts:

// map-set-store: Map/Set-heavy client store code: entities in Maps keyed by string id, copy-on-write entries,
// an occasional `new Map(map)` copy, a WeakMap memo, and derived Sets recomputed per round.
const now = () => performance.now();
const phases: Record<string, number> = {};
function phase(name: string, t0: number): void { phases[name] = Math.round((now() - t0) * 10) / 10; }
function hashNum(n: number, h0?: number): number {
  const s = String(n);
  let h = h0 === undefined ? 0x811c9dc5 : h0;
  for (let i = 0; i < s.length; i++) { h ^= s.charCodeAt(i); h = Math.imul(h, 0x01000193); }
  return h >>> 0;
}
function rng(seed: number): () => number { // mulberry32: same data on every engine
  let a = seed >>> 0;
  return function () {
    a = (a + 0x6d2b79f5) >>> 0;
    let t = a;
    t = Math.imul(t ^ (t >>> 15), t | 1);
    t ^= t + Math.imul(t ^ (t >>> 7), t | 61);
    return ((t ^ (t >>> 14)) >>> 0) / 4294967296;
  };
}
function done(bench: string, check: number): void { console.log("BENCH " + JSON.stringify({ bench: bench, phases: phases, check: String(check) })); }

interface Thread { id: string; projectId: string; messageIds: string[]; updatedAt: number }

function main() {
  const r = rng(3);
  const S = 1;
  const NT = Math.floor(2000 * S);
  let threads = new Map<string, Thread>();
  const messages = new Map<string, { id: string; threadId: string; len: number }>();
  let t = now();
  for (let i = 0; i < NT; i++) {
    const id = "thread-" + i;
    const ids: string[] = [];
    for (let k = 0; k < 20; k++) {
      const mid = id + ":m" + k;
      ids.push(mid);
      messages.set(mid, { id: mid, threadId: id, len: Math.floor(r() * 500) });
    }
    threads.set(id, { id: id, projectId: "p" + (i % 17), messageIds: ids, updatedAt: i });
  }
  phase("build", t);
  t = now();
  let acc = 0;
  const memo = new WeakMap<Thread, number>();
  for (let ev = 0; ev < Math.floor(200000 * S); ev++) {
    const id = "thread-" + Math.floor(r() * NT);
    const th = threads.get(id)!;
    if ((ev & 3) === 0) {
      // structural sharing: copy-on-write of the map entry (and every 64th event, the whole map)
      const next: Thread = { id: th.id, projectId: th.projectId, messageIds: th.messageIds, updatedAt: th.updatedAt + 1 };
      if ((ev & 255) === 0) threads = new Map(threads);
      threads.set(id, next);
    }
    let m = memo.get(th);
    if (m === undefined) {
      m = 0;
      for (const mid of th.messageIds) m += messages.get(mid)!.len;
      memo.set(th, m);
    }
    acc = (acc + m) | 0;
  }
  phase("events", t);
  t = now();
  for (let round = 0; round < Math.floor(120 * S); round++) {
    const visible = new Set<string>();
    const byProject = new Map<string, number>();
    for (const [id, th] of threads) {
      if ((th.updatedAt + round) % 3 !== 0) visible.add(id);
      byProject.set(th.projectId, (byProject.get(th.projectId) ?? 0) + 1);
    }
    let removed = 0;
    for (const id of visible) if (id.length % 2 === 0 && visible.delete(id)) removed++;
    acc = (acc + visible.size + removed + byProject.size) | 0;
  }
  phase("derive_sets", t);
  done("map-set-store", hashNum(acc));
}
main();
export PERRY_LL_OPT_LEVEL=2 PERRY_TARGET_CPU=znver4 PERRY_NO_AUTO_OPTIMIZE=1
perry compile bench.ts -o bench && ./bench
NODE_ENV=production node bench.ts

Both print one BENCH {...} line with the phase times and a result digest. The digests agree between Perry and Node.

Results

Engine wall ms median (min-max) Perry/Node phases ms
Perry 08c74942f1 (default, facts on) 462 (440-496) 2.67x build 7.5, events 402.6, derive_sets 46.0
Perry 08c74942f1, PERRY_PROGRAM_FACTS=0 489 (471-527) 2.83x build 7.8, events 406.6, derive_sets 68.8
Node v26.12.1 173 (167-179) 1 build 14.2, events 107.5, derive_sets 11.4

Environment

  • Perry: PerryTS/perry main 08c74942f1, pristine, no patches. Linux host build: cargo build --release -p perry --no-default-features --features dev-cli plus -p perry-runtime-static -p perry-stdlib-static -p perry-wasm-host (and the perry-ext-* archives).
  • Node v26.12.1.
  • Linux x86_64, AMD EPYC (Zen 5), 32 threads, Ubuntu 24.04. A shared box (load 15-35 during the run): runs are pinned to 8 cores with taskset -c 8-15 under nice, and engines are interleaved run by run, so the ratios hold better than the absolute times.
  • Method: whole-process wall time, 1 warm-up and then 10 timed runs per engine; median (min-max). Phases are the program's own performance.now() deltas (medians).
主要言語
Rust
スター
4.9k
フォーク
166
平均マージ
4時間 54分
マージ済み PR(30日)
909

環境構築

はじめの一歩

  1. issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
  2. 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
  3. リポジトリをフォークし、ブランチを切って変更します。
  4. issue 番号を参照したプルリクエストを送ります。

PerryTS/perry のほかの issue

PerryTS/perry の issue をすべて見る

似ている issue

Rust の issue をもっと見る

新しい issue をメールで受け取る

初心者向けの GitHub issue を短くまとめたダイジェスト。