oven-sh / bun

Incredibly fast JavaScript runtime, bundler, test runner, and package manager – all in one
https://bun.sh
Other
73.42k stars 2.7k forks source link

node-sdl poor performance #9218

Open pedroth opened 6 months ago

pedroth commented 6 months ago

What version of Bun is running?

1.0.28+705638470

What platform is your computer?

Linux 6.5.0-21-generic x86_64 x86_64

What steps can reproduce the bug?

// index.js
import sdl from '@kmamal/sdl'

// Setup
const window = sdl.video.createWindow({ title: "Canvas" })
// window.setFullscreen(true);
const { pixelWidth: width, pixelHeight: height } = window
const buffer = Buffer.alloc(width * height * 4).fill(0);

const play = async ({ time, oldT }) => {
    const newT = new Date().getTime();
    const dt = (newT - oldT) * 1e-3;
    console.log(`FPS:${Math.floor(1 / dt)}`);
    for (let n = 0; n < buffer.length; n += 4) {
        const i = Math.floor(n / (4 * width));
        const j = Math.floor((n / 4) % width);
        const x = j;
        const y = height - 1 - i;
        const px = x / width;
        const py = y / height;
        buffer[n] = Math.min(255, ((px * time) % 1) * 255);
        buffer[n + 1] = Math.min(255, ((py * time) % 1) * 255);
        buffer[n + 2] = 0;
        buffer[n + 3] = 255;
    }
    window.render(width, height, width * 4, 'rgba32', buffer);
    setTimeout(() => play({
        oldT: newT,
        time: time + dt,
    }));
}
setTimeout(() => play({ oldT: new Date().getTime(), time: 0 }));

If I run node index.js I get ~60 FPS, If I run bun index.js I get ~20 FPS.

package.json:

{
  "name": "sdl",
  "version": "1.0.0",
  "description": "",
  "main": "index.js",
  "type": "module",
  "scripts": {
    "test": "echo \"Error: no test specified\" && exit 1"
  },
  "author": "",
  "license": "ISC",
  "dependencies": {
    "@kmamal/sdl": "^0.10.2"
  }
}

What is the expected behavior?

I would expect to have better performance on bun instead of node

What do you see instead?

Better performance on node

Additional information

I can't exit the program, when using Ctrl +C and in node I can.

dylan-conway commented 6 months ago

Hitting pause in the debugger almost always hits slow_path_mod. Hardcoding render to window.render(10, 10, 40, "rgba32", buffer); and removing the contents of the for loop will improve performance significantly. I'm guessing there's a problem with jit and napi functions not allowing some operators to hit their fast path.

Screenshot 2024-03-04 at 10 17 31 AM
pedroth commented 3 months ago

Any update on this?