3 ms·
You can get faster than memcmp by rolling your own SSE/AVX compare. memcmp typically has a few branches and instruction cachelines of just trying to verify tha
by CoolGuySteve 8y ago
You can get faster than memcmp by rolling your own SSE/AVX compare.
memcmp typically has a few branches and instruction cachelines of just trying to verify that it's running on aligned memory and checking how aligned (ie 8byte stride vs 64 byte stride). All that can be skipped with a for-loop of intrinsics if you the programmer know alignment characteristics the compiler cannot infer.
- romka2 8y agoThanks, that's good to know. The final version of ListDir calls memcmp only when there are files in the same directory that have identical first 8 characters. Apparently, this is rare enough that memcmp doesn't show on the CPU profile. But if it ever does, I'll look into replacing it with something else.
- CoolGuySteve 8y agoYou should try with the pathological but relatively common case of thousands of files named 'logname.YYYYMMDD.log.gz'
- romka2 8y agoThe final implementation of ListDir has two worse-case scenarios. One is when all files have identical 8 first character but then differ almost immediately. This is bad because I'm telling memcmp that I have 256 bytes of data, which causes it to use the vectorized loop only to exit it on the first iteration. Another is when all files are 255 characters long and differ at the very end. This is bad because string comparisons become very expensive. Even though I'm not showing these benchmarks in the article, this implementation performs very well even in these cases.
- mistrial9 8y agodont you want the compiler to do this for you, with ARCH flags? a bit of foresight and a little checking seems like enough.. excessive ASM is a mistake these days CXXFLAGS -march=sandybridge -mtune=sandybridge
- saagarjha 8y agoThe point is that the compiler does not have enough information to do this automatically.
- mistrial9 8y ago.. meant to say, after rewriting code to avoid (this,that) extra actions, then let the compiler make the optimized code.. not saying "just re-run the old code with CFLAGS thus" ..
- romka2 8y agoWhat part of the code do you expect to get faster with `-march=sandybridge -mtune=sandybridge`? > excessive ASM is a mistake these days There is no ASM in the article.
- kazinator 8y agoThe only downside is that the correctness of your code is relying on alignment that the compiler isn't able to infer, and for which there is deliberately no run-time check.