Increasing the performance of WebAssembly Text Format parser by 350%
4 months ago
- #Performance
- #Parser Optimization
- #WebAssembly
- Rewrote the WAT parser from scratch, achieving a 350% performance increase.
- Switched from a parser combinator library (winnow) to a hand-written parser for better performance and future optimization.
- Optimized by cloning well-known green tokens and nodes (like parentheses and keywords) instead of recreating them.
- Improved keyword matching by checking source code prefixes in bytes and verifying non-identifier characters.
- Used `get_unchecked` for ASCII tokens to skip UTF-8 boundary checks.
- Introduced a custom `Token` type to delay the creation of expensive `rowan::GreenToken`.
- Reduced allocations by using a shared single `Vec` for node creation, inspired by `rowan::GreenNodeBuilder`.
- Benchmark results show significant performance improvement: old parser ~59.5 µs vs. new parser ~13.1 µs.