Is it faster to create stackalloc arrays or NativeArrays for temp operations inside jobs?

So I’m receiving some feedback because I use NativeArray(Allocator.Temp) for a temporal use inside a job, to get some items, use them for the calculations, then it gets disposed after the job automatically.

I was told to use

stackalloc int [ x ]

instead. I’ve never used it before and never had big performance issues.

But it makes all my calls unsafe. Not sure if that’s really faster and in what context and if it’s really worth it.

Any ideas? Thanks

1 Like

I suspect that unless you’re doing these allocations in a hot loop (which in general might not be a good idea from a performance standpoint) then I don’t think it’ll make much of a difference. stackalloc might be slightly faster. But as always, I can just recommend doing performance profiling and measuring the difference. There might be some complex compilation interactions that give non-obvious results, so it’s always worth measuring, if performance needs to be improved in that area.

4 Likes