The fun continues as today we look at Raymond's third improvement.
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.
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 :)