-
-
Notifications
You must be signed in to change notification settings - Fork 672
What is the correct way of resetting the heap when using "stub" or "minimal" runtime? #2736
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Comments
Hi! AssemblyScript can't optimize simple structures like this.color.set(
unchecked(pixels[p ]),
unchecked(pixels[p + 1]),
unchecked(pixels[p + 2])
);
this.color.add(ray.getColor(ray, this.world.objects, this.depth));
unchecked(pixels[p ] = this.color.xf32);
unchecked(pixels[p + 1] = this.color.yf32);
unchecked(pixels[p + 2] = this.color.zf32); better to use: let objects = this.world.objects;
let depth = this.depth;
for ... {
for ... {
let r = unchecked(pixels[p + 0]);
let g = unchecked(pixels[p + 1]);
let b = unchecked(pixels[p + 2]);
let color = ray.getColor(ray, objects, depth);
r += color.r;
g += color.g;
b += color.b;
unchecked(pixels[p + 0] = r);
unchecked(pixels[p + 1] = g);
unchecked(pixels[p + 2] = b);
}
} Also note: you can use global I hope this helps. |
Thanks for the hints! I will definitely check them out, especially scalars whenever possible. ❤️ |
The best way to increase performance is to minimize allocations. You can also try to use SIMD where it makes sense. Also, in the minimal runtime, you should call |
Thanks guys! With (a lot) of refactoring, tricks from @MaxGraey and a By the way, I've also tried to use |
Yeah, sometimes |
Next simple optimization could be replacing all |
@CountBleck Yeah, but shouldn't it be exported in that interface in the first place? @MaxGraey Thanks, I guess I'm already doing it. The only function I need for my Or do you mean getting rid of |
Yes. Switching to
I didn't see this. For example |
Yeah, I thought you were talking about using |
Also, instead Before: export default class Vector3
{
private readonly vec: StaticArray<f64> = new StaticArray<f64>(3);
public constructor (x: f64 = 0.0, y: f64 = x, z: f64 = x) {
this.vec[0] = x;
this.vec[1] = y;
this.vec[2] = z;
}
...
} After: export class Vector3 {
constructor(
public x: f32 = 0,
public y: f32 = 0,
public z: f32 = 0,
) {}
...
} |
Question
First of all, I'd like to thank you for maintaining this awesome project. I've really enjoyed working with
AssemblyScript
, great job!After implementing what I thought was a perfect algorithm for WASM, I realized that the result was a lot slower than it's counter part written in TypeScript. I made some research and tested several tricks aimed to improve performance. As mentioned here and here some things did help. The
unchecked()
function most of them all, but I do use@inline
whenever it makes sense and always cache arrays' length and useStaticArray
s (even though it does not make much difference in my tests comparing toFloat32Array
s orUint8ClampedArray
s, I decided to stick with it in order to avoid missing some optimization technique). I compile with--optimize
-O3
andconverge
andnoAssert
are both set totrue
. Unfortunately none of that helped and theAS
version is running~10
times slower thanTS
counterpart.Then I've read this fantastic article and it seamed like one last thing to do is to play with
runtime
options. Indeed usingminimal
helped a bit. For instance,TS
version takes on average0.08
seconds to render a frame,incremental
runtime takes~0.55
seconds,minimal
takes~0.31
and withstub
it's about0.17
seconds. Now, I know that withminimal
&stub
runtimes I'm supposed to clean memory manually and according to this, "free all memory at once" would probably work best for me, but I'm not sure how to do that since there is aheap.reset()
method in documentation but it seems to be missing in theAPIs
. Maybe it's deprecated and I'm supposed to use onlyheap.free(ptr: usize)
, but I'm not sure how that works? How can I get theptr
to myStaticArray
let's say..?Another possible optimization I was thinking about is to use a less OO approach (maybe starting from
Vector3
class) since it seems to me like WASM doesn't play nice with objects so maybe it's worth testing with raw functions even though the original idea was to mimic as much as possibleTS
version and I don't even know if that might help, maybe you guys have some tips on that?Sorry if that was too long, I'll leave some more info here if someone has time to take a look and hopefully suggest some further optimization, it would be much appreciated, thanks a lot!
stub
without freeing memory, out of range after 11th sample, click on generated image to see stats)The text was updated successfully, but these errors were encountered: