by codedokode · 2y ago · 0 comments
Also, if program uses lot of memory (due to alignment) it can cause swapping and the performance will be much worse than with unaligned storage.
An unaligned access could be across 2 cache lines or 2 DDR Bursts. Or across a page table (I think 4096-bytes??) Or other higher level of organization requiring multiple accesses under the hood.
X86 does the easy thing and takes two or more clock ticks to read. But aligned accesses remain faster, likely for these low level groupings of bytes.
Some architectures straight up do not allow unaligned reads and force the programmer to read two registers and then extract the unaligned data.
-------
In particular, reading or writing across two cache lines can be disastrous loss of efficiencies. The memory system needs to lock / MESI indicate both cache blocks. If one (or the other) memory location is still locked by another thread / CPU core, you'll get false sharing.
As such, a common practice is to 64-byte / 512-bit align your memory accesses in heavily multithreaded code.
Also, reading a cache-line in shared MESI state should not cause false sharing degradation, only writing.
Also, you're correct that aside from caching effects, unaligned loads and stores generally carry no penalty on x86. This is one of the smarter (IMO) things that Intel/AMD have done to keep x86's market share.
That's largely obsolete for architectures that still get used now that individual transistors are cheap, since it turns out that it's much more efficient to do it in silicon, even if such accesses are pretty rare.
For a lot of 128-bit arithmetic, it's also better to use the integer registers to take advantage of the ADC/ADCX/ADOX and MULX instructions for basic arithmetic operations. It actually depends a lot on the operation chain.
IMO it's actually a pretty big performance bug for 128-bit ints to not be 16-byte aligned.
IIRC at least one compiler (maybe ICC?) no longer bothers to emit aligned instructions at all, even if it knows the data should be aligned, because they found it to be more trouble than it's worth on modern hardware.
This allows rust programs to actually use less memory than C programs.
It's possible to guarantee the layout of a struct by opting into a specific representation, such as the `repr(C)` shown in the OP.
> There is no indirection for these types; all data is stored within the struct, as you would expect in C. However with the exception of arrays (which are densely packed and in-order), the layout of data is not specified by default.
struct A {
a: i32,
b: u64,
}
struct B {
a: i32,
b: u64,
}
> Rust does guarantee that two instances of A have their data laid out in exactly the same way. However Rust does not currently guarantee that an instance of A has the same field ordering or padding as an instance of B.> By default, composite structures have an alignment equal to the maximum of their fields' alignments. Rust will consequently insert padding where necessary to ensure that all fields are properly aligned and that the overall type's size is a multiple of its alignment. [...]
> There is no indirection for these types; all data is stored within the struct, as you would expect in C. However with the exception of arrays (which are densely packed and in-order), the layout of data is not specified by default.
struct A {
a: i32,
b: u64,
}
struct B {
a: i32,
b: u64,
}
> Rust does guarantee that two instances of A have their data laid out in exactly the same way. However Rust does not currently guarantee that an instance of A has the same field ordering or padding as an instance of B.Here is my source: https://doc.rust-lang.org/nomicon/repr-rust.html
https://learn.microsoft.com/en-us/cpp/preprocessor/pack?view...
GCC and Clang also support this pragma:
https://gcc.gnu.org/onlinedocs/gcc-13.2.0/gcc/Structure-Layo...
https://clang.llvm.org/docs/UsersManual.html#microsoft-exten...
Rust can take it a bit further. In rust, fields must be aligned to a multiple of their size, and structs/tuples must be aligned to a multiple of their largest element. This combination makes it very easy to handle memory offsets, or reusing the same representation in a way that reduces memory usage (these are called niches)[1].
This is why dynamic linking is a bit more complex in rust. Alternatively you can offer a C bridge via `extern "c"`. C will act as a bridge between rust and different languages, and at that stage you need to take care of ordering and padding.