caching

package
v0.0.0-...-811d8d3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Sep 13, 2015 License: Apache-2.0 Imports: 1 Imported by: 0

README

CPU Caches

Understanding how the hardware works is an critical component to understanding how to write the most performant code you can. Knowing the basics of processor caching can help you make better decisions within the scope of writing idiomatic code.

Acknowledgment

This content is provided by Scott Meyers from his talk in 2014 at Dive:

CPU Caches and Why You Care

Notes

  • CPU Caches works by caching memory on cache lines.
  • On our 64 bit processors, the cache line will be 64k.
  • Cache lines are moved and stored in L1, L2 and L3 caches.
  • Memory in L1 and L2 caches is also in L3 cache.
  • Both data and instructions are stored in these caches.
  • Hardware likes to traverse data and instructions linearly along cache lines.
  • Access to main memory is incredibly slow, we need the cache.
    • Accessing one byte from main memory will cause an entire cache line to be read.
    • Writes to one byte in a cache line requires the entire cache line to be written.
  • Small = Fast
    • Compact, well localized code that fits in cache is fastest.
    • Compact data structures that fit in cache are fastest.
    • Traversals touching only cached data is the fastest.
  • Predictable access patterns matter.
    • Provide regular patterns of memory access.
    • Hardware can make better predictions about required memory.

Cache Hierarchies

This is subject to be different in different processors. For this content, the following is the multi-levels of cache associated with the Intel 4 Core i7-9xx processor:

L1 - 64KB Cache (Per Core)
	32KB I-Cache
	32KB D-Cache
	2 HW Threads

L2 - 256KB Cache (Per Core)
	Holds both Instructions and Data
	2 HW Threads

L3 - 8MB Cache
	Holds both Instructions and Data
	Shared across all 4 cores
	8 HW Threads

This is a diagram of the relationship of the cache hierarchy for each core and main memory:

figure1

https://www.youtube.com/watch?v=WDIkqP4JbkE

http://www.akkadia.org/drepper/cpumemory.pdf

http://www.extremetech.com/extreme/188776-how-l1-and-l2-cpu-caches-work-and-why-theyre-an-essential-part-of-modern-chips

Code Review

Caching (Go Playground)

Tests (Go Playground)


Ardan Labs Ardan Studios GoingGo Blog


All material is licensed under the Apache License Version 2.0, January 2004.

Documentation

Overview

package caching provides code to show why CPU caches matter and the way the hardware caches memory affects performance.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL