Sunteți pe pagina 1din 32

Zagreb, 25. rujna 2014.

Hotel Antunovi

Zagreb, 25. rujna 2014.

C# produktivnost + C++
performanse = .NET Native
Bahrudin Hrnjica, MVP C#
daenet

Hvala svim sponzorima i partnerima!

Agenda
1. Compiling .NET applications
2. RyuJIT next generation of JIT Compiler
3. .NET Native new way of compilation
4. 7 stage of .NET Native compilation
chain
5. .NET Native warm up Demo
6. Runtime directive in .NET Native
7. .NET Native Demo (Using RD, WCF .NET
Native)
8. Migration to .NET Native
9. Summary

Compilation of .NET applications


Two ways of compilation MSIL to native
code
1. .NET Framework just-in-time (JIT) compiler
2. .NET Framework Native Image Generator
(Ngen.exe)

JIT Compiler
JIT converts MSIL to native code at RunTime 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

RyuJIT new .NET JIT


Compiler
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

Install and setting RYUJIT


two ways to turn on RyuJIT.
set an environment variable:
COMPLUS_AltJit=*
for entire machine, set the registry key
HKLM\SOFTWARE\Microsoft\.NETFramework\A
ltJit to the string "*".

Both methods cause the 64-bit CLR to


use RyuJIT instead of JIT64.
And both are temporary settings
installing RyuJIT doesnt make any
permanent changes to your machine
(aside from installing the RyuJIT files in a
directory)

de
mo

RyuJIT demo
How to enable using RyuJIT

.NET Native Demo

Compiling with .NET Native

.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

With .NET Native your apps


Provides superior performance of native
apps
Still using C# language and .NET
Framework
Still using debugging and developer
experience of .NET

Advantages of .NET Native


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

7 steps in building .NET native app

1. Building the MSIL application from


source
2. Generating interop marshaling and
serialization code
3. Merging the application
4. Reducing the application
5. Other MSIL transformations
6. Compiling from MSIL to Machine
Dependent Intermediate Language
(MDIL)
7. Binding from MDIL to native code

.NET Native apps already in the store

VS experience for .NET Native

1. Enable

2. Debug/test
app

de
mo

.NET Native demo


Compiling with .NET Native

Runtime directives of .NET Native

rd.xml whether designated elements are


available for reflection.

Structure of rd directives
1. http://schemas.microsoft.com/netfx/201
3/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 element
1. is simply a container

Rd.xml
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 - Examples
<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 - recomendations
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.

de
mo

.NET Native demo


Using WCF in .NET Native

de
mo

.NET Native demo


Using 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 arent
supported
Value types cant 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

Hvala!

Popunite ankete i osvojite vrijedne nagrade!

S-ar putea să vă placă și