O'Reilly logo

Stay ahead with the world's most comprehensive technology and business learning platform.

With Safari, you learn the way you learn best. Get unlimited access to videos, live online training, learning paths, books, tutorials, and more.

Start Free Trial

No credit card required

Writing High-Performance .NET Code, 2nd Edition

Book Description

Take performance to the next level!

This book does not just teach you how the CLR works---it teaches you exactly what you need to do now to obtain the best performance today. It will expertly guide you through the nuts and bolts of extreme performance optimization in .NET, complete with in-depth examinations of CLR functionality, free tool recommendations and tutorials, useful anecdotes, and step-by-step guides to measure and improve performance.

This second edition incorporates the advances and improvements in .NET over the last few years, as well as greatly expanded coverage of tools, more topics, more tutorials, more tips, and improvements throughout the entire book.

New in the 2nd Edition:

  • 50% increase in content!
  • New examples, code samples, and diagrams throughout entire book
  • More ways to analyze the heap and find memory problems
  • More tool coverage, including expanded usage of Visual Studio
  • More benchmarking
  • New GC configuration options
  • Code warmup techniques
  • New .NET features such as ref-returns, value tuples, SIMD, and more
  • More detailed analysis of LINQ
  • Tips for high-level feature areas such as ASP.NET, ADO.NET, and WPF

Also find expanded coverage and discover new tips and tricks for:

  • Profiling with multiple tools to quickly find problem areas
  • Detailed description of the garbage collector, how to optimize your code for it, and how to diagnose difficult memory-related issues
  • How to analyze JIT and diagnose warmup problems
  • Effective use of the Task Parallel Library to maximize throughput
  • Which .NET features and APIs to use and which to avoid
  • Instrument your program with performance counters and ETW events
  • Use the latest and greatest .NET features
  • Build a performance-minded team

...and so much more

Ben Watson has been a software engineer at Microsoft since 2008. On the Bing platform team, he has built one of the world's leading .NET-based, high-performance server applications, handling high-volume, low-latency requests across thousands of machines for millions of customers.

In his spare time, he enjoys books, music, the outdoors, and spending time with his wife Leticia and children Emma and Matthew. They live near Seattle, Washington, USA.

Table of Contents

  1. Writing High-Performance .NET Code
  2. About the Author
  3. Acknowledgements
  4. Foreword
  5. Introduction to the Second Edition
  6. Introduction
    1. Purpose of this Book
    2. Why Should You Choose Managed Code?
    3. Is Managed Code Slower Than Native Code?
    4. Are The Costs Worth the Benefits?
    5. Am I Giving Up Control?
    6. Work With the CLR, Not Against It
    7. Layers of Optimization
    8. The Seductiveness of Simplicity
    9. .NET Performance Improvements Over Time
    10. .NET Core
    11. Sample Source Code
    12. Why Gears?
  7. Performance Measurement and Tools
    1. Choosing What to Measure
    2. Premature Optimization
    3. Average vs. Percentiles
    4. Benchmarking
    5. Useful Tools
    6. Measurement Overhead
    7. Summary
  8. Memory Management
    1. Memory Allocation
    2. Garbage Collection Operation
    3. Configuration Options
    4. Performance Tips
    5. Investigating Memory and GC
    6. Summary
  9. JIT Compilation
    1. Benefits of JIT Compilation
    2. JIT in Action
    3. JIT Optimizations
    4. Reducing JIT and Startup Time
    5. Optimizing JITting with Profiling (Multicore JIT)
    6. When to Use NGEN
    7. .NET Native
    8. Custom Warmup
    9. When JIT Cannot Compete
    10. Investigating JIT Behavior
    11. Summary
  10. Asynchronous Programming
    1. The Thread Pool
    2. The Task Parallel Library
    3. TPL Dataflow
    4. Parallel Loops
    5. Performance Tips
    6. Thread Synchronization and Locks
    7. Investigating Threads and Contention
    8. Summary
  11. General Coding and Class Design
    1. Classes and Structs
    2. Tuples
    3. Interface Dispatch
    4. Avoid Boxing
    5. ref returns and locals
    6. for vs. foreach
    7. Casting
    8. P/Invoke
    9. Delegates
    10. Exceptions
    11. dynamic
    12. Reflection
    13. Code Generation
    14. Preprocessing
    15. Investigating Performance Issues
    16. Summary
  12. Using the .NET Framework
    1. Understand Every API You Call
    2. Multiple APIs for the Same Thing
    3. Collections
    4. Strings
    5. Avoid APIs that Throw Exceptions Under Normal Circumstances
    6. Avoid APIs That Allocate From the Large Object Heap
    7. Use Lazy Initialization
    8. The Surprisingly High Cost of Enums
    9. Tracking Time
    10. Regular Expressions
    11. LINQ
    12. Reading and Writing Files
    13. Optimizing HTTP Settings and Network Communication
    14. SIMD
    15. Investigating Performance Issues
    16. Summary
  13. Performance Counters
    1. Consuming Existing Counters
    2. Creating a Custom Counter
    3. Summary
  14. ETW Events
    1. Defining Events
    2. Consume Custom Events in PerfView
    3. Create a Custom ETW Event Listener
    4. Get Detailed EventSource Data
    5. Consuming CLR and System Events
    6. Custom PerfView Analysis Extension
    7. Summary
  15. Code Safety and Analysis
    1. Understanding the OS, APIs, and Hardware
    2. Restrict API Usage in Certain Areas of Your Code
    3. Centralize and Abstract Performance-Sensitive and Difficult Code
    4. Isolate Unmanaged and Unsafe Code
    5. Prefer Code Clarity to Performance Until Proven Otherwise
    6. Summary
  16. Building a Performance-Minded Team
    1. Understand the Areas of Critical Performance
    2. Effective Testing
    3. Performance Infrastructure and Automation
    4. Believe Only Numbers
    5. Effective Code Reviews
    6. Education
    7. Summary
  17. Kick-Start Your Application’s Performance
    1. Define Metrics
    2. Analyze CPU Usage
    3. Analyze Memory Usage
    4. Analyze JIT
    5. Analyze Asynchronous Performance
  18. Higher-Level Performance
    1. ASP.NET
    2. ADO.NET
    3. WPF
    4. Big O
    5. Common Algorithms and Their Complexity
  19. Bibliography
    1. Useful Resources
    2. People and Blogs
  20. Contact Information