Alignment in C Seminar Effiziente Programmierung in C Sven-Hendrik - - PowerPoint PPT Presentation

alignment in c
SMART_READER_LITE
LIVE PREVIEW

Alignment in C Seminar Effiziente Programmierung in C Sven-Hendrik - - PowerPoint PPT Presentation

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary Alignment in C Seminar Effiziente Programmierung in C Sven-Hendrik Haase Universit at Hamburg, Fakult at f ur Informatik 2014-01-09 Sven-Hendrik


slide-1
SLIDE 1

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Alignment in C

Seminar “Effiziente Programmierung in C” Sven-Hendrik Haase

Universit¨ at Hamburg, Fakult¨ at f¨ ur Informatik

2014-01-09

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 1/ 32

slide-2
SLIDE 2

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Outline

Introduction Guiding Questions of This Presentation Memory Addressing Alignment 101 Consquences of Misalignment Different Types of Alignment Data Structure Alignment Structs and Stuff Padding in the Real World Performance Implications SSE Heap Alignment Introduction Example Use Cases Stack Alignment Introduction The Problem Use Cases Summary TL;DR Resources Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 2/ 32

slide-3
SLIDE 3

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Introduction

Guiding Questions of This Presentation

  • Which types of alignment exist in C?

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 3/ 32

slide-4
SLIDE 4

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Introduction

Guiding Questions of This Presentation

  • Which types of alignment exist in C?
  • What is data alignment?

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 3/ 32

slide-5
SLIDE 5

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Introduction

Guiding Questions of This Presentation

  • Which types of alignment exist in C?
  • What is data alignment?
  • What is heap alignment?

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 3/ 32

slide-6
SLIDE 6

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Introduction

Guiding Questions of This Presentation

  • Which types of alignment exist in C?
  • What is data alignment?
  • What is heap alignment?
  • What is stack alignment?

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 3/ 32

slide-7
SLIDE 7

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Introduction

Guiding Questions of This Presentation

  • Which types of alignment exist in C?
  • What is data alignment?
  • What is heap alignment?
  • What is stack alignment?
  • How does it work in C?

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 3/ 32

slide-8
SLIDE 8

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Introduction

Guiding Questions of This Presentation

  • Which types of alignment exist in C?
  • What is data alignment?
  • What is heap alignment?
  • What is stack alignment?
  • How does it work in C?
  • Do we need to care abouy any of these?

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 3/ 32

slide-9
SLIDE 9

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Introduction

Memory Addressing

  • Computers address memory in word-sized chunks

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 4/ 32

slide-10
SLIDE 10

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Introduction

Memory Addressing

  • Computers address memory in word-sized chunks
  • A word is a computer’s natural unit for data
  • Word size is defined by architecture
  • Usual word sizes: 4 bytes on 32-bit, 8 bytes on 64-bit

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 4/ 32

slide-11
SLIDE 11

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Introduction

Memory Addressing

  • Computers address memory in word-sized chunks
  • A word is a computer’s natural unit for data
  • Word size is defined by architecture
  • Usual word sizes: 4 bytes on 32-bit, 8 bytes on 64-bit
  • This means we can only address data at memory locations

that are multiples of 4 or 8 respectively (strictly speaking)

  • Many processors allow access of arbitrary memory locations

while some fail horribly

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 4/ 32

slide-12
SLIDE 12

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Introduction

Memory Addressing

  • Modern processors can load word-sized (4 bytes) and long

word-sized (8 bytes) memory locations equally well

  • Find out word-sizes:
  • getconf WORD_BIT (32 for me, 32 on RPi)
  • getconf LONG_BIT (64 for me, 32 on RPi)

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 5/ 32

slide-13
SLIDE 13

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Introduction

Alignment 101

  • Assume a 32-bit architecture with a word size of 4 byte

0x00000000 0x00000004 0x00000008 0x00000012

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 6/ 32

slide-14
SLIDE 14

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Introduction

Alignment 101

  • Assume a 32-bit architecture with a word size of 4 byte

0x00000000 0x00000004 0x00000008 0x00000012

  • Let’s save a 4 byte int

