nodejs / nodejs/node

[V8 perf] Loop callback changing is throttling

Aperta
#65,894 1 commento 0 reazioni 0 assegnatari Vedi su GitHub

Nessuno ha ancora preso questa issue.

Lingua principale
JavaScript
Stelle
122k
Fork
37.3k
Merge medio
4g 2h
PR unite (30g)
283

Descrizione

Version

v24.20.0

Platform
Windows 10
Subsystem

No response

What steps will reproduce the bug?
How often does it reproduce? Is there a required condition?

Always

What is the expected behavior? Why is that the expected behavior?
  • BOX (~5 secs)
  • BOX (~5 secs)
  • LANCZOS (~8 secs)
  • LANCZOS (~8 secs)
What do you see instead?
  • BOX (~5 secs)
  • BOX (~5 secs)
  • LANCZOS (~30 secs)
  • LANCZOS (~30 secs)
Additional information

I have downsample1D_V1 function (it's very unoptimized, but it works, and it's perfect for the demonstration), which executes the passed callback const weight = fn(x); billions of times (3_107_687_040 times per 1 downsample2D call).

There are 2 types of the passed callback function — lanczos3 and box:

function lanczos3(x: number): number {
  if (-3 <= x && x < 3) {
    return sinc(x) * sinc(x / 3);
  }
  return 0;
}

function sinc(x: number): number {
  if (x === 0) {
    return 1;
  }
  x = Math.PI * x;
  return Math.sin(x) / x;
}


function box(x: number): number {
  if (x > -0.5 && x <= 0.5) {
    return 1;
  }
  return 0;
}

I call downsample2D multiple times with the follow callback params (demo-1.ts):

  • BOX (~5 secs)
  • BOX (~5 secs)
  • LANCZOS (~30 secs)
  • LANCZOS (~30 secs)

The first 2 calls work with the expected speed, for ~5 second.
But when the callback is changed, downsample2D works multiple times slower than expected, for ~30 seconds.

In reverse order I also face the throttling (demo-2.ts):

  • LANCZOS (~8 secs)
  • LANCZOS (~8 secs)
  • BOX (~30 secs)
  • BOX (~30 secs)

The problem appears on both Node.js (24) and Deno (2.9).

Bun (1.3/1.4) works fine:

  • BOX (~5 secs)
  • BOX (~5 secs)
  • LANCZOS (~8 secs)
  • LANCZOS (~8 secs)

Related keywords: performance v8 polymorphic inline cache

Guida per i contributori

Apri la guida per i contributori

Come iniziare

  1. Leggi tutta la issue e poi la guida ai contributi del progetto.
  2. Commenta sulla issue per dire che te ne occupi tu — evita che due persone facciano lo stesso lavoro.
  3. Fai un fork del repository e lavora su un branch.
  4. Apri una pull request che faccia riferimento al numero della issue.

Direzione di ricerca

Inizia clonando la riproduzione collegata ed eseguendo demo-1.ts e demo-2.ts con Node.js v24, confrontando i tempi del cambio dei callback. Analizza il comportamento riportato della inline cache polimorfica di V8 e verifica il risultato con i casi BOX e LANCZOS. Il lavoro è completato quando la modifica dei callback non fa più rallentare le chiamate ripetute a downsample2D rispetto ai tempi attesi.

Scritto dal modello di indicizzazione a partire dal testo della issue.

Valutazione

Stack tecnologico
javascript
Ambito
performance
Tipo di issue
Bug
Difficoltà
5/5
Tempo stimato
Più di una settimana
Stato di attività
Attiva
Chiarezza
Da chiarire
Idoneità per principianti
25/100

Ricevi le nuove issue nella tua casella

Un breve riepilogo di issue GitHub adatte ai principianti.