2014-08-27 12 views
12

EDITC# 3 vergleichen Bytefeld

cmp Die Anweisungen, die nicht verwendet werden, sind eine Nullpointer verursachen.

What are these strange cmp [ecx], ecx instructions doing in my C# code?

ORIGINAL POST (noch Änderungen unten)

Ich versuche, den Weg der JIT-Compiler kompiliert den Code zu verstehen.

Im Speicher habe ich ein Feld mit 3 Zeichen. In C++ zwei solche Felder vergleichen Ich kann dies tun:

return ((*(DWORD*)p) & 0xFFFFFF00) == ((*(DWORD*)q) & 0xFFFFFF00); 

MSVC 2010 wird in etwa so erzeugen (aus dem Gedächtnis):

1 mov   edx,dword ptr [rsp+8] 
2 and   edx,0FFFFFF00h 
3 mov   ecx,dword ptr [rsp] 
4 and   ecx,0FFFFFF00h 
5 cmp   edx,ecx 

in C#, ich versuche, herauszufinden, wie man so nah wie ich kann. Wir haben Aufzeichnungen, die aus vielen 1,2,3,4,5,6,7,8 Byte-Feldern bestehen. Ich habe viele verschiedene Wege in C# getestet, um eine größere Struktur zu erstellen, die einen Datensatz mit kleineren Strukturen dieser Größe darstellt. Ich bin nicht mit dem Assembler-Code zufrieden. Gerade jetzt spiele ich mit so etwas:

[StructLayout(LayoutKind.Sequential, Size = 3)] 
public unsafe struct KLF3 
{ 
    public fixed byte Field[3]; 
    public bool Equals(ref KLF3 r) 
    { 
     fixed (byte* p = Field, q = r.Field) 
     { 
      return ((*(UInt32*)p) & 0xFFFFFF00) == ((*(UInt32*)q) & 0xFFFFFF00); 
     } 
    } 
} 

Aber ich habe zwei Probleme. Problem ist der Compiler eine Menge nutzloser suchen Code generiert:

  fixed (byte* p = Field, q = r.Field) 
1 sub   rsp,18h 
2 mov   qword ptr [rsp+8],0 
3 mov   qword ptr [rsp],0 
4 cmp   byte ptr [rcx],0 
5 mov   qword ptr [rsp+8],rcx 
6 cmp   byte ptr [rdx],0 
7 mov   qword ptr [rsp],rdx 
       return ((*(UInt32*)p) & 0xFFFFFF00) == ((*(UInt32*)q) & 0xFFFFFF00); 
8 mov   rax,qword ptr [rsp+8] 
9 mov   edx,dword ptr [rax] 
10 and   edx,0FFFFFF00h 
11 mov   rax,qword ptr [rsp] 
12 mov   ecx,dword ptr [rax] 
13 and   ecx,0FFFFFF00h 
14 xor   eax,eax 
15 cmp   edx,ecx 
16 sete  al 
17 add   rsp,18h 
18 ret 

Linien 2,3,4,5,6,7 scheinen nutzlos, da wir nur die Register RCX und rdx nutzen könnten und nicht brauchen Linie 8 und Zeile 11. Die Zeilen 4 und 6 scheinen nutzlos, da nichts das Ergebnis des cmp verwendet. Ich sehe viele dieser nutzlosen cmps in .net Code.

Problem zwei ist, kann ich nicht den Compiler, um die Equals-Funktion inline zu bekommen. Tatsächlich fällt es mir schwer, etwas inline zu sehen.

Irgendwelche Tipps, um das kompilieren besser zu bekommen? Ich benutze Visual Studio 2010 und .net Version 4. Ich arbeite daran, 4.5 installiert und Visual Studio 2013, aber das könnte noch ein paar Tage dauern.

EDIT

Also habe ich versucht, ein paar Stellvertreter

Damit wird eine bessere Code suchen, aber immer noch ein bisschen lang:

