Permalink
Browse files

figured out why signed is faster for us w.r.t. #39

  • Loading branch information...
1 parent f32932f commit 11e5dd8049225cb81352178517d55315b92705cf @kevin-montrose committed Aug 11, 2014
Showing with 30 additions and 1 deletion.
  1. +30 −1 Jil/Serialize/Methods.cs
@@ -973,10 +973,39 @@ static void _CustomWriteIntUnrollSigned(TextWriter writer, int num, char[] buffe
number = num;
}
- TwoDigits digits;
+ // Why signed integers?
+ // Earlier versions of this code used unsigned integers,
+ // but it turns out that's not ideal and here's why.
+ //
+ // The signed version of the relevant code gets JIT'd down to:
+ // instr operands latency/throughput (approx. worst case; Haswell)
+ // ========================================================================================
+ // mov ecx,### 2 / 0.5
+ // cdq 1 / -
+ // idiv eax,ecx 29 / 11
+ // mov ecx,### 2 / 0.5
+ // cdq 1 / -
+ // idiv eax,ecx 29 / 11
+ // movsx edx,dl - / 0.5
+ //
+ // The unsigned version gets JIT'd down to:
+ // instr operands latency/throughput (approx. worst case; Haswell)
+ // ========================================================================================
+ // mov ecx,### 2 / 0.5
+ // xor edx,edx 1 / 0.25
+ // div eax,ecx 29 / 11
+ // mov ecx,### 2 / 0.5
+ // xor edx,edx 1 / 0.25
+ // div eax,ecx 29 / 11
+ // and edx,### 1 / 0.25
+ //
+ // In theory div (usigned division) is faster tha idiv, and it probably is *but* cdq + cdq + movsx is
+ // faster than xor + xor + and; in practice it's fast *enough* to make up the difference.
int numLen;
sbyte ix;
+ TwoDigits digits;
+
// unroll the loop
if (number < 10)
{

0 comments on commit 11e5dd8

Please sign in to comment.