Table Of ContentPro .NET Per formance
Sasha Goldshtein
Dima Zurbalev
Ido Flatow
Pro .NET Performance
Copyright © 2012 by Sasha Goldshtein, Dima Zurbalev, and Ido Flatow
ThThis work is subject to copyright. All rights are reserved by the Publisher, whether the whole or part of the material is
concerned, specififically the rights of translation, reprinting, reuse of illustrations, recitation, broadcasting, reproduction on
microfifilms or in any other physical way, and transmission or information storage and retrieval, electronic adaptation, computer
software, or by similar or dissimilar methodology now known or hereafter developed. Exempted from this legal reservation
are brief excerpts in connection with reviews or scholarly analysis or material supplied specififi cally for the purpose of being
entered and executed on a computer system, for exclusiveuse by the purchaser of the work. Duplication of this publication
or parts thereof is permitted only under the provisions of the Copyright Law of the Publisher’s location, in its current version,
and permission for use must always be obtained from Springer. Permissions for use may be obtained through RightsLink at the
Copyright Clearance Center. Violations are liable to prosecution under the respective Copyright Law.
ISBN 978-1-43 02-4458-5
ISBN 978-1-4302-445 9-2 (eBook)
Trademarked names, logos, and images may appear in this book. Rather than use a trademark symbol with every occurrence of
a trademarked name, logo, or image we use the names, logos, and images only in an editorial fashion and to the benefifit of the
trademark owner, with no intention of infringement of the trademark.
ThTh e use in this publication of trade names, trademarks, service marks, and similar terms, even if they are not identifified as such,
is not to be taken as an expression of opinion as to whether or not they are subject to proprietary rights.
While the advice and information in this book are believed to be true and accurate at the date of publication, neither the
authors nor the editors nor the publisher can accept any legal responsibility for any errors or omissions that may be made. ThThe
publisher makes no warranty, express or implied, with respect to the material contained herein.
President and Publisher: Paul Manning
Lead Editor: Gwenan Spearing
Developmental Editor: Gwenan Spearing
Technical Reviewers: Todd Meister and Fabio Claudio Ferracchiati
Editorial Board: Steve Anglin, Ewan Buckingham, Gary Cornell, Louise Corrigan, Morgan Ertel, Jonathan Gennick,
Jonathan Hassell, Robert Hutchinson, Michelle Lowman, James Markham, Matthew Moodie, Jeffff Olson, Jeffffff rey
Pepper, Douglas Pundick, Ben Renow-Clarke, Dominic Shakeshaft, Gwenan Spearing, Matt Wade, Tom Welsh
Coordinating Editors: Corbin Collins and Kevin Shea
Copy Editor: Lori Cavanaugh
Compositor: SPi Global
Indexer: SPi Global
Artist: SPi Global
Cover Designer: Anna Ishchenko
Distributed to the book trade worldwide by Springer Science+Business Media New York, 233 Spring Street, 6th Floor,
New York, NY 10013. Phone 1-800-SPRINGER, fax (201) 348-4505, e-mailorders-ny@springer-sbm.com, or visit
www.springeronline.com.
For information on translations, please e-mailrights@apress.com, or visit www.apress.com.
Apress and friends of ED books may be purchased in bulk for academic, corporate, or promotional use. eBook versions and
licenses are also available for most titles. For more information, reference our Special Bulk Sales–eBook Licensing web page at
www.apress.com/bulk-sales.
Any source code or other supplementary materials referenced by the author in this text is available to readers at
www.apress.com/9781430244585. For more information about how to locate your book’s source code, go to
www.apress.com/source-code.
To my loving wife Dina, who is the sunshine of my life.
To my parents Boris and Marina, for making all the sacrififi ces so I don’t have to.
—Sasha
To my lovely wife Efrat, for reminding me that a muse is not gone, it is just playing hide and seek.
—Ido
Contents at a Glance
Foreword ......................................................................................................................xv
About the Authors .......................................................................................................xvii
About the Technical Reviewers ...................................................................................xix
Acknowledgments .......................................................................................................xxi
Introduction ...............................................................................................................xxiii
■ Chapter 1: Performance Metrics .................................................................................1
■ Chapter 2: Performance Measurement ........................................................................7
■ Chapter 3: Type Internals ..........................................................................................61
■ Chapter 4: Garbage Collection ...................................................................................91
■ Chapter 5: Collections and Generics ........................................................................145
■ Chapter 6: Concurrency and Parallelism .................................................................173
■ Chapter 7: Networking, I/O, and Serialization .........................................................215
■ Chapter 8: Unsafe Code and Interoperability ...........................................................235
■ Chapter 9: Algorithm Optimization ..........................................................................259
■ Chapter 10: Performance Patterns ..........................................................................277
■ Chapter 11: Web Application Performance ..............................................................305
Index ...........................................................................................................................335
v
C ontents
Foreword ......................................................................................................................xv
About the Authors .......................................................................................................xvii
About the Technical Reviewers ...................................................................................xix
Acknowledgments .......................................................................................................xxi
Introduction ...............................................................................................................xxiii
■ Chapter 1: Performance Metrics .................................................................................1
Performance Goals ..................................................................................................................2
Performance Metrics ...............................................................................................................4
Summary .................................................................................................................................6
■ Chapter 2: Performance Measurement ........................................................................7
Approaches to Performance Measurement .............................................................................7
Built-in Windows Tools ............................................................................................................7
Performance Counters ....................................................................................................................................8
Event Tracing for Windows (ETW) .................................................................................................................13
Time Profi lers ........................................................................................................................26
Visual Studio Sampling Profi ler ....................................................................................................................26
Visual Studio Instrumentation Profi ler ..........................................................................................................31
Advanced Uses of Time Profi lers ..................................................................................................................33
Allocation Profi lers ................................................................................................................35
Visual Studio Allocation Profi ler ....................................................................................................................35
CLR Profi ler ...................................................................................................................................................39
Memory Profi lers ...................................................................................................................44
ANTS Memory Profi ler ..................................................................................................................................44
vii
■ CONTENTS
SciTech .NET Memory Profi ler ......................................................................................................................47
Other Profi lers .......................................................................................................................49
Database and Data Access Profi lers .............................................................................................................49
Concurrency Profi lers ...................................................................................................................................50
I/O Profi lers ...................................................................................................................................................52
Microbenchmarking ..............................................................................................................53
Poor Microbenchmark Example ....................................................................................................................54
Microbenchmarking Guidelines ....................................................................................................................56
Summary ...............................................................................................................................58
■ Chapter 3: Type Internals ..........................................................................................61
An Example ...........................................................................................................................61
Semantic Differences between Reference Types and Value Types .......................................62
Storage, Allocation, and Deallocation ....................................................................................63
Reference Type Internals .......................................................................................................65
The Method Table .........................................................................................................................................66
Invoking Methods on Reference Type Instances...........................................................................................70
Sync Blocks And Thelock Keyword ............................................................................................................75
Value Type Internals ..............................................................................................................79
Value Type Limitations ..................................................................................................................................80
Virtual Methods on Value Types ....................................................................................................................82
Boxing ...........................................................................................................................................................82
Avoiding Boxing on Value Types with the Equals Method ...........................................................................84
The GetHashCodeMethod ...........................................................................................................................87
Best Practices for Using Value Types ....................................................................................89
Summary ...............................................................................................................................89
■ Chapter 4: Garbage Collection ...................................................................................91
Why Garbage Collection? ......................................................................................................91
Free List Management ..................................................................................................................................92
Reference-Counting Garbage Collection .......................................................................................................93
Tracing Garbage Collection....................................................................................................94
viii
■CONTENTS
Mark Phase ...................................................................................................................................................95
Sweep and Compact Phases ........................................................................................................................99
Pinning ........................................................................................................................................................102
Garbage Collection Flavors ..................................................................................................103
Pausing Threads for Garbage Collection .....................................................................................................103
Workstation GC ...........................................................................................................................................106
Server GC ....................................................................................................................................................107
Switching Between GC Flavors ...................................................................................................................108
Generations .........................................................................................................................110
Generational Model Assumptions ...............................................................................................................110
.NET Implementation of Generations ..........................................................................................................111
Large Object Heap ......................................................................................................................................115
References between Generations ...............................................................................................................116
Background GC ...........................................................................................................................................119
GC Segments and Virtual Memory .......................................................................................119
Finalization ..........................................................................................................................123
Manual Deterministic Finalization ..............................................................................................................123
Automatic Non-Deterministic Finalization ..................................................................................................123
Pitfalls of Non-Deterministic Finalization ...................................................................................................125
The Dispose Pattern ...................................................................................................................................128
Weak References ................................................................................................................130
Interacting with the Garbage Collector ................................................................................132
The System.GC Class ..................................................................................................................................132
Interacting with the GC using CLR Hosting .................................................................................................135
GC Triggers .................................................................................................................................................136
Garbage Collection Performance Best Practices .................................................................137
Generational Model .....................................................................................................................................137
Pinning ........................................................................................................................................................138
Finalization .................................................................................................................................................139
Miscellaneous Tips and Best Practices ......................................................................................................139
Summary .............................................................................................................................143
ix
■ CONTENTS
■ Chapter 5: Collections and Generics ........................................................................145
Generics ..............................................................................................................................145
.NET Generics .............................................................................................................................................148
Generic Constraints ....................................................................................................................................149
Implementation of CLR Generics ................................................................................................................151
Collections ...........................................................................................................................158
Concurrent Collections ...............................................................................................................................160
Cache Considerations .................................................................................................................................162
Custom Collections ..............................................................................................................166
Disjoint-Set (Union-Find) ............................................................................................................................166
Skip List ......................................................................................................................................................168
One-Shot Collections ..................................................................................................................................169
Summary .............................................................................................................................171
■ Chapter 6: Concurrency and Parallelism .................................................................173
Challenges and Gains ..........................................................................................................173
Why Concurrency and Parallelism? ............................................................................................................174
From Threads to Thread Pool to Tasks ................................................................................174
Task Parallelism ..........................................................................................................................................180
Data Parallelism .........................................................................................................................................186
C# 5 Async Methods ...................................................................................................................................190
Advanced Patterns in the TPL .....................................................................................................................193
Synchronization ...................................................................................................................194
Lock-Free Code ..........................................................................................................................................195
Windows Synchronization Mechanisms .....................................................................................................200
Cache Considerations .................................................................................................................................202
General Purpose GPU Computing ........................................................................................205
Introduction to C++ AMP ............................................................................................................................205
Matrix Multiplication ...................................................................................................................................207
N-Body Simulation ......................................................................................................................................208
Tiles and Shared Memory ...........................................................................................................................209
Summary .............................................................................................................................213
x
■CONTENTS
■ Chapter 7: Networking, I/O, and Serialization .........................................................215
General I/O Concepts ...........................................................................................................215
Synchronous and Asynchronous I/O ...........................................................................................................215
I/O Completion Ports ...................................................................................................................................217
NET Thread Pool .........................................................................................................................................220
Copying Memory .........................................................................................................................................221
Scatter–Gather I/O ...............................................................................................................221
File I/O .................................................................................................................................222
Cache Hinting .............................................................................................................................................222
Unbuffered I/O ............................................................................................................................................223
Networking ..........................................................................................................................223
Network Protocols ......................................................................................................................................223
Network Sockets ........................................................................................................................................225
Data Serialization and Deserialization .................................................................................226
Serializer Benchmarks ...............................................................................................................................226
DataSet Serialization ..................................................................................................................................229
Windows Communication Foundation .................................................................................229
Throttling ....................................................................................................................................................229
Process Model ............................................................................................................................................230
Caching .......................................................................................................................................................231
Asynchronous WCF Clients and Servers .....................................................................................................231
Bindings ......................................................................................................................................................233
Summary .............................................................................................................................234
■ Chapter 8: Unsafe Code and Interoperability ...........................................................235
Unsafe Code ........................................................................................................................235
Pinning and GC Handles .............................................................................................................................236
Lifetime Management .................................................................................................................................237
Allocating Unmanaged Memory .................................................................................................................237
Memory Pooling ..........................................................................................................................................238
xi
■ CONTENTS
P/Invoke ..............................................................................................................................239
PInvoke.net and P/Invoke Interop Assistant ................................................................................................240
Binding .......................................................................................................................................................241
Marshaler Stubs .........................................................................................................................................242
Blittable Types ............................................................................................................................................245
Marshaling Direction, Value and Reference Types ......................................................................................247
Code Access Security .................................................................................................................................248
COM Interoperability ............................................................................................................248
Lifetime Management .................................................................................................................................249
Apartment Marshaling ................................................................................................................................249
TLB Import and Code Access Security ........................................................................................................251
NoPIA ..........................................................................................................................................................251
Exceptions ..................................................................................................................................................252
C++/CLI Language Extensions ............................................................................................253
The marshal_as Helper Library ................................................................................................................255
IL Code vs. Native Code ..............................................................................................................................256
Windows 8 WinRT Interop ...................................................................................................256
Best Practices for Interop ....................................................................................................257
Summary .............................................................................................................................258
■ Chapter 9: Algorithm Optimization ..........................................................................259
Taxonomy of Complexity .....................................................................................................259
Big-Oh Notation ..........................................................................................................................................259
Turing Machines and Complexity Classes ..................................................................................................261
Memoization and Dynamic Programming ...........................................................................265
Edit Distance ...............................................................................................................................................266
All-Pairs-Shortest-Paths .............................................................................................................................267
Approximation .....................................................................................................................269
Traveling Salesman ....................................................................................................................................270
Maximum Cut .............................................................................................................................................271
xii