in our memory:

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 6/ 32

slide-15
SLIDE 15

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Introduction

Alignment 101

  • Assume a 32-bit architecture with a word size of 4 byte

0x00000000 0x00000004 0x00000008 0x00000012

  • Let’s save a 4 byte int

in our memory: 0x00000000 0x00000004 0x00000008 0x00000012

  • Looks good!

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 6/ 32

slide-16
SLIDE 16

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Introduction

Alignment 101

  • Let’s save a char

, a short and an int in our memory:

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 7/ 32

slide-17
SLIDE 17

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Introduction

Alignment 101

  • Let’s save a char

, a short and an int in our memory: 0x00000000 0x00000004 0x00000008 0x00000012

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 7/ 32

slide-18
SLIDE 18

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Introduction

Alignment 101

  • Let’s save a char

, a short and an int in our memory: 0x00000000 0x00000004 0x00000008 0x00000012

  • Oh wait

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 7/ 32

slide-19
SLIDE 19

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Introduction

Alignment 101

  • Let’s save a char

, a short and an int in our memory: 0x00000000 0x00000004 0x00000008 0x00000012

  • Oh wait
  • Needs two memory accesses and some arithmetic to fetch the

int.

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 7/ 32

slide-20
SLIDE 20

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Introduction

Alignment 101

  • We need to be smarter about this!
  • Padding

to the rescue

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 8/ 32

slide-21
SLIDE 21

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Introduction

Alignment 101

  • We need to be smarter about this!
  • Padding

to the rescue 0x00000000 0x00000004 0x00000008 0x00000012

  • Much better
  • This is considered naturally aligned

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 8/ 32

slide-22
SLIDE 22

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Introduction

Consquences of Misalignment

  • Different behavior depending on architecture
  • Alignment fault errors on some platforms (RISC, ARM)
  • Bad performance on others
  • SSE requires proper alignment per specification (though this

restriction is about to be removed)

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 9/ 32

slide-23
SLIDE 23

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Introduction

Different Types of Alignment

  • Some definitions so we don’t get confused:

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 10/ 32

slide-24
SLIDE 24

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Introduction

Different Types of Alignment

  • Some definitions so we don’t get confused:
  • Data Structure Alignment refers to the alignment of

sequential memory inside a data structure (struct)

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 10/ 32

slide-25
SLIDE 25

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Introduction

Different Types of Alignment

  • Some definitions so we don’t get confused:
  • Data Structure Alignment refers to the alignment of

sequential memory inside a data structure (struct)

  • Heap Alignment refers to the alignment of dynamically

allocated memory

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 10/ 32

slide-26
SLIDE 26

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Introduction

Different Types of Alignment

  • Some definitions so we don’t get confused:
  • Data Structure Alignment refers to the alignment of

sequential memory inside a data structure (struct)

  • Heap Alignment refers to the alignment of dynamically

allocated memory

  • Stack Alignment refers to the alignment of the stack pointer

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 10/ 32

slide-27
SLIDE 27

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Structs and Stuff

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 11/ 32

slide-28
SLIDE 28

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Structs and Stuff

