boxing & unboxing

25
BOXING & UNBOXING Larry Nung

Upload: larry-nung

Post on 17-Jul-2015

61 views

Category:

Technology


0 download

TRANSCRIPT

Page 1: Boxing & unboxing

BOXING & UNBOXING

Larry Nung

Page 2: Boxing & unboxing

AGENDA

Boxing/UnBoxing

Issue for boxing/unboxing

How to find out boxing/Unboxing

2

Page 3: Boxing & unboxing

BOXING/UNBOXING

3

Page 4: Boxing & unboxing

BOXING

Value Type => Object

New object must be allocated and constructed

Expensive computationally

4

int i = 123;

object o = i; // explicit boxing

Page 5: Boxing & unboxing

UNBOXING

Extracts value type from the object

Expensive computationally

5

int i = 123; // a value type

object o = i; // boxing

int j = (int)o; // unboxing

Page 6: Boxing & unboxing

ISSUE FOR BOXING/UNBOXING

6

Page 7: Boxing & unboxing

ISSUE FOR BOXING/UNBOXING

7

using System;

using System.Diagnostics;

internal class Program {

private static void Main(string[] args) {

var count = 1000000000;

Console.WriteLine(“Boxing: {0} ms",

DoTest(count, () => {

var value = 123;

object value2 = value;

}));

Console.WriteLine(“None boxing: {0} ms",

DoTest(count, () => {

var value = 123;

var value2 = value;

}));

}

static long DoTest(int count, Action action) {

var sw = Stopwatch.StartNew();

for (int i = 0; i < count; ++i) action();

return sw.ElapsedMilliseconds;

}

}

0

2000

4000

6000

8000

10000

12000

1000000 10000000 100000000 1000000000

Boxing

None boxing

Page 8: Boxing & unboxing

ISSUE FOR BOXING/UNBOXING

8

using System;

using System.Diagnostics;

internal class Program {

private static void Main(string[] args) {

var count = 1000000000;

Console.WriteLine("Boxing: {0} ms",

DoTest(count, () => {

var value = 123;

var value2 = string.Format("{0}", value);

}));

Console.WriteLine("None Boxing: {0} ms",

DoTest(count, () => {

var value = 123;

var value2 = string.Format("{0}", value.ToString());

}));

}

static long DoTest(int count, Action action) {

var sw = Stopwatch.StartNew();

for (int i = 0; i < count; ++i) action();

return sw.ElapsedMilliseconds;

}

}

0

50000

100000

150000

200000

250000

Boxing

None Boxing

Page 9: Boxing & unboxing

ISSUE FOR BOXING/UNBOXING

9

using System;

internal class Program {

private static void Main(string[] args)

{

try {

var value1 = 1;

object value2 = value1;

var value3 = (float)value2;

} catch (Exception ex) {

Console.WriteLine(ex);

}

}

}

Page 10: Boxing & unboxing

ISSUE FOR BOXING/UNBOXING

10

using System;

internal class Program

{

private static void Main(string[] args)

{

object a = 1;

object b = 1;

bool value = a == b;

bool value2 = a.Equals(b);

Console.WriteLine(value);

Console.WriteLine(value2);

}

}

Page 11: Boxing & unboxing

ISSUE FOR BOXING/UNBOXING

11

using System;

internal class Program

{

private static void Main(string[] args)

{

object a = 1;

object b = 1;

bool value = a == b;

bool value2 = a.Equals(b);

Console.WriteLine(value);

Console.WriteLine(value2);

}

}

Page 12: Boxing & unboxing

ISSUE FOR BOXING/UNBOXING

12

using System;

struct Counter

{

private int x;

public void Increment() { this.x++; }

public int Count { get { return this.x; } }

}

internal class Riddle

{

public readonly object counter = new Counter();

}

internal class Program

{

private static void Main(string[] args)

{

var riddle = new Riddle();

((Counter)riddle.counter).Increment();

Console.WriteLine(((Counter)riddle.counter).Count);

}

}

Page 13: Boxing & unboxing

ISSUE FOR BOXING/UNBOXING

13

using System;

struct Counter

{

private int x;

public void Increment() { this.x++; }

public int Count { get { return this.x; } }

}

internal class Riddle

{

public readonly object counter = new Counter();

}

internal class Program

{

private static void Main(string[] args)

{

var riddle = new Riddle();

((Counter)riddle.counter).Increment();

Console.WriteLine(((Counter)riddle.counter).Count);

}

}

Page 14: Boxing & unboxing

HOW TO FIND OUT

BOXING/UNBOXING

14

Page 15: Boxing & unboxing

ILDASM

15

Page 16: Boxing & unboxing

ILSPY

16

Page 17: Boxing & unboxing

.NET REFLECTOR

17

Page 18: Boxing & unboxing

BOXCOP

For .NET 3.5

18

Page 19: Boxing & unboxing

RESHARPER - HEAP ALLOCATION VIEWER

EXTENSION

19

Page 20: Boxing & unboxing

RESHARPER - HEAP ALLOCATION VIEWER

EXTENSION

20

Page 21: Boxing & unboxing

RESHARPER - HEAP ALLOCATION VIEWER

EXTENSION

21

Page 22: Boxing & unboxing

CLR C# HEAP ALLOCATION ANALYZER

For Visual Studio 2015

22

Page 23: Boxing & unboxing

REFERENCE

Boxing and Unboxing (C# Programming Guide)

https://msdn.microsoft.com/en-

us//library/yz2be5wk.aspx?f=255&MSPPError=-

2147217396

[C#]Effective C# 條款十七:盡量減少裝箱與拆箱 -

Level Up-點部落

http://www.dotblogs.com.tw/larrynung/archive/2011/03/0

8/21712.aspx

[.NET Resource]透過BoxCop偵測程式是否存在Boxing與UnBoxing - Level Up-點部落

http://www.dotblogs.com.tw/larrynung/archive/2013/03/0

1/94727.aspx 23

Page 24: Boxing & unboxing

ReSharper - Heap Allocation Viewer Extension -

Level Up

http://larrynung.github.io/2014/08/12/resharper-heap-

allocation-viewer-extension/

Clr C# Heap Allocation Analyzer - Level Up

http://larrynung.github.io/2014/11/05/clr-c-number-heap-

allocation-analyzer/

24

Page 25: Boxing & unboxing

QUESTION & ANSWER

25