Preface¶

  • What is better code?
  • What is good code?
  • Goals:
    • Not prescriptive
    • Not always achievable
  • Not limited to C++
  • Language is a constraint

Definitions¶

The term raw appears in a couple of goals, and I'll use it frequently throughout this talk.

The raw usage of a construct means the construct should be considered too low-level and not used outside of a function or class whose primary purpose it to create a safe and efficient, higher level construct that can be used instead.

Throughout these slides I use word to mean a machine word, typically 64 bits on a 64 bit architecture and 32 bits on a 32 bit architecture. I do not mean a Win32 WORD type which is 16 bits.

Philosophy¶

  • Correct
  • Efficient
    • On a wide variety of consumer and enterprise hardware
    • Balance CPU/GPU/ML/Memory/Storage/Power resources
  • Reusable
  • Scalable
    • With large data sets, but primarily on a single machine

Note: What is not on this list:

  • Readable: Not that I don't think readability is important, I think people put it too high on the list. Code isn't prose, code is math.

Software is Physics¶

  • Object are physical entities
  • The building block for modern computers is the transistor
    • A transistor is simply an electronically controlled switch, similar to a relay but without moving parts
Relay
Relay
  • To build a transistor, silicon is doped with a material such as Boron and Phosphorus
    • Silicon + Boron = P, fewer electrons, insulator
    • Silicon + Phosphorus = N, has free electrons, conductive
Transistor Construction
Transistor Construction
  • We can make a NAND gate by combining two transistors
NAND Gate
NAND Gate
  • A transistor is fast, but not infinitely fast, so we need a clock to delineate which A and B the result applies to
CMOS NAND Gate
CMOS NAND Gate
  • Combining two NAND gates with feedback creates a sequential circuit and a simple memory cell, an SR latch
SR Latch
SR Latch
  • Combining SR Latches we can create a register
Register
Register
  • By combining transistors with a clock to form sequential circuits we can make both memory and a processor
    • A modern CPU contains billions of transistors
  • The transistor count increases complexity
    • Memory hierarchy
    • Parallel execution
    • Speculative execution
    • Branch prediction
  • The fundamental physics don't change, for performance we want:
    • Simple operations
    • Executed sequentially
    • Operating on memory near the processor

Software is Mathematics¶

  • Not pure mathematics

    • We are not describing systems on an idealized device that cannot be realized
  • Mathematics is a tool to describe what is and a tool to reason about code

  • Software is defined on algebraic structures