3 ms·
std::iostream is going to be hopelessly slow, but I would like to point out that -O3 is more appropriate for templated C++ code. It seems that the two hot spot
by gpderetta 6y ago
std::iostream is going to be hopelessly slow, but I would like to point out that -O3 is more appropriate for templated C++ code.
It seems that the two hot spots are the std::string constructor (unsurprisingly) and the vector constructor.
Making string faster is not easy. I would check that gcc is configured to use C++11 strings with the small string optimization and not the older C++03 compatible refcounted string. Writing your own string or a custom allocator are options, but probably overkill for the problem.
The vector constructor is more surprising. It is possible that -O3 could help a bit there. Wrapping the map iterators with std::move_iterator would also avoid a lot of string copies.
- sesuximo 6y agoWhy copy the string at all? You have string in file. Mmap file and just store string view of it.
- gpderetta 6y agoyou can't mmap a pipe unfortunately and that's a very valid use case. At the very least you would need two distinct code paths.
- thestoicattack 6y agostd::move_iterator was my first thought too, but I'm not sure if it works, since the key type for std::unordered_map is defined to be const, so you may have to make a copy anyway. Maybe use a vector-of-pointer-to-pair, since we're leaving the map around and we don't really need the vector to own anything.
- gpderetta 6y agogood point about the string being const! There are ways around that, but none pretty.
- usefulcat 6y ago-O3 makes basically no difference for this code. I tried it.