.NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception...

54
Deep Dive into the Kernel of .NET on Windows Phone 8 Subramanian (Mani) Ramaswamy Senior Program Manager Developer Division, Microsoft Corporation Session ID: 3-005

Transcript of .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception...

Page 1: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Deep Dive into the Kernel of .NET on Windows Phone 8

Subramanian (Mani) RamaswamySenior Program ManagerDeveloper Division, Microsoft Corporation

Session ID: 3-005

Page 2: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Background: What is the CLR?

.NET Core Libraries

Common Language Runtime

CodeGenGarbage Collector

Security Model

Exception Handling

Loader & Binder

Profiling & Debugging APIs

Entity Frame-work

And more!

XAML for

Windows Store Apps

Silverlight for

Windows Phone

ASP.NET

Page 3: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Wasn’t Windows Phone using NETCF?

Page 4: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Wasn’t Windows Phone using NETCF?

Yes, before Windows Phone 8. Now, it uses the engine that powers Silverlight Apps (aka CoreCLR).

Page 5: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Agenda: What have we been up to for Windows Phone 8?

Page 6: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Agenda: What have we been up to for Windows Phone 8?

Great Functionality

Great Performance

Page 7: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Agenda: What have we been up to for Windows Phone 8?

Great Functionality

Great Performance

Support Native Game EnginesShare Code Across PlatformsWrite Fluid & Responsive Apps

Page 8: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Agenda: What have we been up to for Windows Phone 8?

Great Functionality

Great Performance

Support Native Game EnginesShare Code Across PlatformsWrite Fluid & Responsive Apps Improve App Startup Time Improve App Execution Time Improve App Responsiveness

Page 9: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Great Functionality

Support Native Game Engines Share Code Across Platforms Write Fluid & Responsive Apps

Page 10: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Support Native Game Engines:WinRT

Page 11: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Windows Phone 8 Apps Can be Hybrid

Native game engines can be used as-isEasily call into native code from managedPorting apps to WP8 is made easy!

Libraries can be written in C++: Tweak to your heart’s content

Better than P/Invoke: Interop performance is optimized!

Page 12: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

WinRT for Windows Phone 8

A modern set of rich OS APIs!App calling into these APIs are hybrid automatically!

Do more with your code, more safely.

Windows Phone 8 apps and Windows 8 apps can be closer than ever.

Page 13: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Go Game application using the GNU Go AI (also used in some iOS apps)

Support Native Game Engines:WinRT Demo

Page 14: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Share Code Across Platforms:Portable Class Libraries

Page 15: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Platform & Project Proliferation

Become a great coder by writing your app over and over again for ~ 20 different .NET frameworks?

Page 16: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

One Source, One Project, One Binary….

Target multiple platforms simultaneously.

Use the Model-View-View-Model pattern & create portable abstractions for cross-platform apps.

Learn more at the hands-on talk by Daniel Plaisted on Portable Class Libraries.

Page 17: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Write Fluid & Responsive Apps:Async

Page 18: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Async

What if Outlook ran synchronously?No reading emails while sending/receiving!

Synchronous Programming: cheap, well-understood.

Apps need to be responsive!

We have provided Async versions of many APIs.

Asynchronousprogrammingdoesn’t have to be hard.

Page 19: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

A Synchronous Code Sample

void SynchronousMethod()

{

for(int i = 0; i <=5; ++i) DoWork(GetWork());

}

Page 20: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Asynchronous Code Sample

void SynchronousMethod()

{

for(int i = 0; i <=5; ++i) DoWork(GetWork());

}

async void AsynchronousMethod()

{

for(int i = 0; i <=5; ++i)

DoWork(await GetWorkTask());

}

Page 21: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Go Game application using the GNU Go AI (also used in some iOS apps)

Write Fluid & Responsive Apps:Async Demo

Page 22: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Great Performance

App Startup Time ImprovesApp Execution Time ImprovesApp Responsiveness Improves

Page 23: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Improve App Startup Times:Compiler in the Cloud

Page 24: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Compiler in the Cloud Improves Startup

