Click here to Skip to main content
15,904,823 members
Home / Discussions / Managed C++/CLI
   

Managed C++/CLI

 
GeneralAspireing c++ programmer (doesn't know Jack atm) Question Pin
Caden1-Mar-03 9:56
Caden1-Mar-03 9:56 
GeneralRe: Aspireing c++ programmer (doesn't know Jack atm) Question Pin
Paul Selormey2-Mar-03 23:47
Paul Selormey2-Mar-03 23:47 
GeneralRe: Aspireing c++ programmer (doesn't know Jack atm) Question Pin
Caden3-Mar-03 8:06
Caden3-Mar-03 8:06 
GeneralCreating objects on stack Pin
VizOne28-Feb-03 6:05
VizOne28-Feb-03 6:05 
GeneralRe: Creating objects on stack Pin
Jeff J1-Mar-03 16:26
Jeff J1-Mar-03 16:26 
GeneralRe: Creating objects on stack Pin
Paul Selormey2-Mar-03 23:40
Paul Selormey2-Mar-03 23:40 
GeneralRe: Creating objects on stack Pin
Jeff J3-Mar-03 11:49
Jeff J3-Mar-03 11:49 
GeneralRe: Creating objects on stack Pin
Jeff J3-Mar-03 11:43
Jeff J3-Mar-03 11:43 
Hello Andre (and Paul),

I played with the code you posted and some other stuff, but could not find a way to optimise the MC++ MSIL any further, but that may not be as much of a problem as we first thought. BTW, I can now see your MSIL was taken from optimised compiles.

Regarding your earlier question of whether the C# struct was being allocated on the stack or on the heap, I think I judged too quickly based on the MSIL. CIL documentation does not say much about newobj being applied to value types, however it "can" be used to allocate on the stack. It also says that it is rarely done this way. The blanks leave questions, but my guess is that newobj is itself optimised to return value types/structs as efficiently as possible, and in this unusual case, not on the heap. CLI instructions are not like low-level assembler op-codes, but more like convenient groups of instructions (actually, so are some op-codes).

Frustrated with MSIL, I moved to assembler, and found the following:

static MCppStruct GetStack()              | public static CshStruct GetNew()
{ return MCppStruct(1,2,3); }             | { return new CshStruct(1, 2, 3); }
-------------------------------------------------------------------------------------
00  push  ebp                             | 00  push  ebp
01  mov   ebp,esp                         | 01  mov   ebp,esp
03  sub   esp,10h                         | 03  sub   esp,10h
06  push  edi                             | 06  push  edi
07  push  esi                             | 07  push  esi
08  push  ebx                             | 08  push  ebx
09  <FONT color="green">mov   ebx,ecx</FONT>                         |  
0b  <FONT color=blue>lea   edi,[ebp-10h]</FONT>                   |  
0e  xor   eax,eax                         | 09  xor   eax,eax
10  <FONT color=blue>stos  dword ptr [edi]</FONT>                 | 0b  <FONT color=blue>mov   dword ptr [ebp-10h],eax</FONT>
11  <FONT color=blue>stos  dword ptr [edi]</FONT>                 | 0e  <FONT color=blue>mov   dword ptr [ebp-0Ch],eax</FONT>
12  <FONT color=blue>stos  dword ptr [edi]</FONT>                 | 11  <FONT color=blue>mov   dword ptr [ebp-8],eax</FONT>
                                          | 14  <FONT color="green">mov   ebx,ecx</FONT>
13  push  2                               | 16  push  2
15  push  3                               | 18  push  3
17  lea   ecx,[ebp-10h]                   | 1a  lea   ecx,[ebp-10h]
1a  mov   edx,1                           | 1d  mov   edx,1
1f  call  dword ptr ds:[00825230h]        | 22  call  dword ptr ds:[008151F0h]
25  mov   edi,ebx                         | 28  mov   edi,ebx
27  lea   esi,[ebp-10h]                   | 2a  lea   esi,[ebp-10h]
2a  movs  dword ptr [edi],dword ptr [esi] | 2d  movs  dword ptr [edi],dword ptr [esi]
2b  movs  dword ptr [edi],dword ptr [esi] | 2e  movs  dword ptr [edi],dword ptr [esi]
2c  movs  dword ptr [edi],dword ptr [esi] | 2f  movs  dword ptr [edi],dword ptr [esi]
2d  pop   ebx                             | 30  pop   ebx 
2e  pop   esi                             | 31  pop   esi 
2f  pop   edi                             | 32  pop   edi
30  mov   esp,ebp                         | 33  mov   esp,ebp 
32  pop   ebp                             | 35  pop   ebp
33  ret                                   | 36  ret
-------------------------------------------------------------------------------------

Most instructions are the same, except for those in blue. For the C# code, the JIT individually moves zero (in EAX) into the ints, whereas the MC++ stuff translates to using store-string (which requires loading the first address into EDI before the calls). They accomplish the same thing, and I don't recall that stos is slower than mov, so I'm stumped here. Best I can tell, performance should be about the same. The constructors were exactly the same. I don't see heap alloc here. So much for reading MSIL.

Is it possible that there is a difference in the code you used to test the performance? Maybe forgot to switch the MC++ to release mode as I originally did? Who knows, maybe the JIT settings used when debuggin' C# code, hides further optimisations done outside the IDE. I am assuming the disassembly view is accurate. Anyway, this has been a lesson for me that different byte codes can produce similar asm. Sorry for the short post Wink | ;)

Regards,
Jeff
GeneralRe: Creating objects on stack Pin
Paul Selormey3-Mar-03 23:47
Paul Selormey3-Mar-03 23:47 
GeneralRe: Creating objects on stack Pin
VizOne10-Mar-03 1:45
VizOne10-Mar-03 1:45 
GeneralRe: Creating objects on stack Pin
Paul Selormey9-Mar-03 17:07
Paul Selormey9-Mar-03 17:07 
GeneralMixins and __gc Pin
-=jarl=-27-Feb-03 3:59
-=jarl=-27-Feb-03 3:59 
GeneralRe: Mixins and __gc Pin
Paul Selormey27-Feb-03 22:11
Paul Selormey27-Feb-03 22:11 
GeneralRe: Mixins and __gc Pin
-=jarl=-6-Mar-03 1:14
-=jarl=-6-Mar-03 1:14 
GeneralHandles and pointers... Pin
-=jarl=-26-Feb-03 7:28
-=jarl=-26-Feb-03 7:28 
GeneralRe: Handles and pointers... Pin
Jeff J26-Feb-03 16:46
Jeff J26-Feb-03 16:46 
GeneralRe: Handles and pointers... Pin
-=jarl=-26-Feb-03 23:19
-=jarl=-26-Feb-03 23:19 
GeneralManaged and Unmanaged structures Pin
-=jarl=-26-Feb-03 6:55
-=jarl=-26-Feb-03 6:55 
GeneralRe: Managed and Unmanaged structures Pin
-=jarl=-26-Feb-03 6:58
-=jarl=-26-Feb-03 6:58 
GeneralRe: Managed and Unmanaged structures Pin
Paul Selormey26-Feb-03 13:51
Paul Selormey26-Feb-03 13:51 
GeneralRe: Managed and Unmanaged structures Pin
-=jarl=-26-Feb-03 23:16
-=jarl=-26-Feb-03 23:16 
GeneralRe: Managed and Unmanaged structures Pin
Paul Selormey27-Feb-03 0:20
Paul Selormey27-Feb-03 0:20 
GeneralRe: Managed and Unmanaged structures Pin
-=jarl=-27-Feb-03 0:22
-=jarl=-27-Feb-03 0:22 
GeneralManaged/Unmanaged: pinning ptr madness Pin
VizOne25-Feb-03 22:04
VizOne25-Feb-03 22:04 
GeneralRe: Managed/Unmanaged: pinning ptr madness Pin
Jeff J26-Feb-03 16:24
Jeff J26-Feb-03 16:24 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.