net native compiler in .net

32
Zagreb, 25. rujna 2014. Hotel Antunović

Upload: bahrudin-hrnjica

Post on 10-Jul-2016

29 views

Category:

Documents


0 download

DESCRIPTION

mobility day session.

TRANSCRIPT

Page 1: Net Native compiler in .NET

Zagreb, 25. rujna 2014.

Hotel Antunović

Page 3: Net Native compiler in .NET

Hvala svim sponzorima i partnerima!

Page 4: Net Native compiler in .NET

Agenda1. Compiling .NET applications2. RyuJIT next generation of JIT Compiler3. .NET Native new way of compilation4. 7 stage of .NET Native compilation

chain5. .NET Native warm up Demo6. Runtime directive in .NET Native7. .NET Native Demo (Using RD, WCF .NET

Native)8. Migration to .NET Native9. Summary

Page 5: Net Native compiler in .NET

Compilation of .NET applications

• Two ways of compilation MSIL to native code

1. .NET Framework just-in-time (JIT) compiler2. .NET Framework Native Image Generator

(Ngen.exe)

Page 6: Net Native compiler in .NET

JIT Compiler• JIT converts MSIL to native code at Run-

Time at the user machine. • Developers build MSIL assemblies• MSIL assemblies run on different

machines/architecture• MSIL assemblies contains managed code• JIT compilation converts IL code in to

native and stores in to memory• When the operation is called for the first

time JIT converts this part of the code in to native.• Some of the code can never be used

Page 7: Net Native compiler in .NET

NGen.exe• It performs the conversion from MSIL to

native code before rather than while running the application• It compiles entire assembly at a time,

rather than a method at a time• It persist the generated code in the Native Image Cache as file on the disk

Page 8: Net Native compiler in .NET

• 64 bit JIT – twice slower than 32JIT• RyuJIT the same code base as 32JIT• RyuJIT for 64 bit architecture• 2x is faster that 32 JIT• RyuJIT app 30% faster than 32JIT

RyuJIT new .NET JIT Compiler

Page 9: Net Native compiler in .NET

• two ways to turn on RyuJIT. • set an environment variable:

COMPLUS_AltJit=*• for entire machine, set the registry key

HKLM\SOFTWARE\Microsoft\.NETFramework\AltJit to the string "*".

• Both methods cause the 64-bit CLR to use RyuJIT instead of JIT64. • And both are temporary settings—

installing RyuJIT doesn’t make any permanent changes to your machine (aside from installing the RyuJIT files in a directory)

Install and setting RYUJIT

Page 10: Net Native compiler in .NET

demo

RyuJIT demoHow to enable using RyuJIT

Page 11: Net Native compiler in .NET

.NET Native Demo

Compiling with .NET Native

Page 12: Net Native compiler in .NET

• Precompilation technology building Windows Store app• .NET apps compiled to IL code. • At Run-Time JIT compiler translates IL

code in to native• At Design Time .NET Native directly

compiles C# apps targets .NET in to native code

.NET Native

Page 13: Net Native compiler in .NET

• Provides superior performance of native apps• Still using C# language and .NET

Framework• Still using debugging and developer

experience of .NET

With .NET Native your apps

Page 14: Net Native compiler in .NET

• Fast execution times• Consistently speedy startup times• Low deployment and update cost• Optimized app memory usage• It transform the way .NET apps are built

and executed• During precompilation .NET dll are statically

linked• Apps runs with local libs of .NET Framework

• Same back end as the C++ compiler, optimized for static precompilation scenarios

Advantages of .NET Native

Page 15: Net Native compiler in .NET

1. Building the MSIL application from source

2. Generating interop marshaling and serialization code

3. Merging the application4. Reducing the application5. Other MSIL transformations6. Compiling from MSIL to Machine

Dependent Intermediate Language (MDIL)

7. Binding from MDIL to native code

7 steps in building .NET native app

Page 16: Net Native compiler in .NET