[StructLayout(LayoutKind.Sequential, Size = 3, Pack = 1)] 
public unsafe struct KLF31 
{ 
    public UInt16 pos0_1; 
    public byte pos2; 
    public bool Equals(ref KLF31 r) 
    { 
     return pos0_1 == r.pos0_1 && pos2 == r.pos2; 
    } 
} 
      return pos0_1 == r.pos0_1 && pos2 == r.pos2; 
00000000 mov   r8,rdx 
00000003 mov   rdx,rcx 
00000006 movzx  ecx,word ptr [rdx] 
00000009 movzx  eax,word ptr [r8] 
0000000d cmp   ecx,eax 
0000000f jne   0000000000000025 
00000011 movzx  ecx,byte ptr [rdx+2] 
00000015 movzx  eax,byte ptr [r8+2] 
0000001a xor   edx,edx 
0000001c cmp   ecx,eax 
0000001e sete  dl 
00000021 mov   al,dl 
00000023 jmp   0000000000000027 
00000025 xor   eax,eax 
00000027 rep ret 

Diese ist ziemlich mager, mit Ausnahme der Struktur Größe ist 4 Bytes statt 3.

[StructLayout(LayoutKind.Explicit, Size = 3, Pack = 1)] 
public unsafe struct KLF33 
{ 
    [FieldOffset(0)] public UInt32 pos0_3; 
    public bool Equals(ref KLF33 r) 
    { 
     return (pos0_3 & 0xFFFFFF00) == (r.pos0_3 & 0xFFFFFF00); 
    } 
} 
      return (pos0_3 & 0xFFFFFF00) == (r.pos0_3 & 0xFFFFFF00); 
00000000 mov   rax,rdx 
00000003 mov   edx,dword ptr [rcx] 
00000005 and   edx,0FFFFFF00h 
0000000b mov   ecx,dword ptr [rax] 
0000000d and   ecx,0FFFFFF00h 
00000013 xor   eax,eax 
00000015 cmp   edx,ecx 
00000017 sete  al 
0000001a ret 

Dieser sieht genauso aus wie der beschissene Fest char-Array, wie erwartet:

[StructLayout(LayoutKind.Sequential, Size = 3, Pack = 1)] 
public unsafe struct KLF34 
{ 
    public byte pos0, pos1, pos2; 
    public bool Equals(ref KLF34 r) 
    { 
     fixed (byte* p = &pos0, q = &r.pos0) 
     { 
      return ((*(UInt32*)p) & 0xFFFFFF00) == ((*(UInt32*)q) & 0xFFFFFF00); 
     } 
    } 
} 
      fixed (byte* p = &pos0, q = &r.pos0) 
