Making `wc` 20x faster with parallel state machines
2 days ago
- The blog post explains how to make the Unix wc command faster using a state machine, inspired by Robert Graham's wc2 repository.
- A basic wc implementation tracks whether the previous character was whitespace to count words, but a state machine approach models transitions between states for each input character.
- The state machine method uses a transition table and character type table, resulting in a simple loop that processes each byte efficiently.
- Benchmarks show that the state machine version is slower than GNU wc for ASCII input due to branch prediction advantages, but it becomes faster for UTF-8 text.
- Supporting UTF-8 in the state machine requires additional states for multibyte sequences, but the per-character cost remains constant, making it significantly faster than standard wc implementations that use mbrtowc and iswspace.
- The author parallelized the state machine by splitting input into chunks processed by multiple threads, using a producer-consumer model with work and free queues.
- Parallelization achieves 5-20x speedup on large inputs, with throughput reaching ~4 GB/s, outperforming disk read speeds due to caching.
- The appendix compares disassembly and branch misses: the state machine reduces branch mispredictions by 99.5% compared to GNU wc.
- A quick intro to Unicode and UTF-8 is provided, explaining how the byte encoding works.