230 likes | 485 Views
.NET Native deep dive. Andrew Pardoe ( andrew.pardoe@microsoft.com ) .NET Native ( dotnetnative@microsoft.com ) 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.
E N D
.NET Native deep dive Andrew Pardoe (andrew.pardoe@microsoft.com) .NET Native (dotnetnative@microsoft.com) Program Manager, .NET Runtime
.NET code generation: A primer MSIL bytecode Source code (C#/VB/F#) 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
.NET Code Execution today C++ Performance Self-contained apps Dynamic experience “Unlimited” class libraries
Code execution in CLR 4.5 • Code generation • One JIT compiler for each architecture • 64-bit JIT emphasizes code quality over throughputfor 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
Triton: Native code on Windows Phone • C# • MSIL • MDIL • • native • code • MSIL MDIL Windows Phone apps are compiled to native code in the cloud on WP8
The properties of code execution • Code 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.”
.NET Code Execution tomorrow .NET Native RyuJIT& CLR VM C# Language with dynamic experiences C# Productivity with C++ Performance Today: One runtime engine & same code execution for all scenarios Tomorrow: Refactored engine & multiple execution strategies provide best of class on both extremes. From there, we target the middle.
.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 . Wordament on .NET Native:
Visual Studio Experience for .NET Native Debug and test your app with .NET Native Enable .NET Native for your project
IL Compiler: C# -> MSIL • Compile C# source code • Generate & compile marshalling and serialization code
Pregenerated interop code • 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
IL Compiler: Merge & reduce app code • Merge all app & library code • “Reduce” app to throw away unneeded types, code, metadata, etc.
Static dynamic code (dynamic static code?) • 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?
Runtime directives (default.rd.xml) • 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” <Directivesxmlns="http://schemas.microsoft.com/netfx/2013/01/metadata"> <Application> <AssemblyName="*Application*"Dynamic="Required All" /> <!-- Add your application specific runtime directives here. --> </Application> </Directives>
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.
Example: automatic vectorization/parallelization 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; floatdistSqr = r.x * r.x + r.y * r.y + r.z * r.z; distSqr += softeningSquared; floatinvDist = 1.0f / (float)Math1.Sqrt(distSqr); floatinvDistCube = invDist * invDist * invDist; float s = fParticleMass * invDistCube; acc.x += r.x * s; acc.y += r.y * s; acc.z += r.z * s; } The C++ optimizer can recognize this code as parallel and generate vector (SIMD) instructions
.NET Native performance gains Optimized runtime Refactored core libraries Pregeneratedserializers Pregenerated interop Tree-shaken code Pay-for-play metadata C++ optimizer Global optimizations
.NET Native: Call to action Learn more about .NET Native • http://aka.ms/dotnetnative • http://blogs.msdn.com/b/dotnet Download the .NET Native VS add-In • http://aka.ms/dotnetnative • Requires Visual Studio 2013 Update 2 RC Try out your Store app with .NET Native • Do you have a top Store app or library? • Reach out to us: dotnetnative@microsoft.com • Participate in the .NET Native Forum • Reach out to us: dotnetnative@microsoft.com Help us make .NET Native better
We would love to hear from you! Blog E-mail dotnet UserVoice MSDN Forums @dotnet