Pre-compiling to native code with Mono

For advanced users and developer audience, using Mono runtime on Linux!

Although .NET runtimes in general are pretty fast, one can pre-compile a .NET assembly (.exe, .dll) to native code.
Here I am using ScoobyRom 0.6.0 R1 build (125 KiB) as demo assembly.

mono --aot=full --optimize=all ScoobyRom.exe
Created “ScoobyRom.exe.so” (685 KiB)

Since I run Linux x64, Linux file command tells it contains 64 bit native code:
file ScoobyRom.exe.so

ScoobyRom.exe.so: ELF 64-bit LSB shared object, x86-64, version 1 (SYSV), dynamically linked, not stripped

Generate native disassembly into text file:
objdump -d ScoobyRom.exe.so --disassembler-options=intel --no-show-raw-insn > ScoobyRom.exe.asm

ScoobyRom.exe.asm contains >49,000 lines so use a good editor (or less) to view it.
Snippet from the file showing a single method (from MainWindow.cs source):


0000000000002bf0 <MainWindow_OnVisualizationAction_object_System_EventArgs>:
    2bf0:	sub    rsp,0x28
    2bf4:	mov    QWORD PTR [rsp],r14
    2bf8:	mov    QWORD PTR [rsp+0x8],r15
    2bfd:	mov    r15,rdi
    2c00:	mov    rax,QWORD PTR [r15+0x58]
    2c04:	mov    rcx,rax
    2c07:	cmp    DWORD PTR [rcx],0x0
    2c0a:	movzx  eax,BYTE PTR [rax+0x50]
    2c0e:	test   eax,eax
    2c10:	je     2c7e <MainWindow_OnVisualizationAction_object_System_EventArgs+0x8e>
    2c16:	mov    rdi,r15
    2c19:	call   2460 <MainWindow_get_CurrentUI>
    2c1e:	mov    r14,rax
    2c21:	dec    r14d
    2c24:	cmp    r14d,0x2
    2c28:	jae    2c7e <MainWindow_OnVisualizationAction_object_System_EventArgs+0x8e>
    2c2e:	mov    rax,r14
    2c31:	shl    eax,0x3
    2c34:	mov    ecx,eax
    2c36:	mov    rax,QWORD PTR [rip+0x449db]        # 47618 <__bss_start+0x160>
    2c3d:	add    rax,rcx
    2c40:	mov    rax,QWORD PTR [rax]
    2c43:	jmp    rax
    2c45:	mov    rax,QWORD PTR [r15+0x78]
    2c49:	mov    rdi,rax
    2c4c:	cmp    DWORD PTR [rax],0x0
    2c4f:	call   2ee44 <plt_ScoobyRom_DataView2DGtk_get_Selected>
    2c54:	mov    rsi,rax
    2c57:	mov    rdi,r15
    2c5a:	call   2b10 <MainWindow_Show2D_Subaru_Tables_Table2D>
    2c5f:	jmp    2c7e <MainWindow_OnVisualizationAction_object_System_EventArgs+0x8e>
    2c64:	mov    rax,QWORD PTR [r15+0x68]
    2c68:	mov    rdi,rax
    2c6b:	cmp    DWORD PTR [rax],0x0
    2c6e:	call   2ee4e <plt_ScoobyRom_DataView3DGtk_get_Selected>
    2c73:	mov    rsi,rax
    2c76:	mov    rdi,r15
    2c79:	call   2ee58 <plt_MainWindow_Show3D_Subaru_Tables_Table3D>
    2c7e:	mov    r14,QWORD PTR [rsp]
    2c82:	mov    r15,QWORD PTR [rsp+0x8]
    2c87:	add    rsp,0x28
    2c8b:	ret

Lines containing r…-registers definitely are x64-only Intel/AMD-CPU instructions.
http://en.wikipedia.org/wiki/X86_64
Seems like Mono on Linux x64 machine ignores the .NET assembly 32 bit flag. That’s ok for me, even though I’ve got gtk (x64) and gtk-32bit packages installed on my box.
Mono picks up pre-compiled files automatically. I don’t notice big performance improvements running ScoobyRom though, many things depend on UI libs and their native performance anyway.
Of course you can pre-compile and inspect other .NET assemblies the same way (LibScoobyRom.dll, NPlot.dll).
gnuplot is fully separated, I certainly won’t bother trying to optimize its large codebase.

Advertisements

One response to “Pre-compiling to native code with Mono

  1. Pingback: ASP.NET , PHP , WEB DESIGN | Размисли

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s