perf: Map/Set/WeakMap store code is ~2.7x slower than Node (string-keyed get/set + new Map(map) phase 3.7x)
メンテナーはふだん 1 日以内に返信
まだ誰も着手していません。
評価
- 難易度
- 4/5
- 見積もり時間
- 3〜5日
- 初心者へのやさしさ
- 45/100
- issue の種類
- バグ
- 明瞭さ
- おおむね明確
- 活発さ
- 活発
- 技術スタック
- node.js, rust, typescript
- 領域
- backend, performance
調査の方向性
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 の本文から書いたものです。
説明
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-cliplus-p perry-runtime-static -p perry-stdlib-static -p perry-wasm-host(and theperry-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-15undernice, 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
環境構築
- Dockerfile または Docker Compose ファイルあり
- プルリクエストのテンプレートあり
- コントリビューションガイドを読む
はじめの一歩
- issue を最後まで読み、次にプロジェクトのコントリビューションガイドを読みます。
- 着手することを issue にコメントします — 二人が同じ作業をするのを防げます。
- リポジトリをフォークし、ブランチを切って変更します。
- issue 番号を参照したプルリクエストを送ります。
PerryTS/perry のほかの issue
-
area:compiler effort:S impact:med platform:windows type:bug
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
メンテナーはふだん 1 日以内に返信
-
area:compiler effort:S impact:med type:bug
難易度 2/5 1〜3時間 初心者へのやさしさ 78/100
メンテナーはふだん 1 日以内に返信
-
area:runtime-gc effort:S impact:low platform:linux type:bug
難易度 2/5 1〜3時間 初心者へのやさしさ 73/100
PerryTS/perry#14789 · コメント 1 件 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 70/100
メンテナーはふだん 1 日以内に返信
-
area:compiler effort:S impact:med type:bug
難易度 4/5 3〜5日 初心者へのやさしさ 56/100
メンテナーはふだん 1 日以内に返信
似ている issue
-
<input type="number"> misses text-entry leaf sizing in layout対応中かも @GoCoder7 が今日担当しました。 オープン
難易度 1/5 1時間未満 初心者へのやさしさ 88/100
DioxusLabs/blitz#1675 ·
メンテナーはふだん 1 日以内に返信
-
bug
難易度 2/5 1〜3時間 初心者へのやさしさ 65/100
metasequoiaime/msime#11666 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 77/100
メンテナーはふだん 1 日以内に返信
-
bug drawings help wanted ui
難易度 2/5 1〜3時間 初心者へのやさしさ 82/100
userFRM/egui-charts#3479 ·
メンテナーはふだん 1 日以内に返信
-
難易度 2/5 1〜3時間 初心者へのやさしさ 72/100
s2-streamstore/cachey#225 ·
メンテナーはふだん 1 日以内に返信