Consider this: struct Foo { char x; // 1 byte short y // 2 bytes int z; // 4 bytes };

  • The struct’s naive size would be 1 byte + 2 bytes + 4 bytes =

7 bytes

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 11/ 32

slide-29
SLIDE 29

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Structs and Stuff

Consider this: struct Foo { char x; // 1 byte short y // 2 bytes int z; // 4 bytes };

  • The struct’s naive size would be 1 byte + 2 bytes + 4 bytes =

7 bytes

  • Of course, we know it’s actually going to be

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 11/ 32

slide-30
SLIDE 30

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Structs and Stuff

Consider this: struct Foo { char x; // 1 byte short y // 2 bytes int z; // 4 bytes };

  • The struct’s naive size would be 1 byte + 2 bytes + 4 bytes =

7 bytes

  • Of course, we know it’s actually going to be 8 bytes due to

padding

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 11/ 32

slide-31
SLIDE 31

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Structs and Stuff

  • A struct is aligned to the largest type’s alignment

requirements

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 12/ 32

slide-32
SLIDE 32

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Structs and Stuff

  • A struct is aligned to the largest type’s alignment

requirements

  • This can yield some rather inefficient structures:

struct Foo { char x; // 1 byte double y // 8 bytes char z; // 1 bytes };

  • The struct’s naive size would be 1 byte + 8 bytes + 1 bytes =

10 bytes

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 12/ 32

slide-33
SLIDE 33

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Structs and Stuff

  • A struct is aligned to the largest type’s alignment

requirements

  • This can yield some rather inefficient structures:

struct Foo { char x; // 1 byte double y // 8 bytes char z; // 1 bytes };

  • The struct’s naive size would be 1 byte + 8 bytes + 1 bytes =

10 bytes

  • Its effective size is

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 12/ 32

slide-34
SLIDE 34

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Structs and Stuff

  • A struct is aligned to the largest type’s alignment

requirements

  • This can yield some rather inefficient structures:

struct Foo { char x; // 1 byte double y // 8 bytes char z; // 1 bytes };

  • The struct’s naive size would be 1 byte + 8 bytes + 1 bytes =

10 bytes

  • Its effective size is 24 bytes!

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 12/ 32

slide-35
SLIDE 35

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Structs and Stuff

  • The memory ineffiency can be minimized by reordering the

members like so: struct Foo { char x; // 1 byte char z; // 1 bytes double y // 8 bytes };

  • Now it’s only 16 bytes, best we can do if we want to keep

alignment

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 13/ 32

slide-36
SLIDE 36

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Structs and Stuff

  • How about this?

struct Foo { double a; // 8 byte char b; // 1 byte char c; // 1 byte short d; // 2 bytes int e; // 4 bytes double f; // 8 bytes };

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 14/ 32

slide-37
SLIDE 37

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Structs and Stuff

  • How about this?

struct Foo { double a; // 8 byte char b; // 1 byte char c; // 1 byte short d; // 2 bytes int e; // 4 bytes double f; // 8 bytes };

  • This structure is 24 bytes in total
  • Most efficient configuration possible
  • It’s called tighly packed

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 14/ 32

slide-38
SLIDE 38

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Structs and Stuff

  • How about extension types?

struct Foo { char x; // 1 byte __uint128_t y; // 16 bytes char a; // 1 byte __uint128_t b; // 16 bytes };

  • This struct is

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 15/ 32

slide-39
SLIDE 39

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Structs and Stuff

  • How about extension types?

struct Foo { char x; // 1 byte __uint128_t y; // 16 bytes char a; // 1 byte __uint128_t b; // 16 bytes };

  • This struct is 64 bytes
  • World’s most wasteful struct

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 15/ 32

slide-40
SLIDE 40

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Structs and Stuff

  • Of course, we can also reorder this to make it 34 bytes only

struct Foo { __uint128_t y; // 16 bytes __uint128_t b; // 16 bytes char x; // 1 byte char a; // 1 byte };

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 16/ 32

slide-41
SLIDE 41

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Padding in the Real World

  • Every decent compiler will automatically use data structure

padding depending on architecture

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 17/ 32

slide-42
SLIDE 42

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Padding in the Real World

  • Every decent compiler will automatically use data structure

padding depending on architecture

  • Some compilers support -Wpadded which generates nice

warnings about structure padding

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 17/ 32

slide-43
SLIDE 43

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Padding in the Real World

  • Every decent compiler will automatically use data structure

padding depending on architecture

  • Some compilers support -Wpadded which generates nice

warnings about structure padding

  • Compiler warnings can help you find inefficiencies
  • Example output with clang:

clang -Wpadded -o example1 example1.c example1.c:5:11: warning: padding struct ’struct Foo’ with 1 byte to align ’y’ [-Wpadded] short y; ^ 1 warning generated.

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 17/ 32

slide-44
SLIDE 44

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Padding in the Real World

  • It’s possible to prevent the compiler from padding a struct

using either __attribute__((packed)) after a struct definition, #pragma pack (1) in front of a struct definition

  • r -fpack-struct as a compiler parameter

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 18/ 32

slide-45
SLIDE 45

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Padding in the Real World

  • It’s possible to prevent the compiler from padding a struct

using either __attribute__((packed)) after a struct definition, #pragma pack (1) in front of a struct definition

  • r -fpack-struct as a compiler parameter
  • Either of these generate an incompatible ABI
  • We can use the sizeof operator to check the effective size of

a struct

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 18/ 32

slide-46
SLIDE 46

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Performance Implications

  • Do we actually have to worry about this?

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 19/ 32

slide-47
SLIDE 47

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Performance Implications

  • Do we actually have to worry about this?
  • Most likely not unless in special use cases (device drivers,

extremely memory limited computers) or when using a compiler from 1878

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 19/ 32

slide-48
SLIDE 48

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Performance Implications

For fun, let’s look at the performance impact of misaligned memory:

struct Foo { char x; short y; int z; }; struct Foo foo; clock_gettime(CLOCK, &start); for (unsigned long i = 0; i < RUNS; ++i) { foo.z = 1; foo.z += 1; } clock_gettime(CLOCK, &end); struct Bar { char x; short y; int z; } __attribute__((packed)); struct Bar bar; clock_gettime(CLOCK, &start); for (unsigned long i = 0; i < RUNS; ++i) { bar.z = 1; bar.z += 1; } clock_gettime(CLOCK, &end);

Compiled with

gcc -DRUNS=400000000 -DCLOCK=CLOCK_MONOTONIC -std=gnu99 -O0

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 20/ 32

slide-49
SLIDE 49

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Performance Implications

Results

aligned runtime: 9.504220399 s unaligned runtime: 9.491816620 s

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 21/ 32

slide-50
SLIDE 50

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Performance Implications

Results

aligned runtime: 9.504220399 s unaligned runtime: 9.491816620 s

  • Takes the same time!

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 21/ 32

slide-51
SLIDE 51

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Performance Implications

Results

aligned runtime: 9.504220399 s unaligned runtime: 9.491816620 s

  • Takes the same time!
  • Nowadays it totally doesn’t matter for performance! :D
  • Modern processors can read aligned/unaligned memory

equally fast (at least Intel Sandy Bridge and up)

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 21/ 32

slide-52
SLIDE 52

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Performance Implications

Results

aligned runtime: 9.504220399 s unaligned runtime: 9.491816620 s

  • Takes the same time!
  • Nowadays it totally doesn’t matter for performance! :D
  • Modern processors can read aligned/unaligned memory

equally fast (at least Intel Sandy Bridge and up)

  • But what about processors with the computing power of a

potato?

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 21/ 32

slide-53
SLIDE 53

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Performance Implications

Results on Raspberry Pi with 1/10 the loop length

aligned runtime: 12.174631568 s unaligned runtime: 26.453561832 s

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 22/ 32

slide-54
SLIDE 54

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

Performance Implications

Results on Raspberry Pi with 1/10 the loop length

aligned runtime: 12.174631568 s unaligned runtime: 26.453561832 s

  • On some architectures alignment matters a lot!
  • We can nicely see that it takes about twice the time (two

memory fetches) + some arithmetic

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 22/ 32

slide-55
SLIDE 55

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

SSE

  • Classically, SSE requires 16 byte alignment of data and stack

pointer

  • Requirement will be lifted soon

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 23/ 32

slide-56
SLIDE 56

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Data Structure Alignment

SSE

  • Classically, SSE requires 16 byte alignment of data and stack

pointer

  • Requirement will be lifted soon
  • Compilers automatically align to that when using SIMD types

(__m128 and friends)

  • x86 64 is 16 byte aligned anyway
  • Very modern compilers even automagically vectorize loops
  • No worries to the programmer

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 23/ 32

slide-57
SLIDE 57

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Heap Alignment

Introduction

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 24/ 32

slide-58
SLIDE 58

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Heap Alignment

Introduction

  • malloc is usually good enough
  • Allocated memory is aligned to largest primitive type

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 24/ 32

slide-59
SLIDE 59

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Heap Alignment

Introduction

  • malloc is usually good enough
  • Allocated memory is aligned to largest primitive type
  • Use aligned_alloc instead of malloc for custom alignments
  • Other heap alignment functions: posix_memalign,

aligned_alloc and valloc

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 24/ 32

slide-60
SLIDE 60

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Heap Alignment

Introduction

  • malloc is usually good enough
  • Allocated memory is aligned to largest primitive type
  • Use aligned_alloc instead of malloc for custom alignments
  • Other heap alignment functions: posix_memalign,

aligned_alloc and valloc

  • memalign and pvalloc are considered obsolete

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 24/ 32

slide-61
SLIDE 61

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Heap Alignment

Example

#include <stdio.h> #include <stdlib.h> #define SIZE 1024 * 1024 #define ALIGN 4096 int main() { void* a = malloc(SIZE); void* b = aligned_alloc(ALIGN, SIZE); printf("a: %p, a %% %i: %lu\n", a, ALIGN, ((unsigned long)a) % ALIGN); printf("b: %p, b %% %i: %lu\n", b, ALIGN, ((unsigned long)b) % ALIGN); return 0; }

Results

a: 0x7fdec2265010, a % 4096: 16 b: 0x7fdec1cec000, b % 4096: 0

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 25/ 32

slide-62
SLIDE 62

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Heap Alignment

Use Cases

You should consider using custom heap memory alignments

  • when. . .

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 26/ 32

slide-63
SLIDE 63

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Heap Alignment

Use Cases

You should consider using custom heap memory alignments

  • when. . .
  • interfacing with low-level stuff (hardware)

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 26/ 32

slide-64
SLIDE 64

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Heap Alignment

Use Cases

You should consider using custom heap memory alignments

  • when. . .
  • interfacing with low-level stuff (hardware)
  • trying to be really clever about CPU cache line optimization

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 26/ 32

slide-65
SLIDE 65

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Heap Alignment

Use Cases

You should consider using custom heap memory alignments

  • when. . .
  • interfacing with low-level stuff (hardware)
  • trying to be really clever about CPU cache line optimization
  • writing custom allocators (for instance when writing an

interpreter or garbage collector)

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 26/ 32

slide-66
SLIDE 66

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Heap Alignment

Use Cases

You should consider using custom heap memory alignments

  • when. . .
  • interfacing with low-level stuff (hardware)
  • trying to be really clever about CPU cache line optimization
  • writing custom allocators (for instance when writing an

interpreter or garbage collector)

  • using SIMD and your compilers is too stupid to align stuff

properly by itself

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 26/ 32

slide-67
SLIDE 67

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Stack Alignment

Introduction

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 27/ 32

slide-68
SLIDE 68

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Stack Alignment

Introduction

  • Different platforms make different assumptions about stack

alignment

  • Platforms:
  • Linux: depends (legacy is 4 byte, modern is 16 byte)
  • Windows: 4 byte
  • OSX: 16 byte
  • x86 64 always uses 16 byte

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 27/ 32

slide-69
SLIDE 69

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Stack Alignment

Introduction

  • Different platforms make different assumptions about stack

alignment

  • Platforms:
  • Linux: depends (legacy is 4 byte, modern is 16 byte)
  • Windows: 4 byte
  • OSX: 16 byte
  • x86 64 always uses 16 byte
  • But why do we care?

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 27/ 32

slide-70
SLIDE 70

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Stack Alignment

Introduction

  • Different platforms make different assumptions about stack

alignment

  • Platforms:
  • Linux: depends (legacy is 4 byte, modern is 16 byte)
  • Windows: 4 byte
  • OSX: 16 byte
  • x86 64 always uses 16 byte
  • But why do we care?
  • Mixing stack alignments is very bad!

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 27/ 32

slide-71
SLIDE 71

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Stack Alignment

The Problem

Consider this: void foo() { struct MyType bar; }

  • Looks benign!

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 28/ 32

slide-72
SLIDE 72

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Stack Alignment

The Problem

Consider this: void foo() { struct MyType bar; }

  • Looks benign!
  • Imagine it is 16 byte aligned, then what will happen if this is

called from a platform with 4 byte alignment such as Windows?

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 28/ 32

slide-73
SLIDE 73

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Stack Alignment

The Problem

Consider this: void foo() { struct MyType bar; }

  • Looks benign!
  • Imagine it is 16 byte aligned, then what will happen if this is

called from a platform with 4 byte alignment such as Windows?

  • Stack corruption

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 28/ 32

slide-74
SLIDE 74

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Stack Alignment

The Problem

  • We don’t usually care about stack alignment unless we have to

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 29/ 32

slide-75
SLIDE 75

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Stack Alignment

The Problem

  • We don’t usually care about stack alignment unless we have to
  • If we have cross-architecture calls, we need special tricks

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 29/ 32

slide-76
SLIDE 76

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Stack Alignment

The Problem

  • We don’t usually care about stack alignment unless we have to
  • If we have cross-architecture calls, we need special tricks
  • To fix, decorate function with

__attribute__((force_align_arg_pointer)) or use

  • mstackrealign

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 29/ 32

slide-77
SLIDE 77

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Stack Alignment

The Problem

  • We don’t usually care about stack alignment unless we have to
  • If we have cross-architecture calls, we need special tricks
  • To fix, decorate function with

__attribute__((force_align_arg_pointer)) or use

  • mstackrealign (or stop using Windows)
  • Other compiler arguments to play with stack alignment:
  • mpreferred-stack-boundary,
  • mincoming-stack-boundary

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 29/ 32

slide-78
SLIDE 78

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Stack Alignment

Use Cases

  • Play with stack alignment only if you absolutely, positively

have to

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 30/ 32

slide-79
SLIDE 79

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Stack Alignment

Use Cases

  • Play with stack alignment only if you absolutely, positively

have to

  • Software that needs stack alignment: valgrind (virtual CPU),

wine (cross-compiled cross-platform cross-architecture compatibility layer), cross-compilers, kernels

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 30/ 32

slide-80
SLIDE 80

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Stack Alignment

Use Cases

  • Play with stack alignment only if you absolutely, positively

have to

  • Software that needs stack alignment: valgrind (virtual CPU),

wine (cross-compiled cross-platform cross-architecture compatibility layer), cross-compilers, kernels

  • Very memory limited device

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 30/ 32

slide-81
SLIDE 81

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Stack Alignment

Use Cases

  • Play with stack alignment only if you absolutely, positively

have to

  • Software that needs stack alignment: valgrind (virtual CPU),

wine (cross-compiled cross-platform cross-architecture compatibility layer), cross-compilers, kernels

  • Very memory limited device
  • You will probably never have to worry about this

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 30/ 32

slide-82
SLIDE 82

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Summary

TL;DR

Do worry about

  • Positions of members within a struct
  • Using weird compiler parameters
  • GCC, Windows and SSE instructions

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 31/ 32

slide-83
SLIDE 83

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Summary

TL;DR

Do worry about

  • Positions of members within a struct
  • Using weird compiler parameters
  • GCC, Windows and SSE instructions

Do not worry about

  • Struct alignment/padding (compilers are smart)
  • Performance issues (computers are fast)
  • The Stack (unless you are doing really wierd stuff)

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 31/ 32

slide-84
SLIDE 84

Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary

Summary

Resources

  • http://www.agner.org/optimize/blog/read.php?i=142&v=t
  • http://en.wikipedia.org/wiki/Data_structure_alignment
  • http://en.wikipedia.org/wiki/Word_(data_type)
  • http://www.geeksforgeeks.org/structure-member-alignment-padding-and-data-packing/
  • http://lemire.me/blog/archives/2012/05/31/data-alignment-for-speed-myth-or-reality/
  • http://www.makelinux.com/books/lkd2/ch19lev1sec3
  • http://www.cs.umd.edu/class/sum2003/cmsc311/Notes/Data/aligned.html
  • http://tuxsudh.blogspot.de/2005/05/structure-packing-in-gcc.html
  • http://www.peterstock.co.uk/games/mingw_sse/
  • http://eigen.tuxfamily.org/dox-2.0/WrongStackAlignment.html

Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 32/ 32