00000000 sub   rsp,18h 
00000004 mov   qword ptr [rsp+8],0 
0000000d mov   qword ptr [rsp],0 
00000015 cmp   byte ptr [rcx],0 
00000018 mov   qword ptr [rsp+8],rcx 
0000001d cmp   byte ptr [rdx],0 
00000020 mov   qword ptr [rsp],rdx 
      { 
       return ((*(UInt32*)p) & 0xFFFFFF00) == ((*(UInt32*)q) & 0xFFFFFF00); 
00000024 mov   rax,qword ptr [rsp+8] 
00000029 mov   edx,dword ptr [rax] 
0000002b and   edx,0FFFFFF00h 
00000031 mov   rax,qword ptr [rsp] 
00000035 mov   ecx,dword ptr [rax] 
00000037 and   ecx,0FFFFFF00h 
0000003d xor   eax,eax 
0000003f cmp   edx,ecx 
00000041 sete  al 
00000044 add   rsp,18h 
00000048 ret 

EDIT

Als Antwort auf Hans, hier ist Beispielcode.

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Runtime.CompilerServices; 
using System.Reflection; 
using System.Runtime.InteropServices; 

namespace ConsoleApplication2 
{ 
    [StructLayout(LayoutKind.Sequential, Size = 3)] 
    public unsafe struct KLF30 
    { 
     public fixed byte Field[3]; 
     public bool Equals(ref KLF30 r) 
     { 
      fixed (byte* p = Field, q = r.Field) 
      { 
       return ((*(UInt32*)p) & 0xFFFFFF00) == ((*(UInt32*)q) & 0xFFFFFF00); 
      } 
     } 
     public bool Equals1(ref KLF30 r) 
     { 
      fixed (byte* p = Field, q = r.Field) 
      { 
       return p[0] == q[0] && p[1] == q[1] && p[2] == q[2]; 
      } 
     } 
     public bool Equals2(ref KLF30 r) 
     { 
      fixed (byte* p = Field, q = r.Field) 
      { 
       return p[0] == q[0] && p[1] == q[1] && p[2] == q[2]; 
      } 
     } 
    } 

    [StructLayout(LayoutKind.Sequential, Size = 3, Pack = 1)] 
    public unsafe struct KLF31 
    { 
     public UInt16 pos0_1; 
     public byte pos2; 
     public bool Equals(ref KLF31 r) 
     { 
      return pos0_1 == r.pos0_1 && pos2 == r.pos2; 
     } 
    } 

    [StructLayout(LayoutKind.Sequential, Size = 3, Pack = 1)] 
    public unsafe struct KLF32 
    { 
     public fixed byte Field[3]; 
     public bool Equals(ref KLF32 r) 
     { 
      fixed (byte* p = Field, q = r.Field) 
      { 
       return EqualsImpl(p, q); 
      } 
     } 
     private bool EqualsImpl(byte* p, byte* q) 
     { 
      return (*(uint*)p & 0xffffff) == (*(uint*)q & 0xffffff); 
     } 
    } 

    [StructLayout(LayoutKind.Explicit, Size = 3, Pack = 1)] 
    public unsafe struct KLF33 
    { 
     [FieldOffset(0)] 
     public UInt32 pos0_3; 
     public bool Equals(ref KLF33 r) 
     { 
      return (pos0_3 & 0xFFFFFF00) == (r.pos0_3 & 0xFFFFFF00); 
     } 
    } 

    [StructLayout(LayoutKind.Sequential, Size = 3, Pack = 1)] 
    public unsafe struct KLF34 
    { 
     public byte pos0, pos1, pos2; 
     public bool Equals(ref KLF34 r) 
     { 
      fixed (byte* p = &pos0, q = &r.pos0) 
      { 
       return ((*(UInt32*)p) & 0xFFFFFF00) == ((*(UInt32*)q) & 0xFFFFFF00); 
      } 
     } 
    } 

    [StructLayout(LayoutKind.Explicit)] 
    public struct Klf 
    { 
     [FieldOffset(0)] public char pos0; 
     [FieldOffset(1)] public char pos1; 
     [FieldOffset(2)] public char pos2; 
     [FieldOffset(3)] public char pos3; 
     [FieldOffset(4)] public char pos4; 
     [FieldOffset(5)] public char pos5; 
     [FieldOffset(6)] public char pos6; 
     [FieldOffset(7)] public char pos7; 

     [FieldOffset(0)] public UInt16 pos0_1; 
     [FieldOffset(2)] public UInt16 pos2_3; 
     [FieldOffset(4)] public UInt16 pos4_5; 
     [FieldOffset(6)] public UInt16 pos6_7; 

     [FieldOffset(0)] public UInt32 pos0_3; 
     [FieldOffset(4)] public UInt32 pos4_7; 

     [FieldOffset(0)] public UInt64 pos0_7; 
    } 

    [StructLayout(LayoutKind.Sequential, Size = 3)] 
    public unsafe struct KLF35 
    { 
     public Klf Field; 
     public bool Equals(ref KLF35 r) 
     { 
      return (Field.pos0_3 & 0xFFFFFF00) == (r.Field.pos0_3 & 0xFFFFFF00); 
     } 
    } 

    public unsafe class KlrAAFI 
    { 
     [StructLayout(LayoutKind.Sequential, Pack = 1)] 
     public struct _AAFI 
     { 
      public KLF30 AirlineCxrCode0; 
      public KLF31 AirlineCxrCode1; 
      public KLF32 AirlineCxrCode2; 
      public KLF33 AirlineCxrCode3; 
      public KLF34 AirlineCxrCode4; 
      public KLF35 AirlineCxrCode5; 
     } 

     public KlrAAFI(byte* pData) 
     { 
      Data = (_AAFI*)pData; 
     } 
     public _AAFI* Data; 
     public int Size = sizeof(_AAFI); 
    } 

    class Program 
    { 
     static unsafe void Main(string[] args) 
     { 
      byte* foo = stackalloc byte[256]; 
      var a1 = new KlrAAFI(foo); 
      var a2 = new KlrAAFI(foo); 
      var p1 = a1.Data; 
      var p2 = a2.Data; 
      //bool f01= p1->AirlineCxrCode0.Equals (ref p2->AirlineCxrCode0); 
      //bool f02= p1->AirlineCxrCode0.Equals1(ref p2->AirlineCxrCode0); 
      //bool f03= p1->AirlineCxrCode0.Equals2(ref p2->AirlineCxrCode0); 
      //bool f1 = p1->AirlineCxrCode1.Equals (ref p2->AirlineCxrCode1); 
      bool f2 = p1->AirlineCxrCode2.Equals (ref p2->AirlineCxrCode2); 
      //bool f3 = p1->AirlineCxrCode3.Equals (ref p2->AirlineCxrCode3); 
      //bool f4 = p1->AirlineCxrCode4.Equals (ref p2->AirlineCxrCode4); 
      //bool f5 = p1->AirlineCxrCode5.Equals (ref p2->AirlineCxrCode5); 
      //int q = f01 | f02 | f03 | f1 | f2 | f3 | f4 ? 0 : 1; 
      int q = f2 ? 0 : 1; 
      Console.WriteLine("{0} {1} {2} {3} {4} {5}", 
       sizeof(KLF30), sizeof(KLF31), sizeof(KLF32), sizeof(KLF33), sizeof(KLF34), sizeof(KLF35)); 
      Console.WriteLine("{0}", q); 
     } 
    } 
} 

Wenn ich kompilieren, die mit allen aber f2 auf Kommentar, ich diese:

  var p1 = a1.Data; 
0000007b mov   rax,qword ptr [rdi+8] 
      var p2 = a2.Data; 
0000007f mov   rcx,qword ptr [rbx+8] 
      bool f2 = p1->AirlineCxrCode2.Equals (ref p2->AirlineCxrCode2); 
00000083 cmp   byte ptr [rax],0 
00000086 add   rax,10h 
0000008c cmp   byte ptr [rcx],0 
0000008f add   rcx,10h 
00000093 xor   edx,edx 
00000095 mov   qword ptr [rbp],rdx 
00000099 mov   qword ptr [rbp+8],rdx 
0000009d cmp   byte ptr [rax],0 
000000a0 mov   qword ptr [rbp],rax 
000000a4 cmp   byte ptr [rcx],0 
000000a7 mov   qword ptr [rbp+8],rcx 
000000ab mov   rax,qword ptr [rbp] 
000000af mov   rcx,qword ptr [rbp+8] 
000000b3 mov   edx,dword ptr [rax] 
000000b5 and   edx,0FFFFFFh 
000000bb mov   ecx,dword ptr [rcx] 
000000bd and   ecx,0FFFFFFh 
000000c3 xor   eax,eax 
000000c5 cmp   edx,ecx 
000000c7 sete  al 
000000ca movzx  ecx,al 
000000cd movzx  eax,cl 

Wenn Sie genau an der Montage aussehen wird inlined wie Hans angegeben, aber die meisten dieser asm doesn tu nichts. Sehen Sie sich alle nutzlosen Cmp-Anweisungen vor 000000c5 an. Schau dir an, wie oft es den gleichen Wert in rbp und rbp + 8 hinein und heraus bewegt. Vielleicht verstehe ich den Nutzen davon nicht.

, wenn Sie alles außer f1 Kommentar aus, bekomme ich diese:

  var p1 = a1.Data; 
00000071 mov   rdx,qword ptr [rdi+8] 
      var p2 = a2.Data; 
00000075 mov   r8,qword ptr [rbx+8] 
      bool f1 = p1->AirlineCxrCode1.Equals (ref p2->AirlineCxrCode1); 
00000079 cmp   byte ptr [rdx],0 
0000007c cmp   byte ptr [r8],0 
00000080 movzx  ecx,word ptr [rdx+8] 
00000084 movzx  eax,word ptr [r8+8] 
00000089 cmp   ecx,eax 
0000008b jne   00000000000000A2 
0000008d movzx  ecx,byte ptr [rdx+0Ah] 
00000091 movzx  eax,byte ptr [r8+0Ah] 
00000096 xor   edx,edx 
00000098 cmp   ecx,eax 
0000009a sete  dl 
0000009d movzx  eax,dl 
000000a0 jmp   00000000000000A4 
000000a2 xor   eax,eax 

die noch nutzlos cmp instr 79, 7c, aber viel weniger Overhead hat.

Scheint, dass behoben in diesem Fall eine Menge (nutzlos?) ASM generiert.

+2

Mit .Net 4.5 können Sie [MethodImplOptions.AggressiveInlining] (http://msdn.microsoft.com/en-us/library/system.runtime.compilerservices.methodimploptions.aspx) in einem [MethodImpl] (http : //msdn.microsoft.com/en-us/library/system.runtime.compilerservices.methodimplattribute.aspx) Attribut. – Blorgbeard

+0

Auch dies [http://stackoverflow.com/questions/12397108/disassembly-view-of-c-sharp-64-bit-release-code-is-75-longer-than-32-bit-debug ? rq = 1) Frage kann von Interesse sein. – Blorgbeard

+0

@Blorg Ich versuche, das installiert zu bekommen. Ich weiß nicht, ob das alles klären wird. – johnnycrash

Antwort

8

Ja, der Optimizer flattert bei diesem Code, er ist nicht sehr glücklich über das Pinning. Sie können es über den Kopf Whack durch ein separates Verfahren zu schreiben:

public bool Equals(ref KLF3 r) { 
     fixed (byte* p = Field, q = r.Field) { 
      return EqualsImpl(p, q); 
     } 
    } 
    private unsafe bool EqualsImpl(byte* p, byte* q) { 
     return (*(uint*)p & 0xffffff) == (*(uint*)q & 0xffffff); 
    } 

Was es wisens bis zu:

0000006b mov   rax,qword ptr [rsp+20h] 
00000070 mov   rcx,qword ptr [rsp+28h] 
00000075 mov   edx,dword ptr [rax] 
00000077 and   edx,0FFFFFFh 
0000007d mov   ecx,dword ptr [rcx] 
0000007f and   ecx,0FFFFFFh 
00000085 xor   eax,eax 
00000087 cmp   edx,ecx 
00000089 sete  al 
0000008c movzx  ecx,al 
0000008f movzx  ecx,cl 

inline in der Anrufer Methode generiert. Außerdem ist es sehr wichtig, dass Sie eine Version profilieren, die das Argument ref nicht übergibt, schneller sein sollte und Ihre aktuelle Version zu viele Unfälle verursacht. Ich habe Ihre Bitmasken geändert, sie sollten auf einer Little-Endian-Maschine 0xffffff sein.

+0

Ich dachte fast, du hättest es, außer ich sehe das gleiche extra asm in der asm von der äußeren Equals(): fixed (byte * p = Feld, q = r.Field) 00000000 sub rsp, 18h 00000004 mov qword ptr [rsp + 8], 0 0000000d mov qword PTR [rsp], 0 00000015 cmp byte ptr [RCX], 0 00.000.018 mov qword PTR [rsp + 8], rcx 0000001d cmp byte ptr [RDX], 0 00000020 mov qword ptr [rsp], rdx – johnnycrash

+0

Nein, es wurde inline. Ich kann natürlich nicht versprechen, dass es in Ihrem Fall nicht ein vollständiges Beispiel geben wird. –

+0

Ich sehe die genau gleiche Inlined-Asm, die Sie für EqualsImpl haben, aber es gibt auch Asm in Equals von der festen() Zeile. Siehst du das? – johnnycrash