4 ms·
All things being equal, smaller code is better[1]. Of course, if things are not equal, and loop is slower than building it yourself, then build it yourself exce
by toast0 6y ago
All things being equal, smaller code is better[1]. Of course, if things are not equal, and loop is slower than building it yourself, then build it yourself except where you're size constrained.
Also, you really have to benchmark these things to know for sure, and it can change between processor generations and the author couldn't have tested Zen 3 in 2003; and we'll have a hard time testing Pentium M in 2021.
[1] Memory bandwidth is limited, cache sizes are limited, even though both are huge. Otoh, the quantum of useful savings is a cache line, 64 bytes on modern amd64 if I'm reading correctly. If you save some bytes, but not a cache line, it probably doesn't help anything. If you only save one cacheline, the difference will likely be hard to measure, but could be measurable depending on circumstances.
- chrisseaton 6y agoWhen Intel themselves don't recommend using the instruction in their optimisation manual... I think you can take that as a good indicator that it isn't going to be fast even before you start benchmarking.
- mhh__ 6y agoCachelines are 64 bytes on most architectures these days. Zen 3(?) has some other requirements along these lines but I don't remember what exactly.
- 38491239821398 6y agoInterestingly, the M1 (and probably other Apple Silicon CPUs) is an exception: % sysctl hw.cachelinesize hw.cachelinesize: 128
- zeusk 6y agoThat's an ARM thing afaik, the snapdragons I work with also use 128 byte cacheline.
- 38491239821398 6y agoA quick search suggests ARM's own designs tend to use 64-byte cache lines, but perhaps this is something the SoC vendors can customize? AFAIK, Qualcomm's recent parts use ARM-designed cores.
- zeusk 6y agoActually you're right, recent designs use 64 byte because of things like this https://www.mono-project.com/news/2016/09/12/arm64-icache/ https://www.mono-project.com/news/2016/09/12/arm64-icache/ Perhaps, either Apple is fooling the userspace software or is using 128b cachelines for their little cores too.
- 38491239821398 6y agoIt looks like sysctl shows the cache info for the little cores, at least by default: hw.cachelinesize: 128 hw.l1icachesize: 131072 hw.l1dcachesize: 65536 The reported L1I and L1D matches up with the little cores. Also very interesting: if you run sysctl under Rosetta, it reports 64 bytes: % arch -x86_64 /bin/bash -c "sysctl hw.cachelinesize" hw.cachelinesize: 64 This information is exposed specifically so that app developers can optimize for the cache line size in use. I very much doubt Apple would report a false number. As for Samsung: my guess is that Apple has been using 128-byte cache lines for a long time, and Samsung simply copied that because Apple did it, without understanding the implications. That would be totally in line with what I expect from them.
- zeusk 6y ago> This information is exposed specifically so that app developers can optimize for the cache line size in use. I very much doubt Apple would report a false number. No, this number is used for compatibility reasons. OS is free to pull shenanigan behind apps to take care of CPU cache line size changing underneath them (like using a larger size so it'll align on everything; 128b aligned is 64b aligned too).
- jeffbee 6y agoHyperoptimizing for code size can be counterproductive. If you pack several branches or branch targets into the same cache line, the CPU might lose its ability to predict them. Making your code bigger (inserting NOP to align branch targets, for example) will often be worth the tradeoff in pure size.
- agalunar 6y agoI don't think this is true, though – it'd strike me as utterly bizarre if an architect decided to use the same index bits for the branch target buffer as the cache. For the cache, the low bits of the address are the offset, the next bits are the index, and the remaining bits are the tag. Branch target buffers have no offset, but instead (as I understand it) use those low bits for an index (perhaps more or fewer; it needn't be the same number as the cache) and the remaining bits as a tag. I'm sure there are additional, more advanced techniques, but I also have a hard time believing they wouldn't use the lower bits of the address.
- jeffbee 6y agoWell, maybe not an L1i cache line, but there are a LOT of caches in one of these CPUs and the optimization manual describes a plethora of conditions under which the caches will stop working. One of these conditions is if you've managed to pack four branches into 16 bytes of code. Another is if 32 bytes of code decodes to uops that don't fit in the DSB. Intel also warns "If there are many taken branches from one 32-byte chunk into another, it impacts the micro-ops being delivered per cycle."
- agalunar 6y agoOh, that's interesting. Looks like I have some reading to do! Thanks for replying.