net native compiler in .net
DESCRIPTION
mobility day session.TRANSCRIPT
Zagreb, 25. rujna 2014.
Hotel Antunović
Zagreb, 25. rujna 2014.
C# produktivnost + C++ performanse
= .NET NativeBahrudin Hrnjica, MVP C#
daenet
Hvala svim sponzorima i partnerima!
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
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)
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
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
• 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
• 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
demo
RyuJIT demoHow to enable using RyuJIT
.NET Native Demo
Compiling with .NET Native
• 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
• 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
• 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
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
.NET Native apps already in the store
VS experience for .NET Native
1. Enable 2. Debug/test app
demo
.NET Native demoCompiling with .NET Native
• rd.xml whether designated elements are available for reflection.
Runtime directives of .NET Native
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
• 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
• <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
• 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
demo
.NET Native demoUsing WCF in .NET Native
demo
.NET Native demoUsing Run-Time Directives with .NET Native
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>
Migration to .NET Native• TypeLoadException exception in CLR.• .NET Native is OK
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
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
Summary• Precompilation technology • Superior performance like C++• Productivity Development like C#• Optimized app memory usage• Only Windows Store App for now.
pitanja
Popunite ankete i osvojite vrijedne nagrade!
Hvala!