Upgrade to Pro — share decks privately, control downloads, hide ads and more …

The 1000x Spreadsheet: Performance from first p...

The 1000x Spreadsheet: Performance from first principles

Follow Causal's journey of scaling their spreadsheet engine from thousands to billions of cells. Profilers are helpful but first-principle thinking is required to break out of iterative improvements and make order of magnitude improvements.

Avatar for Angad Nadkarni

Angad Nadkarni

August 18, 2022

Other Decks in Programming

Transcript

  1. Categories add cells exponentially 💣 10 variables x 100 months

    x 10 products x 10 countries 
 
 x 10 languages 1,000 cells 10,000 cells 100,000 cells 1,000,000 cells
  2. Atwood’s Law 👨⚖ - Single Threaded - Event loop woes

    - Workers and data serialization - Interpreted environment - Preallocation discouraged - Everything is a hash-map
  3. Profiling for fun and profit - go tool pprof -

    Preallocation, String concatenations - sync.Pool
  4. - Initialise what you can upfront. - Channels are for

    productivity, not performance. Use errgroup. - Anything that needs safety can go into a Context. - Run go test --race in CI! Practice safe concurrency 🫃
  5. Establishing a baseline with Napkin Math™ Rockets need metals. Software

    needs CPU, memory, and I/O. Simon Eskildsen sirupsen.com/napkin
  6. “I believe that first-principle thinking is required to break out

    of iterative improvement and make order of magnitude improvements.”
  7. Napkin Math 🧠 - 1 variable with 1 Billion cells

    = 8 GB - Adding 2 variables = (2 + 1) * 8 GB = 24 GB - With SIMD and threading: 30 ms / 1 GB - 24 GB * 30 ms ≅ 700 ms
  8. 👎 - If an array is filled up 50% or

    so, is it really sparse? - The real cost of maps will never show up on profiles. - Flame charts don’t show CPU cache misses. Maps in Golang are really fast 🤷 👍 - Golang hashes things really fast. Especially integer-keys.
  9. ELI5: CPU Caches 👶 - Temporal Locality = LRU -

    Spatial Locality = Cache lines - Typically, each cache line is 64 bytes
  10. How do we prove our hypothesis? 🧐 - The real

    world has too much variance (esp. on public clouds like GCP/ AWS). - Simpler POC → Faster iteration → More experimentation. - The simplest Causal model is adding up two variables with millions of cells.
  11. Structs-of-arrays, Out-of-bands… - Smaller structs - When storing references: Pointers

    are 64-bit, integers are 32-bit. - Structs should reflect locality in data-access.
  12. The road ahead 🚏 - Vectorised Arithmetic + SIMD -

    Time-series Compression - GPUs - Distributed Monte Carlo simulations - Sparse data structures …