.NET Native apps already in the store

Page 17: Net Native compiler in .NET

VS experience for .NET Native

1. Enable 2. Debug/test app

Page 18: Net Native compiler in .NET

demo

.NET Native demoCompiling with .NET Native

Page 19: Net Native compiler in .NET

• rd.xml whether designated elements are available for reflection.

Runtime directives of .NET Native

Page 20: Net Native compiler in .NET

1. http://schemas.microsoft.com/netfx/2013/01/metadata namespace

2. root element is the Directives element, can contain:• 0 or more Library elements• 0 or more Application element

3. Application define 1. runtime reflection policy, 2. container for child elements

4. Library element1. is simply a container

Structure of rd directives

Page 21: Net Native compiler in .NET

• degree declares what you're planning on doing with that type or member at runtime,• Dynamic degree means that you're planning

to create objects or invoke methods dynamically• EG. DataContractSerializer degree means that you'll

serialize a class using the DataContractSerializer• Policy how to apply the degree to the type or

member.• Accessibility (public or all-all members will be

applied)• Required (or not) is always included in the final

binary, even though nothing statically needs it.• More info MSDN

Rd.xml

Page 22: Net Native compiler in .NET

• <Type Name="MyNamespace.MyClass" Dynamic="Public" /> - MyClass is available to dynamic reflection

• <Namespace Name="MyNamespace" Serialize="Required All" /> - include all types from namespace and make them Required whether is really need or not.

Rd.xml - Examples

Page 23: Net Native compiler in .NET

• Replace the C# dynamic keyword with strongly typed code• Skip compiled LINQ expressions because they are

interpreted instead of compiled and use reflection too.• Instead of Type.GetType(“MyWellKnownType”),

consider typeof(MyWellKnownType). • Similarly, when you can, construct delegates

directly from methods instead of using MethodInfo.CreateDelegate.• By using CallerMemberName attribute, it cause the

compiler to automatically fill in the string without adding any reflection requirements.

Rd.xml - recomendations

Page 24: Net Native compiler in .NET

demo

.NET Native demoUsing WCF in .NET Native

Page 25: Net Native compiler in .NET

demo

.NET Native demoUsing Run-Time Directives with .NET Native

Page 26: Net Native compiler in .NET

Migration to .NET Native• TypeLoadException– compile-time error• GC.WaitForPendingFinalizers – in UI thread

result deadlock• Don't rely on static class constructor

invocation ordering.• In some cases, not be able to cast

IEnumerable<T>

Page 27: Net Native compiler in .NET

Migration to .NET Native• TypeLoadException exception in CLR.• .NET Native is OK

Page 28: Net Native compiler in .NET

Migration to .NET Native• Dynamic programming differences• You should test all dynamic code paths

thoroughly when porting your app to .NET Native. • Constructs like Type.MakeGenericType and

MethodInfo.MakeGenericMethod can't be determined statically.

• Other reflection-related differences• See at msdn site : Migration to .NET Native

• Unsupported scenarios and APIs• There are several unsupported scenarious,

more info at msdn site

Page 29: Net Native compiler in .NET

Migration to .NET Native• General development differences• Value types –larger than 1 MB aren’t

supported• Value types – can’t have default constructor• Arrays – > 4D are not supported, use Jagged

arrays• Pointers : arrays of pointer are not supported• Serialization: KnownTypeAttribute(String)

attribute isn't supported• Resources: localized resources with the

EventSource class isn't supported• Delegate: Delegate.BeginInvoke and

Delegate.EndInvoke aren't supported. • More info at MSDN Site

Page 30: Net Native compiler in .NET

Summary• Precompilation technology • Superior performance like C++• Productivity Development like C#• Optimized app memory usage• Only Windows Store App for now.

Page 31: Net Native compiler in .NET

pitanja

Page 32: Net Native compiler in .NET

Popunite ankete i osvojite vrijedne nagrade!

Hvala!