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

Abierto
#14,978 0 comentarios 0 reacciones 0 asignados Ver en GitHub

Los mantenedores suelen responder en 1 día

Nadie ha tomado este issue todavía.

Evaluación

Dificultad
4/5
Tiempo estimado
3-5 días
Aptitud para principiantes
45/100
Tipo de issue
Error
Claridad
Bastante claro
Estado de actividad
Activo
Stack tecnológico
node.js, rust, typescript

Línea de trabajo

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.

Escrito por el modelo de indexación a partir del texto del issue.

Descripción

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).
Lenguaje dominante
Rust
Estrellas
4.9k
Forks
166
Merge medio
4 h 54 min
PR fusionados (30 d)
909

Preparar el entorno

Primeros pasos

  1. Lee el issue completo y luego la guía de contribución del proyecto.
  2. Comenta en el issue que vas a ocuparte — evita que dos personas hagan lo mismo.
  3. Haz un fork del repositorio y trabaja en una rama.
  4. Abre un pull request que haga referencia al número del issue.

Más de PerryTS/perry

Todos los issues de PerryTS/perry

Issues similares

Más issues de Rust

Recibe los nuevos issues en tu correo

Un resumen breve de issues de GitHub para principiantes.