Skip to content

Low-Cost Vectors, Colors, Structs #2721

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

Closed
mattdesl opened this issue Jul 21, 2023 · 4 comments
Closed

Low-Cost Vectors, Colors, Structs #2721

mattdesl opened this issue Jul 21, 2023 · 4 comments

Comments

@mattdesl
Copy link

Feature suggestion

I have some code that deals with geometry and graphics programming, where Vectors and Colors are often needed. However, I'd like to avoid allocating new memory, or using classes, if possible. I've noticed the performance, bundle size, and complexity (in terms of dealing with Wasm Memory) are much smaller and more manageable when just using variables, or even global variables, so I wonder how to achieve some of these things with AssemblyScript.

Example use case:

struct Vec2 {
  x:f32;
  y:f32;
}

function normalize (vec:Vec2):void {
  let len:f32 = vec.x * vec.x + vec.y * vec.y;
  if (len > 0) {
    let f:f32 = Mathf.sqrt(len);
    vec.x /= f;
    vec.y /= f;
  }
}

export function init ():void {
  let a:Vec2 = Vec2(0.5, 0.25);
  normalize(a);
  /* do something with a.x, a.y */
}

I don't really care for functions, memory management, or other features of classes, and I don't want my bundle size to bloat with each new class definition. Really I am just using this as a sort of tuple. Is this possible?

@Heath123
Copy link

I think this is a duplicate of #2254

@CountBleck
Copy link
Member

CountBleck commented Jul 21, 2023

Your proposed syntax wouldn't really be compatible with TS in any way. TS has a syntax for tuple types but #2254 (comment) (as @Heath123 pointed out) states the issues with using that and other possibilities.

Since you're also looking for performance improvements, maybe v128 is of interest to you. It can fit four f32s/two f64s and allows you to potentially make code faster by using SIMD ops.

@mattdesl
Copy link
Author

Thanks. I am OK with any syntax really; or a decorator. Since this is a dupe we can close and continue discussion in the other topic.

In this case, SIMD is interesting but for my use case it will involve a lot of extracting/loading/storing which makes me wonder if it would be as performant, even something like normalize() I have not found a simple way with AssemblyScript's SIMD to achieve f32 dot (or horizontal add after f32x4.mul), and then an extract to compare against zero.

@CountBleck
Copy link
Member

SIMD would likely be more helpful if/when you loop over a bunch of vectors and do some calculations.

Anyways, I might as well close this then...

@CountBleck CountBleck closed this as not planned Won't fix, can't repro, duplicate, stale Jul 21, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

3 participants