Aaron Patterson - Rails World 2024 Closing Keynote

Aaron Patterson explains how Rails router performance was improved through optimizations like reducing allocations, byte parsing, and handwritten parsers in this Rails World 2024 keynote.

Key takeaways
  • The Rails router rewrite improved performance by reducing allocations and optimizing the parser, resulting in ~10% fewer allocations and faster boot times

  • Moving from RACC (Ruby parser generator) to a handwritten recursive descent parser provided better JIT optimization and eliminated Ruby-to-C call overhead

  • String Scanner methods like peek_byte, scan_byte and skip can be used to parse strings without allocations by working with bytes directly instead of creating new string objects

  • Arrays can be more performant than hashes for lookups when dealing with bounded integer keys (like bytes 0-255) since they avoid hash computation overhead

  • Understanding what allocates objects in Ruby is crucial for optimization:

    • Integers, symbols, booleans and nil don’t allocate
    • String operations, array/hash creation usually allocate
    • Math operations may allocate depending on the operation
  • Converting the router’s parser to use symbol tokens instead of strings reduced unnecessary allocations during the parsing process

  • The tokenizer was optimized to avoid returning arrays and strings when possible, using skip operations and byte-level matching instead

  • Parser generators like RACC can be problematic due to:

    • Complex grammar file syntax
    • C runtime dependencies
    • Required array-based token formats
    • Poor JIT optimization opportunities
  • Historical Rails features that didn’t make it to 1.0 included:

    • Active Spacer for table layouts
    • Action Button for UI components
    • WebsterWeb for rapid site building
    • RJS for JavaScript generation
  • The first version of Rails was released in July 2004, though there is some debate about earlier 2002 versions