Re:Parsing JSON in 500 lines of Rust
by thrance on Wednesday February 19, 2025 @05:42AM
(#43100687)
Do not parse using strip_prefix and then to_string, you're allocating a new buffer after each token! You can use Cow<str> for the result of the "parse string value" function, as most JSON strings don't have escapes you can just return a reference to that slice of the buffer, only allocating when there's actually an escape.
In general when writing a parser you should strive to minimize allocations and backtracking.
In general when writing a parser you should strive to minimize allocations and backtracking.