App Startup is 2X Faster on Windows Phone 8!

Page 25: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Generating Machine Code in .NET

C# MSIL ASM

VB/C# Compiler

.NET CodeGen

Page 26: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Generating Machine Code in .NET

JITEmployed Platforms Phone 7.5

ASM Generated Before Execution

Reuses Compiled Code No

Provides Fast Startup No

Easy Deployment Yes

Library updates aren’t impactful Yes

Page 27: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Generating Machine Code in .NET

JIT NGENEmployed Platforms Phone 7.5 Desktop

ASM Generated Before Execution At Install

Reuses Compiled Code No Yes

Provides Fast Startup No Yes

Easy Deployment Yes No

Library updates aren’t impactful Yes No

Page 28: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Generating Machine Code in .NET

JIT NGENEmployed Platforms Phone 7.5 Desktop

ASM Generated Before Execution At Install

Reuses Compiled Code No Yes

Provides Fast Startup No Yes

Easy Deployment Yes No

Library updates aren’t impactful Yes No

(Pre)Compiling on the device is

expensive!

Page 29: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

What do we need?

• Fast startup• JIT can’t avoid compilation costs

• Fast, easy deployment• NGEN install costs

• Fast device updates• NGEN cascaded recompilation

Page 30: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Cascaded Re-compilation

If a managed DLL is updated, native code for all DLLs/EXEs depending on it have to be regenerated.

Mscorlib.dll

Library.dll

App1.exe

App2.exe

Page 31: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Cascaded Re-compilation

If a managed DLL is updated, native code for all DLLs/EXEs depending on it have to be regenerated.

Mscorlib.dll

Library.dll

App1.exe

App2.exe

Page 32: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Cascaded Re-compilation

If a managed DLL is updated, native code for all DLLs/EXEs depending on it have to be regenerated.

Mscorlib.dll

Library.dll

App1.exe

App2.exe

Page 33: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Cascaded Re-compilation

• Ideally, libraries are stable and never updated.

• Either, add all new types to new DLLs.• Or, statically link in the libraries into

the app.

• The root cause is that base classes are fragile. (Fragile Base Class Problem)

Page 34: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

class Base{

int i;string s;virtual void baseFunc() { }

}

Setting it up via an Example….

0x0 MethodTable for Base

0x4 i

0x8 s

Page 35: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

class Base{

int i;string s;virtual void baseFunc() { }

}

Setting it up via an Example….

class AppDerived: Base{

int j;int foo (int a) { return

a+j; }}

0x0 MethodTable for AppDerived

0x4 i

0x8 S

0xC j

0x0 MethodTable for Base

0x4 i

0x8 s

Page 36: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Assembly for “foo” int foo (int a) {return a+j;}

// defining the inputs to the method

; r0 = this

; r1 = a

// loading the field j (offset 0xC)

ldr r0, [r0 + 0xC]

// performing the add and storing the result in r0

add r0, r0, r1

// returning from the method

bx lr

0x0 MethodTable for AppDerived

0x4 i

0x8 S

0xC j

Page 37: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

class Base{

int i;string s;int k;virtual void baseFunc() { }

}

The Fragile Base Class “Base”

0x0 MethodTable for Base

0x4 i

0x8 s

0xC k

Page 38: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

class Base{

int i;string s;int k;virtual void baseFunc() { }

}

The Fragile Base Class “Base”

New entry to class Base changes the

object layout

0x0 MethodTable for Base

0x4 i

0x8 s

0xC k

Page 39: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Crash & Burn: Persisted Code is Invalid// defining the inputs to the method

; r0 = this

; r1 = a

// loading the field j (offset 12)

ldr r0, [r0 + 0xC]

// performing the add and storing the result in r0

add r0, r0, r1

// returning from the method

bx lr

0x0 MethodTable for AppDerived

0x4 i

0x8 s

0xC k

0x10 j

Page 40: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Crash & Burn: Persisted Code is Invalid// defining the inputs to the method

; r0 = this

; r1 = a

// loading the field j (offset 12)

