for( auto value : from_csv( str ) ) { … }
{
std::vector<std::string> tokens = tokenize(line);
// do work with tokens
}
which you could change to std::vector<std::string> tokens;
{
tokens.clear();
tokenize(line, &tokens);
// do work with tokens
}
Combined with your suggestion to use std::string_view, this would mean only O(1) allocations across the program for this part of the code.not quite, std::string can store <=22 character strings without needing to allocate (in 64 bit mode at least) (look up short string optimization), 22 characters is actually quite a lot in the context of tokenization, so its not a given that switching to string views would be an improvement here
implementation dependent - the c++ standard says nothing on this
* libstdc++ has an internal reference to its own address for the SSO. If the moved-from string was referencing its SSO buffer, the moved-to string needs to use its own address. The branch is differentiating the SSO state from a heap-allocated state.
* libc++ string move can be implemented this way, but the branch ends up happening on access to the string. It still needs to discard the old heap allocated buffer, if need-be as well.
if I was actually tasked with hyper-optimizing a tokenizer I would probably skip past string view and do a pair of U16 indexes instead assuming the input file is less than 65k characters [with a "slow path" that uses U32 instead]. I just think that its probably not actually going to be a whole order of magnitude faster than just using string (unless there's long tokens)
If you can keep the original source string around, consider using std::vector<std::string_view> with each string_view pointing to part of the original text.