swc icon indicating copy to clipboard operation
swc copied to clipboard

High Idle CPU Usage

Open phiresky opened this issue 7 months ago • 4 comments

Describe the bug

The issue https://github.com/swc-project/swc/issues/10243 still persists even in the latest version:

cat go.js

require("@swc-node/register");

require("./test");

cat test.ts

class Test {
  a;
  b;
  c;
  d;
  e;
  f;
  g;
  h;
  i;
  j;
  k;
  l;
  m;
  n;
  o;
  p;
  q;
  r;
  s;
  t;
  u;
  v;
  w;
  x;
  y;
  z;
  constructor() {}
  hello() {
    return 'HELLO';
  }
}


setTimeout(() => {}, 20 * 1000); // to make the process run for 20 seconds

cat package.json

{
  "dependencies": {
    "@swc-node/register": "^1.10.10",
    "@swc/core": "^1.11.24"
  }
}

With 1.11.24:

/usr/bin/time -v node go.js
	Command being timed: "node go.js"
	User time (seconds): 2.73
	System time (seconds): 0.81
	Percent of CPU this job got: 17%
	Elapsed (wall clock) time (h:mm:ss or m:ss): 0:20.32
	Average shared text size (kbytes): 0
	Average unshared data size (kbytes): 0
	Average stack size (kbytes): 0
	Average total size (kbytes): 0
	Maximum resident set size (kbytes): 156620
	Average resident set size (kbytes): 0
	Major (requiring I/O) page faults: 0
	Minor (reclaiming a frame) page faults: 15834
	Voluntary context switches: 1125721
	Involuntary context switches: 133
	Swaps: 0
	File system inputs: 0
	File system outputs: 0
	Socket messages sent: 0
	Socket messages received: 0
	Signals delivered: 0
	Page size (bytes): 4096
	Exit status: 0

With 1.10.8:

/usr/bin/time -v node go.js
	Command being timed: "node go.js"
	User time (seconds): 0.33
	System time (seconds): 0.07
	Percent of CPU this job got: 2%
	Elapsed (wall clock) time (h:mm:ss or m:ss): 0:20.32
	Average shared text size (kbytes): 0
	Average unshared data size (kbytes): 0
	Average stack size (kbytes): 0
	Average total size (kbytes): 0
	Maximum resident set size (kbytes): 131564
	Average resident set size (kbytes): 0
	Major (requiring I/O) page faults: 0
	Minor (reclaiming a frame) page faults: 14030
	Voluntary context switches: 175
	Involuntary context switches: 17
	Swaps: 0
	File system inputs: 0
	File system outputs: 0
	Socket messages sent: 0
	Socket messages received: 0
	Signals delivered: 0
	Page size (bytes): 4096
	Exit status: 0

As you can see, the process constantly uses 15% CPU (2.7s of CPU time in 20s) while in 1.10.8 it only uses 2% cpu (0.33s in 20s), all of which is during transpilation (0% during idle).

No swcrc file is present.

CPU usage seems to proportionally rise the more properties the class has.

Link to the code that reproduces this issue

https://play.swc.rs/?version=1.11.24&code=H4sIAAAAAAAAAx3Pyw6CQAwF0P18RXeAMWF0S3Rn4oLEDT%2BAUAR5DM50fBH%2B3bluTtqk7U1VNZTOUcFOaFFEZRa4ggrUgEEDbqAFHbiDHgxgBBMwYAYPYIEDAjx4ghd4gw%2F4%2FnPN5MT6SoyNE1pWJPIwGDShDtdYvJ0oOp%2Fy%2FBJhZVWrUsqxFN3IxkscZg%2FHsLulvaYN7bTWSUZpSmJoLHsmaZlmayoOv1s%2FUWMsRh2H9Nr9AO8kjXIVAQAA&config=H4sIAAAAAAAAA1VQOw6DMAzdOQXy3LUdeocewkoNCiIf2Y5UhLh7E0Io3ez38bO9dn0Pkxh49msucxORhfjsMyKLV%2FxkBHSJJIZtVLg1VqVQyol2ZKsEKPJIWkwk90MNcwhCGRtwFmpCRi9DYFcigQmNXsM5ebWumACTBodqDdSkFuWst8NyNZngIpPIf1SRoh%2Fnc4M6pjuWBhfeaSePT5Rr6wEP%2BIla2DkYrLyac3%2FD9gW9JRc9VQEAAA%3D%3D

SWC Info output

Operating System:
    Platform: linux
    Arch: x64
    Machine Type: x86_64
    Version: #1 SMP PREEMPT_DYNAMIC Thu, 13 Mar 2025 18:12:00 +0000
    CPU: (16 cores)
        Models: AMD Ryzen 7 7840U w/ Radeon  780M Graphics

Binaries:
    Node: 23.9.0
    npm: 11.3.0
    Yarn: 4.7.0
    pnpm: N/A

Relevant Packages:
    @swc/core: 1.11.24
    @swc/helpers: N/A
    @swc/types: 0.1.21
    typescript: 5.8.3

SWC Config:
    output: N/A
    .swcrc path: N/A

Next.js info:
    output: N/A

Expected behavior

No CPU usage

Actual behavior

High CPU usage on idle (even after transpilation done)

Version

1.11.24

Additional context

No response

phiresky avatar May 12 '25 17:05 phiresky

Also note the Voluntary context switches: 1125721 and the higher memory usage.

phiresky avatar May 12 '25 17:05 phiresky

Please provide a standalone repository to reproduce this

kdy1 avatar May 12 '25 17:05 kdy1

Sure, here you go: https://gist.github.com/phiresky/e3d3dff24ff495552971dda945521b84

git clone https://gist.github.com/phiresky/e3d3dff24ff495552971dda945521b84
cd e3d3dff24ff495552971dda945521b84
npm install
node go.js

phiresky avatar May 12 '25 18:05 phiresky

I find that the heartbeats in chili scopes are not dropped so https://github.com/dragostis/chili/blob/6c49338d25f1656ba1cdd0b9b21479cd4207652f/src/lib.rs#L152 is always running and calling HashMap::retain (l.heartbeats.len() == num_workers is always false because there are extra scoped heartbeats).

I believe something wrong with https://github.com/dudykr/ddbase/blob/b092d7ca474c3cc30816bd2a416bfe1689b85883/crates/par-core/src/lib.rs#L182, which prevents scopes being dropped. The problem is solved when I remove SCOPE.set(Some(scope));, but I'm not sure whether this is correct. cc @kdy1

CPunisher avatar Jun 07 '25 13:06 CPunisher

I'll re-close this with https://github.com/swc-project/swc/pull/10629

kdy1 avatar Jun 19 '25 01:06 kdy1

This closed issue has been automatically locked because it had no new activity for a month. If you are running into a similar issue, please create a new issue with the steps to reproduce. Thank you.

swc-bot avatar Jul 20 '25 00:07 swc-bot