nodejs / nodejs/node

[V8 perf] Loop callback changing is throttling

Đang mở
#65,894 1 bình luận 0 reaction 0 người được giao Xem trên GitHub

Chưa có ai nhận issue này.

Ngôn ngữ chính
JavaScript
Star
122k
Fork
37.4k
Merge trung bình
4 ngày 3 giờ
Pull request đã merge (30 ngày)
272

Mô tả

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

Hướng dẫn đóng góp

Mở hướng dẫn đóng góp

Bắt đầu từ đâu

  1. Đọc hết issue, rồi đọc hướng dẫn đóng góp của dự án.
  2. Bình luận trên issue rằng bạn sẽ nhận — tránh hai người làm cùng một việc.
  3. Fork repository và làm thay đổi trên một nhánh.
  4. Mở pull request có tham chiếu số hiệu của issue.

Hướng nghiên cứu

Bắt đầu bằng cách clone bản tái hiện được liên kết và chạy demo-1.ts cùng demo-2.ts trên Node.js v24, đồng thời so sánh thời gian khi chuyển đổi các callback. Điều tra hành vi inline cache đa hình được báo cáo của V8 và xác minh kết quả với các trường hợp BOX và LANCZOS. Hoàn thành khi việc thay đổi callback không còn khiến các lần gọi downsample2D lặp lại bị chậm hơn so với thời gian kỳ vọng.

Do mô hình lập chỉ mục viết ra từ nội dung của issue.

Đánh giá

Công nghệ
javascript
Lĩnh vực
performance
Loại issue
Lỗi
Độ khó
5/5
Thời gian dự kiến
Hơn một tuần
Mức độ hoạt động
Sôi nổi
Độ rõ ràng
Cần làm rõ
Mức phù hợp với người mới
25/100

Nhận issue mới trong hộp thư của bạn

Bản tóm tắt ngắn những issue GitHub phù hợp với người mới.