net core libraries common language runtime codegen garbage collector security model exception...

Post on 02-Apr-2015

235 Views

Category:

Documents

1 Downloads

Preview:

Click to see full reader

TRANSCRIPT

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

Subramanian (Mani) RamaswamySenior Program ManagerDeveloper Division, Microsoft Corporation

Session ID: 3-005

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

Wasn’t Windows Phone using NETCF?

Wasn’t Windows Phone using NETCF?

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

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

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

Great Functionality

Great Performance

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

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

Great Functionality

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

Support Native Game Engines:WinRT

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!

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.

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

Support Native Game Engines:WinRT Demo

Share Code Across Platforms:Portable Class Libraries

Platform & Project Proliferation

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

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.

Write Fluid & Responsive Apps:Async

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.

A Synchronous Code Sample

void SynchronousMethod()

{

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

}

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());

}

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

Write Fluid & Responsive Apps:Async Demo

Great Performance

App Startup Time ImprovesApp Execution Time ImprovesApp Responsiveness Improves

Improve App Startup Times:Compiler in the Cloud

Compiler in the Cloud Improves Startup

App Startup is 2X Faster on Windows Phone 8!

Generating Machine Code in .NET

C# MSIL ASM

VB/C# Compiler

.NET CodeGen

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

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

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!

What do we need?

• Fast startup• JIT can’t avoid compilation costs

• Fast, easy deployment• NGEN install costs

• Fast device updates• NGEN cascaded recompilation

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

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

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

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)

class Base{

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

}

Setting it up via an Example….

0x0 MethodTable for Base

0x4 i

0x8 s

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

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

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

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

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

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!

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

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.

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

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”

What’s the Developer Experience?

Improve App Startup Times:Compiler in the Cloud Demo

Improve App Execution Times:Optimizing Compiler

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

Improve App Responsiveness:GC enhances Memory Management

Windows Phone 8 GC

• Significantly lower GC latencies

• Multi-core allocator

• Improved object lifetime tracking

• Three generational GC

Recap

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

• 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

• .NET Team Blog

• Twitter @DotNet

• Windows Phone Blog

• Windows Phone Dev Blog

• VS Booths

• SubramaR@microsoft.com

Resources

© 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.

top related