嘿,亲!知识可是无价之宝呢,但咱这精心整理的资料也耗费了不少心血呀。小小地破费一下,绝对物超所值哦!如有下载和支付问题,请联系我们QQ(微信同号):813200300
本次赞助数额为: 2 元微信扫码支付:2 元
请留下您的邮箱,我们将在2小时内将文件发到您的邮箱
Pro .NET Performance-Optimize Your C# Applications.(2012)
Contents
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 Profilers ������������������������������������������������������������������������������������������������������������������������26
Visual Studio Sampling Profiler ��������������������������������������������������������������������������������������������������������������������26
Visual Studio Instrumentation Profiler ����������������������������������������������������������������������������������������������������������31
Advanced Uses of Time Profilers ������������������������������������������������������������������������������������������������������������������33
Allocation Profilers ����������������������������������������������������������������������������������������������������������������35
Visual Studio Allocation Profiler ��������������������������������������������������������������������������������������������������������������������35
CLR Profiler ���������������������������������������������������������������������������������������������������������������������������������������������������39
Memory Profilers �������������������������������������������������������������������������������������������������������������������44
ANTS Memory Profiler ����������������������������������������������������������������������������������������������������������������������������������44
■ Contents
viii
SciTech �NET Memory Profiler ����������������������������������������������������������������������������������������������������������������������47
Other Profilers �����������������������������������������������������������������������������������������������������������������������49
Database and Data Access Profilers �������������������������������������������������������������������������������������������������������������49
Concurrency Profilers �����������������������������������������������������������������������������������������������������������������������������������50
I/O Profilers ���������������������������������������������������������������������������������������������������������������������������������������������������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 The lock 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 GetHashCode Method ���������������������������������������������������������������������������������������������������������������������������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
■ Contents
ix
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
■ Contents
x
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
■ Contents
xi
■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
■ Contents
xii
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
■ Contents
xiii
Probabilistic Algorithms ������������������������������������������������������������������������������������������������������271
Probabilistic Maximum Cut �������������������������������������������������������������������������������������������������������������������������271
Fermat Primality Test ����������������������������������������������������������������������������������������������������������������������������������272
Indexing and Compression ��������������������������������������������������������������������������������������������������272
Variable Length Encoding ���������������������������������������������������������������������������������������������������������������������������272
Index Compression �������������������������������������������������������������������������������������������������������������������������������������273
Summary �����������������������������������������������������������������������������������������������������������������������������274
Chapter 10: Performance Patterns ■ ..........................................................................277
JIT Compiler Optimizations �������������������������������������������������������������������������������������������������277
Standard Optimizations ������������������������������������������������������������������������������������������������������������������������������277
Method Inlining �������������������������������������������������������������������������������������������������������������������������������������������278
Range-Check Elimination ���������������������������������������������������������������������������������������������������������������������������279
Tail Call �������������������������������������������������������������������������������������������������������������������������������������������������������281
Startup Performance �����������������������������������������������������������������������������������������������������������283
Pre-JIT Compilation with NGen (Native Image Generator) ��������������������������������������������������������������������������284
Multi-Core Background JIT Compilation �����������������������������������������������������������������������������������������������������287
Image Packers ��������������������������������������������������������������������������������������������������������������������������������������������288
Managed Profile-Guided Optimization (MPGO) �������������������������������������������������������������������������������������������288
Miscellaneous Tips for Startup Performance����������������������������������������������������������������������������������������������289
Processor-Specific Optimization �����������������������������������������������������������������������������������������291
Single Instruction Multiple Data (SIMD) ������������������������������������������������������������������������������������������������������292
Instruction-Level Parallelism ����������������������������������������������������������������������������������������������������������������������294
Exceptions ���������������������������������������������������������������������������������������������������������������������������297
Reflection ����������������������������������������������������������������������������������������������������������������������������298
Code Generation ������������������������������������������������������������������������������������������������������������������298
Generating Code from Source ��������������������������������������������������������������������������������������������������������������������299
Generating Code Using Dynamic Lightweight Code Generation �����������������������������������������������������������������300
Summary �����������������������������������������������������������������������������������������������������������������������������304
Chapter 11: Web Application Performance ■ ..............................................................305
Testing the Performance of Web Applications ���������������������������������������������������������������������305
■ Contents
xiv
Visual Studio Web Performance Test and Load Test �����������������������������������������������������������������������������������306
HTTP Monitoring Tools ��������������������������������������������������������������������������������������������������������������������������������307
Web Analyzing Tools �����������������������������������������������������������������������������������������������������������������������������������308
Improving Web Performance on the Server ������������������������������������������������������������������������308
Cache Commonly Used Objects ������������������������������������������������������������������������������������������������������������������308
Using Asynchronous Pages, Modules, and Controllers �������������������������������������������������������������������������������310
Tweaking the ASP�NET Environment ������������������������������������������������������������������������������������313
Turn Off ASP�NET Tracing and Debugging ���������������������������������������������������������������������������������������������������313
Disable View State ��������������������������������������������������������������������������������������������������������������������������������������315
Server-Side Output Cache ��������������������������������������������������������������������������������������������������������������������������316
Pre-Compiling ASP�NET Applications ����������������������������������������������������������������������������������������������������������317
Fine-Tuning the ASP�NET Process Model ����������������������������������������������������������������������������������������������������318
Configuring IIS ���������������������������������������������������������������������������������������������������������������������319
Output Caching �������������������������������������������������������������������������������������������������������������������������������������������319
Application Pool Configuration �������������������������������������������������������������������������������������������������������������������321
Optimizing the Network �������������������������������������������������������������������������������������������������������323
Apply HTTP Caching Headers ���������������������������������������������������������������������������������������������������������������������323
Turn on IIS Compression �����������������������������������������������������������������������������������������������������������������������������326
Minification and Bundling ���������������������������������������������������������������������������������������������������������������������������328
Use Content Delivery Networks (CDNs) ������������������������������������������������������������������������������������������������������329
Scaling ASP�NET Applications ����������������������������������������������������������������������������������������������330
Scaling Out �������������������������������������������������������������������������������������������������������������������������������������������������331
ASP�NET Scaling Mechanisms ��������������������������������������������������������������������������������������������������������������������331
Scaling Out Pitfalls �������������������������������������������������������������������������������������������������������������������������������������332
Summary �����������������������������������������������������������������������������������������������������������������������������332
Index ��������������������������������������������������������������������������������������������������������������������������������������335