Raymond starts using some pretty hefty Win32 magic with a mapped file view to eliminate once-and-for-all any costs associated with the getline function. And good news, his new version is 2.41 times faster than the previous version. Myself I don’t think I would have gone for the mapped file. I think I would have just used regular I/O in bigger chunks and done my own line splitting at this point. I didn’t ask Raymond but I bet he did it this way at least partly because he’s comfortable with MapViewOfFile.
Unoptimized Managed Port
Let’s drill into the costs a little bit more, this time I’ll show them in tree view. This is all the function with an inclusive cost of at least 5%. That trims the tree nicely and yet still shows the meat.
|Sanitized Function Name|| Exclusive|
|std::basic_string< … >::assign(…)||0.12||7.07|
|std::basic_string<… >::_Grow(unsigned int,bool)||0.12||6.70|
|std::basic_string<… >::_Copy(unsigned int)||0.24||6.46|
|operator new(unsigned int)||0.00||6.09|
|std::basic_string< … >::_Tidy(bool)||0.37||6.94|
Looking at that tree we can make some interesting observations. First it’s pretty clear that do_in is what we should target. There’s a lot of work going on there, probably more than is necessary. Goodness knows why InterlockedIncrement and InterlockedDecrement are being called by _Mbrtowc — and that’s adding up to something. Anyway you slice it going directly to MultiByteToWideChar which is what’s doing the actual work is seeming like a really good idea. I bet that’s where Raymond goes next.
Also worth noting is that we’re measuing the teardown cost of the dictionary and that is adding 7.92% to the overall compute time almost all of which is freeing the memory. There’s a good lesson there — many application try very hard to free their memory before they exit but I find this to generally be a total waste of time. The OS will give that memory back just fine — the last thing you should do is force it all to be loaded back into the processe’s working set and then the processors cache. It might have been swapped out! In this case it’s not much anyway though.
We’re working pretty hard and still almost 3x slower than the managed code. Stay tuned as Quiz #6 continues to develop 🙂