winit icon indicating copy to clipboard operation
winit copied to clipboard

Better compatibility with math libraries

Open Wandalen opened this issue 3 years ago • 4 comments

Most math libraries define vectors as #[repr(C)]. For example popular libs cgmath and nalgebra do that:

winit does not define such structures as winit::dpi::PhysicalSize with #[repr(C)]. If winit followed the same pattern, it would let just reinterpret data instead of constructing another vector. That would make code more optimal, winit more compatible with math libraries and more comfortable to use. Although, you may argue each pro, nevertheless, I don't see any con. Note: my motivation is not as much performance as ability to write less code.

Can I please open PR?

Wandalen avatar Apr 27 '22 21:04 Wandalen

I think this is a case of very premature optimization, especially because a normal reinterpretation of the bytes compiles down to nothing, see this playground link (view the generated assembly in release mode).

madsmtm avatar Apr 27 '22 22:04 madsmtm

I am offering only to add #[repr(C)] to structures. Why is it necessary? Because most math libs do that for vectors. Definitions of vectors should be compatible among libs. Forget about my use case, I am sure there are more use cases which require this changes.

Please attach label type: enhancement

Wandalen avatar Apr 28 '22 05:04 Wandalen

@madsmtm as I noted, optimization is not the main reason vectors should have #[repr(C)]. Although, optimization not always work as in the over-simplified experiment. If you will embed vector in an another structure compiler is much more restricted in what it can do. Also compiler might fail to optimize it in case of exotic settings for example if you build for web and optimization is set for size.

And contrary, adding #[repr(C)] for vectors does not have any negative effect as a far as I see.

Wandalen avatar Apr 28 '22 05:04 Wandalen

I personally don't see much use for it, but you also don't have to ask before opening a PR. For more complex issues (and certain bugfixes), it may be preferable to open an issue first or coordinate through other channels (like our matrix/irc room/channel).

Also compiler might fail to optimize it in case if you build for web and optimization is set for size.

Do you have an example of this happening, or is this just speculation? The compiler is generally pretty good about optimizing stuff, and I don't think these types end up in any sort of hot path.


Anyway, we already support something I believe is far more important for math lib compatibility: mint, so I don't really see the need for this change, but I don't strongly oppose it either.

maroider avatar Apr 28 '22 06:04 maroider