net native deep dive

23
Andrew Pardoe ([email protected] ) .NET Native ([email protected] ) Program Manager, .NET Runtime .NET Native deep dive

Upload: catrin

Post on 25-Feb-2016

57 views

Category:

Documents


1 download

DESCRIPTION

.NET Native deep dive. Andrew Pardoe ( [email protected] ) .NET Native ( [email protected] ) Program Manager, .NET Runtime. Questions?. .NET code g eneration: A primer. MSIL bytecode. Source code (C#/VB/F#). Machine code. C#/VB/F# compiler. Code generation. - PowerPoint PPT Presentation

TRANSCRIPT

Page 1: NET Native  deep dive

Andrew Pardoe ([email protected]).NET Native ([email protected])Program Manager, .NET Runtime

.NET Native deep dive

Page 2: NET Native  deep dive

Questions?

Page 3: NET Native  deep dive

.NET code generation: A primer

Source code(C#/VB/F#)

MSIL bytecode Machine code

C#/VB/F# compiler

Code generation

Code generation is done either….• Lazily at runtime with a Just-In-Time, or JIT, compiler• Optimistically at build time with NGen, Triton or .NET Native

Page 4: NET Native  deep dive

.NET Code Execution today

C++ PerformanceSelf-contained apps

Dynamic experience“Unlimited” class libraries

Page 5: NET Native  deep dive

Code generation One JIT compiler for each architecture

64-bit JIT emphasizes code quality over throughput for server scenarios

32-bit JITs emphasize throughput over code quality for app scenarios Two native code generators with different deployment properties

NGen runs on the user’s machine, at install time or automatically Triton runs in the Windows Phone 8 Store, app bound to libraries at

install One runtime (VM or VEE) spans multiple

scenarios Desktop CLR for Windows Client and Server workloads (different GC

modes) CoreCLR for Mobile and targeted Server workloads built from the same

codebase

Code execution in CLR 4.5

Page 6: NET Native  deep dive

MSIL MDIL

Triton: Native code on Windows Phone

C#MSIL

MDIL nativecode

Windows Phone apps are compiled

to native code in the cloud on WP8

Page 7: NET Native  deep dive

The properties of code executionCode generation properties

• Throughput• Code quality• Memory usage

Deployment properties• Distribution format• Acquisition process• Serviceability

Security properties• Typesafe execution• Code identity &

isolation• Security functionality

Developer experience

• Everything is a tradeoff!

• “Make simple things easy and hard things possible.”

Page 8: NET Native  deep dive

.NET Code Execution tomorrow

C# Productivity with C++ Performance

C# Language withdynamic experiences

RyuJIT & CLR VM.NET Native

Today: One runtime engine & same code execution for all scenariosTomorrow: Refactored engine & multiple execution strategies provide best of class on both extremes. From there, we target the middle.

Page 9: NET Native  deep dive

.NET Native (Project N)

Page 10: NET Native  deep dive

.NET Native

Page 11: NET Native  deep dive

.NET Native

Scenario Improvement (%)Cold Startup 39.32%Warm Startup 31.21%Memory Usage 12.68%

Wordament on .NET Native:

Next Generation Compiler in the Cloud for Store Apps represents an evolution of Triton & NGen

Uses lean runtime and VC++ optimizer for fast code execution and reduced memory usage

Based on a refactored CLR engine. Native code is shared, managed code has moved into the libraries

Provides converged developer experience for .NET across devices

.

Page 12: NET Native  deep dive

Visual Studio Experience for .NET Native

Enable .NET Native for your project

Debug and test your app with .NET Native

Page 13: NET Native  deep dive

.NET Native Compiler Toolchain

Page 14: NET Native  deep dive

IL Compiler: C# -> MSIL

- Compile C# source code

- Generate & compile marshalling and serialization code

Page 15: NET Native  deep dive

Today, interop code is generated as MSIL at runtime, JIT-compiled, executed and thrown out JIT compilation takes time (but the methods are small) If your P/Invoke signature has an issue your compiled code is gone

In .NET Native, the Marshalling Code Generator creates interop code as a C# file at compile-time Compilation is done at build-time The code can be debugged as C#, not assembly The code can be inspected and optimized before compilation to MSIL

Pregenerated interop code

Page 16: NET Native  deep dive

IL Compiler: Merge & reduce app code- Merge all app & library code

- “Reduce” app to throw away unneeded types, code, metadata, etc.

Page 17: NET Native  deep dive

A bit of a conundrum… Static compilation means any code that can run is generated at

compile-time Reflection requires that anything you can inspect at runtime can be

executed The solution: preserve the set of classes,

methods, generic types, etc., that the code might want to call

And if the compiler gets it wrong? Missing metadata results in an exception File a bug with the .NET Native team?

Static dynamic code (dynamic static code?)

Page 18: NET Native  deep dive

The type specifies the class, namespace or assembly affected Types: class, interface, or struct Group of types: namespace, assembly (*Application* is all types in the app

assemblies) Type member: method, field, property, or event

The degree tells the compiler what behavior is needed Reflection behaviors, Serialization behaviors, Interop behaviors

The policy notes the scope to apply that behavior inside the type Accessibility: All, Public, Private, Internal Required: Including this keyword means “Include these types in the final binary”

Runtime directives (default.rd.xml)<Directives xmlns="http://schemas.microsoft.com/netfx/2013/01/metadata"> <Application> <Assembly Name="*Application*" Dynamic="Required All" /> <!-- Add your application specific runtime directives here. --> </Application></Directives>

Page 19: NET Native  deep dive

IL Compiler: MSIL -> MDIL -> machine code

- C++ optimizer compiles MSIL to MDIL, Machine Dependent Intermediate Format. App is mostly machine code but cannot execute.

- “Bind” MDIL app code into machine code. App distributed as a stub, app.exe, and app code in a dll. MRT Runtime is distributed in the app package, making the app package self-sufficient.

Page 20: NET Native  deep dive

for (long j = 0; j < numBodies; j++) // From Nbody sample{ Float_4 r;

r.x = A[j].pos.x - B.pos.x; r.y = A[j].pos.y - B.pos.y; r.z = A[j].pos.z - B.pos.z; float distSqr = r.x * r.x + r.y * r.y + r.z * r.z; distSqr += softeningSquared;

float invDist = 1.0f / (float)Math1.Sqrt(distSqr); float invDistCube = invDist * invDist * invDist;

float s = fParticleMass * invDistCube;

acc.x += r.x * s; acc.y += r.y * s; acc.z += r.z * s;}

Example: automatic vectorization/parallelization

The C++ optimizer can recognize this code as parallel and generate vector (SIMD) instructions

Page 21: NET Native  deep dive

.NET Native performance gainsOptimized runtime

Pregenerated interopTree-shaken code

C++ optimizer Global optimizations

Pay-for-play metadata

Pregenerated serializers

Refactored core libraries

Page 22: NET Native  deep dive

.NET Native: Call to actionLearn more about .NET NativeDownload the .NET Native VS add-InTry out your Store app with .NET NativeHelp us make .NET Native better

http://aka.ms/dotnetnativeRequires Visual Studio 2013 Update 2 RC

http://aka.ms/dotnetnativehttp://blogs.msdn.com/b/dotnet

Do you have a top Store app or library?Reach out to us: [email protected] Participate in the .NET Native ForumReach out to us: [email protected]