TurboWarp / scratch-vm

Scratch VM with a JIT compiler and more features
https://turbowarp.org/
Mozilla Public License 2.0
75 stars 72 forks source link

pen optimisation #161

Closed pwouik closed 9 months ago

pwouik commented 1 year ago

turbowarp don't dramatically improve pure pen performance A factor is that scratch use one path per line, instead of just one path My testing in javascript show that a continuous path is 7~8x faster

function drawCanvas() {
    var canvas = document.getElementById("canvas");
    var ctx = canvas.getContext("2d");
    ctx.clearRect(0, 0, canvas.width, canvas.height);
    var t = performance.now();
    for(var x=0;x<1000;x++){
            //ctx.beginPath();
        for(var y=0;y<1000;y++){
            ctx.beginPath();
            ctx.moveTo(x,y);
            ctx.lineTo(x+1, y+1);
            ctx.stroke();
        }
            //ctx.stroke();
    }
    console.log(performance.now()-t);
}

window.addEventListener("load", drawCanvas);

An idea would be to batch paths of the same style per frame, using Path2D to store them and moveTo to jump from one to another line the batch would need to be flushed before using sensors and erase all (and once per frame)

GarboMuffin commented 1 year ago

turbowarp don't dramatically improve pure pen performance

Show me an example project to compare TurboWarp (compiler disabled) and Scratch. We do.

Pen lines are WebGL, not canvas 2d, so your example's results are not directly meaningful.

GarboMuffin commented 1 year ago

You are correct that Scratch does one draw-call per pen line and that buffering them up into groups would be faster, which is why we already do that

pwouik commented 1 year ago

oh nice, I also thought of webgl but I wasn't sure you could have enough parity with scratch I used this project: https://scratch.mit.edu/projects/441947766/