News for hackers. Stuff that matters.
Re:Memory Safe Languages in Android 13 by pjmlp on Friday December 02, 2022 @05:10AM (#33828710)
It does if you learn to use C# properly,

https://learn.microsoft.com/en-us/dotnet/api/system.runtime....

In actual native code produced by RyuJit, you don't need to worry about cache lines for single instances, because the struct might not even exist at all, the Jit having mapped fields into CPU registers instead.

When it matters, like the struct being part of an array, use StructLayout.

Re:Memory Safe Languages in Android 13 by tialaramex on Friday December 02, 2022 @07:51AM (#33829823)
That link seems like it's about alignment rather than about arrays inside structures?
Re:Memory Safe Languages in Android 13 by pjmlp on Friday December 02, 2022 @08:01AM (#33829917)
No, it is about alignment and packing, you use StructLayout attribute alongside LayoutKind and FieldOffsetAttribute.

https://learn.microsoft.com/en-us/dotnet/api/system.runtime....

You main issue was how structures arrange their fields.

Also regarding arrays and structs, as of C# 7 you can use fixed to declare static arrays inside structs, however these structs need to be marked as unsafe.

Re:Memory Safe Languages in Android 13 by tialaramex on Friday December 02, 2022 @09:54PM (#33840036)
> as of C# 7 you can use fixed to declare static arrays inside structs, however these structs need to be marked as unsafe.

That is exactly what I was talking about.

Re:Memory Safe Languages in Android 13 by pjmlp on Saturday December 03, 2022 @12:53AM (#33841048)
Ah ok, somehow misunderstood that.

However there are actually good reasons for it to be unsafe, although it is debatable if that alone should be it.

One is due to the interactions with the GC, in case it moves the data and there are references to its elements, and stack size.

One way to get around it is to use AoS instead of SoA, which is any the best option if performance is the ultimate goal.