0

I'm trying to render the Mandelbrot set quickly so I remade the calculations function in web assembly thinking it would be faster but it is the same speed

(module
  (func (export "getcolorj") (param f64 f64 f64 f64) (result i32) (local f64 f64 i32 f64 f64) 
    local.get 0
    local.set 4;;cx=x
    local.get 1
    local.set 5;;ci=i
    i32.const 0
    local.set 6;;i3=0
    (block
    (loop
    local.get 4
    local.get 4
    f64.mul
    local.get 5
    local.get 5
    f64.mul
    f64.sub
    local.set 7;;const temp=cx**2-ci**2
      
    local.get 4
    local.get 5
    f64.mul
    f64.const 2
    f64.mul
    local.get 3
    f64.add
    local.set 5;;ci=cx*ci*2+i
      
    local.get 7
    local.get 2
    f64.add
    local.set 4;;cx=temp+x
      
    local.get 4
    local.get 4
    f64.mul
    local.get 5
    local.get 5
    f64.mul
    f64.add
    local.set 7;;(cx**2+ci**2)
     
    local.get 6
    i32.const 1
    i32.add
    local.set 6;;i3++
    (i32.eq (local.get 6) (i32.const 100))
    (f64.gt (local.get 7) (f64.const 4))
    i32.add
    (i32.ne (i32.const 0))
    br_if 1
    (br 0)
    )
     )
    local.get 6
    i32.const -1
    i32.add
)
)

vs

function getcolorj(x,i,juliusx,juliusi,deap){
var cx= x
var ci= i
var i3=0
for("";i3<deap;i3++){
const temp=cx**2-ci**2
ci=cx*ci*2+juliusi
cx=temp+juliusx
if((cx**2+ci**2)>4){break;}
}
return i3
}

I don't know what I did wrong or if this is just how it is, did I just make the web assembly poorly? this is my first time trying it so if it is the case I wouldn't be too surprised

JIC how I started it matters:

WebAssembly.instantiateStreaming(fetch('test.wasm'), {})
.then(results => {
  console.log()
  getcolorj=results.instance.exports.getcolorj
});

btw I'm doing 10,000 interactions for qualitys sake, and yeah both slow down when rendering the black sections.

2
  • 1
    Modern JavaScript interpreters are surprisingly good at JITing functions that have the same type signature (5 Numbers in your case?), so I'm not very surprised here. It's likely that the overhead from calling a WASM function from JS costs a lot too; if you were to implement computing the full image in WASM, things could be different.
    – AKX
    Commented Aug 10, 2021 at 19:52
  • the function does slow down by a lot when rendering black parts of the set the web assembly doesn't even help there?
    – MathMan05
    Commented Aug 10, 2021 at 20:03

1 Answer 1

1

WebAssembly is not going to give you a significant speed boost versus JavaScript in most practical application for the simple reason that JavaScript is really quite fast already.

What it will give you is a more predictable runtime performance, and in future access to features such as SIMD that may give better performance in some circumstances.

For more detail, see this question:

Why is webAssembly function almost 300 time slower than same JS function

1
  • thanks for the response, and I'm sorry about my late response, this post is what made me think that it would be faster than the js equivalent, as it's been 3 years since that post and I thought it would have been faster, and I'm doing 10,000 iterations (I know it's overkill) and thought it would have been large enough to be faster than the JS equivalent. should I expect the Web assembly to speed up over time or stay at the same speed?
    – MathMan05
    Commented Aug 24, 2021 at 1:47

Not the answer you're looking for? Browse other questions tagged or ask your own question.