After a few days of searching and trying various methods including scnlib, sscanf, istringstream, etc., maybe I found the answer. If you care about parsing performance, you should use the from_chars
family of functions in charsconv header that were added to C++17. This is the only reasonable solution for fast parsing of string_view
in C++.
According to cppreference:
Unlike other parsing functions in C++ and C libraries, std::from_chars is locale-independent, non-allocating, and non-throwing. Only a small subset of parsing policies used by other libraries (such as std::sscanf) is provided. This is intended to allow the fastest possible implementation that is useful in common high-throughput contexts such as text-based interchange (JSON or XML).
The problems with the other methods are as follows:
scnlib: I've found that its performance is related to the length of the incoming string, and that even when you're only parsing an integer, passing in a long string causes a linear performance degradation. This is unacceptable for scenarios requiring high performance.
istringstream: As the question says, it can't handle scopes and inevitably requires a copy at initialization time.
sscanf: A C-style null terminated string is necessary, while some implementations may likewise cause performance problems according to cppreference.
istream
around it for convenience). – Lookeron