ldr r0, [r0 + 0xC]

// performing the add and storing the result in r0

add r0, r0, r1

// returning from the method

bx lr

This code is wrong!

It needs to be 0x10

0x0 MethodTable for AppDerived

0x4 i

0x8 s

0xC k

0x10 j

The derived class object layout has to be updated!

Page 41: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Regenerating Native Code for Method “foo” int foo (int a) {return a+j;}// defining the inputs to the method

; r0 = this

; r1 = a

// loading the field j (offset 12)

ldr r0, [r0 + 0x10] //was ldr r0, [r0+0xC]

// performing the add and storing the result in r0

add r0, r0, r1

// returning from the method

bx lr

0x0 MethodTable for AppDerived

0x4 i

0x8 s

0xC k

0x10 j

Page 42: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

The Simple Powerful Idea

• Compile to native code what doesn’t change.• Add placeholders (MDIL) for everything that may

change.• Replace MDIL with native code at install time.• On a library update, re-link quickly to recreate

native code.

Page 43: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Machine Dependent IL: A Better Approach int foo (int a) {return a+j;}// defining the inputs to the method

; r0 = this

; r1 = a

// loading the field j (offset 12)

LDR r0, [r0 + “fieldToken(j)”] //replace at link stage

// performing the add and storing the result in r0

add r0, r0, r1

// returning from the method

bx lr

Page 44: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Where’s the Cloud?

Compatible with Windows Phone 7.5 Apps!

C# Compiler

C# Source Code

MSILAssembly

MDIL Compiler

MDIL Assembly

MDIL Assembly

Download to Device

Native Image

Generator

Native DLL Run

This happens in the “cloud”

Page 45: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

What’s the Developer Experience?

Improve App Startup Times:Compiler in the Cloud Demo

Page 46: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Improve App Execution Times:Optimizing Compiler

Page 47: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Optimizing Compiler: Fast-running Apps

BallD

rawin

gTes

t

BallS

imul

atio

nTes

t

BoxDra

wingT

est

BoxSi

mul

atio

nTes

t0

10

20

30

40

30 32

12 13

26

39

1317

Higher is Better

NetCF Adjusted (Op-timized Box2D)CoreCLR (Original Box2D)F

PS

Windows Phone 7.5 Normalized

Windows Phone 8

Page 48: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Improve App Responsiveness:GC enhances Memory Management

Page 49: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Windows Phone 8 GC

• Significantly lower GC latencies

• Multi-core allocator

• Improved object lifetime tracking

• Three generational GC

Page 50: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Recap

Page 51: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

Putting it all together

WinRT allows use of native game engines.Portable Class libraries make it a snap for creating Win8/WP8 apps.Async programming is the coolest.Compiler in the cloud improves startup dramatically and automatically, even for Windows Phone 7.5 apps.Performance wins up the wazoo.

Make it easier than ever to write great .NET apps for Windows Phone 8

Page 52: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

• 11/2 12:45 – B92 Nexus/Normandy – Portable Libraries (3-004)

• 10/31 - Evolution of .NET(3-016)

• 10/31 – WP8 Critical Developer Practices for Delivering Outstanding Apps (3-045)

• 11/1 - How to Leverage your Code across WP8 and Windows 8 (3-043)

Related Sessions

Page 54: .NET Core Libraries Common Language Runtime CodeGen Garbage Collector Security Model Exception Handling Loader & Binder Profiling & Debugging APIs Entity.

© 2012 Microsoft Corporation. All rights reserved. Microsoft, Windows, Windows Vista and other product names are or may be registered trademarks and/or trademarks in the U.S. and/or other countries.The information herein is for informational purposes only and represents the current view of Microsoft Corporation as of the date of this presentation. Because Microsoft must respond to changing market conditions, it should not be interpreted to be a commitment on the part of Microsoft, and Microsoft cannot guarantee the accuracy of any information provided after the date of this presentation. MICROSOFT MAKES NO WARRANTIES, EXPRESS, IMPLIED OR STATUTORY, AS TO THE INFORMATION IN THIS PRESENTATION.