Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

alignment vs stride in Swift

Tags:

swift

In Swift 4, the MemoryLayout struct tells you the size, stride, and alignment of a type.

I understand size and stride but not alignment really.

Is there an example that shows what alignment is, how it is different from stride, when it has a different value from stride, and where it would be incorrect to use stride but correct to use alignment?

Can I always compute one from the other?

like image 669
algal Avatar asked Dec 02 '17 18:12

algal


People also ask

What is the difference between alignment and stride of a struct?

The alignment of the struct is the maximal alignment of all its fields, in this case the maximum of 2 and 1. The stride of the struct is the size rounded up to alignment, here 3 rounded up to a multiple of 4.

What are the alignment options available in SwiftUI?

SwiftUI Stack Alignment and Alignment Guides 1 Container Alignment. The most basic of alignment options when working with SwiftUI stacks is container alignment. ... 2 Alignment Guides. ... 3 Using the Alignment Guides Tool. ... 4 Custom Alignment Types. ... 5 Cross Stack Alignment. ... 6 ZStack Custom Alignment. ... 7 Summary. ...

How to align Int32 to 4 bytes in Swift?

In Swift, the simple types such as Intand Doublehave the same alignment value as their size. A 32-bit (4 byte) integer has a size of 4 bytes and needs to be aligned to 4 bytes. MemoryLayout<Int32>.size// returns 4MemoryLayout<Int32>.alignment// returns 4MemoryLayout<Int32>.stride// returns 4

What are the three properties of a swift type?

12 March 2018 ∙ Swift Internals ∙ written by Greg Heo Swift types have three properties to consider when you’re dealing with them in memory: size, stride, and alignment. Size Let’s start with two simple structs:


Video Answer


1 Answers

Here is a simple example:

struct Foo {
    let a: Int16
    let b: Int8
}

print(MemoryLayout<Foo>.size)       // 3
print(MemoryLayout<Foo>.alignment)  // 2
print(MemoryLayout<Foo>.stride)     // 4
  • The alignment of the struct is the maximal alignment of all its fields, in this case the maximum of 2 and 1.
  • The stride of the struct is the size rounded up to alignment, here 3 rounded up to a multiple of 4.

The stride is the distance between (the start of) contiguous instance of the same type in memory:

let array = [Foo(a: 1, b:2), Foo(a: 3, b: 4), Foo(a: 5, b: 6)]
array.withUnsafeBytes {
    print(Data($0) as NSData) // <01000234 03000474 0500066f>
    print($0.count) // 12
}

The struct stride is a multiple of the struct alignment, so that all instances (and therefore all instance fields) are properly aligned.

The details can be found in Type Layout:

Fragile Struct and Tuple Layout

Structs and tuples currently share the same layout algorithm, noted as the "Universal" layout algorithm in the compiler implementation. The algorithm is as follows:

  • Start with a size of 0 and an alignment of 1.
  • Iterate through the fields, in element order for tuples, or in var declaration order for structs. For each field:
    • Update size by rounding up to the alignment of the field, that is, increasing it to the least value greater or equal to size and evenly divisible by the alignment of the field.
    • Assign the offset of the field to the current value of size.
    • Update size by adding the size of the field.
    • Update alignment to the max of alignment and the alignment of the field.
  • The final size and alignment are the size and alignment of the aggregate. The stride of the type is the final size rounded up to alignment.
like image 147
Martin R Avatar answered Oct 09 '22 01:10

Martin R