nodejs / nodejs/node

[V8 perf] Loop callback changing is throttling

未关闭
#65,894 1 条评论 0 个 reaction 已指派 0 人 在 GitHub 查看

还没有人认领这个 Issue。

主要语言
JavaScript
星标
122k
派生
37.4k
平均合并
4 天 3 小时
30 天内合并 PR
272

描述

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

贡献指南

打开贡献指南

从这里开始

  1. 先读完整个 Issue,再读项目的贡献指南。
  2. 在 Issue 下留言说明你要接手 —— 这能避免两个人做同样的事。
  3. Fork 仓库,在一个分支上完成修改。
  4. 提交 Pull Request,并在描述里引用这个 Issue 编号。

调研方向

首先克隆链接的复现代码,并在 Node.js v24 下运行 demo-1.ts 和 demo-2.ts,比较切换 callback 时的耗时。调查报告中的 V8 多态 inline cache 行为,并针对 BOX 和 LANCZOS 情况验证结果。当更改 callback 不再导致重复的 downsample2D 调用相较于预期耗时变慢时,即视为完成。

由索引模型根据 Issue 内容生成。

评估

技术栈
javascript
领域
performance
Issue 类型
缺陷
难度
5/5
预计耗时
一周以上
活跃度
活跃
描述清晰度
需要澄清
新手友好度
25/100

把新 issue 发到你的邮箱

精选适合新手参与的 GitHub issue 摘要。