alignment in c
play

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


  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

  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

  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

  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

  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

  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

  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

  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

  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

  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

  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

  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

  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

  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

  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

  16. Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary Introduction Alignment 101 • Let’s save a char , a short and an in our memory: int Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 7/ 32

  17. Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary Introduction Alignment 101 • Let’s save a char , a short and an in our memory: int 0x00000000 0x00000004 0x00000008 0x00000012 Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 7/ 32

  18. Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary Introduction Alignment 101 • Let’s save a char , a short and an in our memory: int 0x00000000 0x00000004 0x00000008 0x00000012 • Oh wait Sven-Hendrik Haase Seminar “Effiziente Programmierung in C” 7/ 32

  19. Introduction Data Structure Alignment Heap Alignment Stack Alignment Summary Introduction Alignment 101 • Let’s save a char , a short and an in our memory: int 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

  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

  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

  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

  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

  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

  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

  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

  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

Download Presentation
Download Policy: The content available on the website is offered to you 'AS IS' for your personal information and use only. It cannot be commercialized, licensed, or distributed on other websites without prior consent from the author. To download a presentation, simply click this link. If you encounter any difficulties during the download process, it's possible that the publisher has removed the file from their server.

Recommend


More recommend