eliaskosunen / scnlib

scanf for modern C++
https://scnlib.dev
Apache License 2.0
1.08k stars 47 forks source link

Output parameters vs return values #129

Open dimztimz opened 3 weeks ago

dimztimz commented 3 weeks ago

My comments are mostly for the C++ proposal P1729. In the first revision the API used output parameters, similar to scanf and iostreams, but in the second revision it was changed to use return value. Now, the newer API might be more ergonomic (arguable), but it can be less performant, especially when reading strings.

Consider the following example:

std::string key;
int value;
for (...) {
    std::scan(some_input, "{} = {}", key, value);
}

versus

for (...) {
    auto result = std::scan<std::string, int>(some_input, "{} = {}")
}

The first example is more performant because it will reuse the same std::string object and it will avoid unnecesarry allocations. In the second example the std::string object will be constructed and destructed in each loop iteration, potentially allocating in each iteration. SSO saves something, but in the first example we save much more unnecessary allocations.

TD22057 commented 3 days ago

I agree w/ this comment. IMO the v1 API was simpler to use, easier to understand, and easier to see how the format string tied to values. Converting our code to the new version has resulting in much longer and more confusing parse statements.