[V8 perf] Loop callback changing is throttling

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

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

評価

難易度
5/5
見積もり時間
1週間以上
初心者へのやさしさ
25/100
issue の種類
バグ
明瞭さ
説明が足りない
活発さ
活発
技術スタック
javascript
領域
performance

調査の方向性

まず、リンクされた再現コードをクローンし、Node.js v24 で demo-1.ts と demo-2.ts を実行して、コールバック切り替え時のタイミングを比較します。報告されている V8 の多相インラインキャッシュの挙動を調査し、BOX と LANCZOS のケースに対して結果を検証します。コールバックを変更しても、繰り返し実行される downsample2D 呼び出しが期待されるタイミングから遅くならなくなれば完了です。

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

説明

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

主要言語
JavaScript
スター
122k
フォーク
37.4k
平均マージ
4日 3時間
マージ済み PR(30日)
273

コントリビューションガイド

コントリビューションガイドを開く

はじめの一歩

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

nodejs/node のほかの issue

nodejs/node の issue をすべて見る

似ている issue

JavaScript の issue をもっと見る

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

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