Pro .NET Per formance
Cover
Contents at a Glance
Contents
Foreword
About the Authors
About the Technical Reviewers
Acknowledgments
Introduction
1: Performance Metrics
Performance Goals
Performance Metrics
Summary
2: Performance Measurement
Approaches to Performance Measurement
Built-in Windows Tools
Performance Counters
Performance Counter Logs and Alerts
Custom Performance Counters
Event Tracing for Windows (ETW)
Windows Performance Toolkit (WPT)
PerfMonitor
The PerfView Tool
Custom ETW Providers
Time Profilers
Visual Studio Sampling Profiler
Visual Studio Instrumentation Profiler
Advanced Uses of Time Profilers
Sampling Tips
Collecting Additional Data While Profiling
Profiler Guidance
Advanced Profiling Customization
Allocation Profilers
Visual Studio Allocation Profiler
CLR Profiler
Memory Profilers
ANTS Memory Profiler
SciTech .NET Memory Profiler
Other Profilers
Database and Data Access Profilers
Concurrency Profilers
I/O Profilers
Microbenchmarking
Poor Microbenchmark Example
Microbenchmarking Guidelines
Summary
3: Type Internals
An Example
Semantic Differences between Reference Types and Value Types
Storage, Allocation, and Deallocation
Reference Type Internals
The Method Table
Invoking Methods on Reference Type Instances
Dispatching Non-Virtual Methods
Dispatching Static and Interface Methods
Sync Blocks And The lock Keyword
Value Type Internals
Value Type Limitations
Virtual Methods on Value Types
Boxing
Avoiding Boxing on Value Types with the Equals Method
The GetHashCode Method
Best Practices for Using Value Types
Summary
4: Garbage Collection
Why Garbage Collection?
Free List Management
Reference-Counting Garbage Collection
Tracing Garbage Collection
Mark Phase
Local Roots
Static Roots
Other Roots
Performance Implications
Sweep and Compact Phases
Pinning
Garbage Collection Flavors
Pausing Threads for Garbage Collection
Pausing Threads during the Mark Phase
Pausing Threads during the Sweep Phase
Workstation GC
Concurrent Workstation GC
Non-Concurrent Workstation GC
Server GC
Switching Between GC Flavors
Generations
Generational Model Assumptions
.NET Implementation of Generations
Generation 0
Generation 1
Generation 2
Large Object Heap
References between Generations
Background GC
GC Segments and Virtual Memory
Finalization
Manual Deterministic Finalization
Automatic Non-Deterministic Finalization
Pitfalls of Non-Deterministic Finalization
The Dispose Pattern
Resurrection
Weak References
Interacting with the Garbage Collector
The System.GC Class
Diagnostic Methods
Notifications
Control Methods
Interacting with the GC using CLR Hosting
GC Triggers
Garbage Collection Performance Best Practices
Generational Model
Pinning
Finalization
Miscellaneous Tips and Best Practices
Value Types
Object Graphs
Pooling Objects
Paging and Allocating Unmanaged Memory
Static Code Analysis (FxCop) Rules
Summary
5: Collections and Generics
Generics
.NET Generics
Generic Constraints
Implementation of CLR Generics
Java Generics
C++ Templates
Generics Internals
Collections
Concurrent Collections
Cache Considerations
Custom Collections
Disjoint-Set (Union-Find)
Skip List
One-Shot Collections
Summary
6: Concurrency and Parallelism
Challenges and Gains
Why Concurrency and Parallelism?
From Threads to Thread Pool to Tasks
Task Parallelism
Throttling Parallelism in Recursive Algorithms
More Examples of Recursive Decomposition
Exceptions and Cancellation
Data Parallelism
Parallel.For and Parallel.ForEach
Parallel LINQ (PLINQ)
C# 5 Async Methods
Advanced Patterns in the TPL
Synchronization
Lock-Free Code
Windows Synchronization Mechanisms
Cache Considerations
General Purpose GPU Computing
Introduction to C++ AMP
Matrix Multiplication
N-Body Simulation
Tiles and Shared Memory
Summary
7: Networking, I/O, and Serialization
General I/O Concepts
Synchronous and Asynchronous I/O
I/O Completion Ports
NET Thread Pool
Copying Memory
Unmanaged Memory
Exposing Part of a Buffer
Scatter–Gather I/O
File I/O
Cache Hinting
Unbuffered I/O
Networking
Network Protocols
Pipelining
Streaming
Message Chunking
Chatty Protocols
Message Encoding and Redundancy
Network Sockets
Asynchronous Sockets
Socket Buffers
Nagle's Algorithm
Registered I/O
Data Serialization and Deserialization
Serializer Benchmarks
DataSet Serialization
Windows Communication Foundation
Throttling
Process Model
Caching
Asynchronous WCF Clients and Servers
Bindings
Summary
8: Unsafe Code and Interoperability
Unsafe Code
Pinning and GC Handles
Lifetime Management
Allocating Unmanaged Memory
Memory Pooling
P/Invoke
PInvoke.net and P/Invoke Interop Assistant
Binding
Marshaler Stubs
Blittable Types
Marshaling Direction, Value and Reference Types
Code Access Security
COM Interoperability
Lifetime Management
Apartment Marshaling
TLB Import and Code Access Security
NoPIA
Exceptions
C++/CLI Language Extensions
The marshal_as Helper Library
IL Code vs. Native Code
Windows 8 WinRT Interop
Best Practices for Interop
Summary
9: Algorithm Optimization
Taxonomy of Complexity
Big-Oh Notation
Turing Machines and Complexity Classes
The Halting Problem
NP-Complete Problems
Memoization and Dynamic Programming
Edit Distance
All-Pairs-Shortest-Paths
Approximation
Traveling Salesman
Maximum Cut
Probabilistic Algorithms
Probabilistic Maximum Cut
Fermat Primality Test
Indexing and Compression
Variable Length Encoding
Index Compression
Summary
10: Performance Patterns
JIT Compiler Optimizations
Standard Optimizations
Method Inlining
Range-Check Elimination
Tail Call
Startup Performance
Pre-JIT Compilation with NGen (Native Image Generator)
Multi-Core Background JIT Compilation
Image Packers
Managed Profile-Guided Optimization (MPGO)
Miscellaneous Tips for Startup Performance
Strong Named Assemblies Belong in the GAC
Make Sure Your Native Images Do Not Require Rebasing
Reduce the Total Number of Assemblies
Processor-Specific Optimization
Single Instruction Multiple Data (SIMD)
Instruction-Level Parallelism
Exceptions
Reflection
Code Generation
Generating Code from Source
Generating Code Using Dynamic Lightweight Code Generation
Summary
11: Web Application Performance
Testing the Performance of Web Applications
Visual Studio Web Performance Test and Load Test
HTTP Monitoring Tools
Web Analyzing Tools
Improving Web Performance on the Server
Cache Commonly Used Objects
Using Asynchronous Pages, Modules, and Controllers
Creating an Asynchronous Page
Creating an Asynchronous Controller
Tweaking the ASP.NET Environment
Turn Off ASP.NET Tracing and Debugging
Disable View State
Server-Side Output Cache
Pre-Compiling ASP.NET Applications
Fine-Tuning the ASP.NET Process Model
Configuring IIS
Output Caching
User-Mode Cache
Kernel-Mode Cache
Application Pool Configuration
Idle Timeouts
Processor Affinity
Web Garden
Optimizing the Network
Apply HTTP Caching Headers
Setting Cache Headers for Static Content
Setting Cache Headers for Dynamic Content
Turn on IIS Compression
Static Compression
Dynamic Compression
Configuring Compression
IIS Compression and Client Applications
Minification and Bundling
Use Content Delivery Networks (CDNs)
Scaling ASP.NET Applications
Scaling Out
ASP.NET Scaling Mechanisms
Scaling Out Pitfalls
Summary
Index