Writing
High-Performance
.NET Code
Ben Watson
Writing High-Performance .NET Code
Version 1.5
Smashwords Edition
ISBN-13: 978-0-9905834-4-8
ISBN-10: 0-9905834-4-9
Copyright 2014 Ben Watson
All Rights Reserved. These rights include reproduction, transmission, translation, and electronic storage. For the purposes of Fair Use, brief excerpts of the text are permitted for non-commercial purposes. Code samples may be reproduced on a computer for the purpose of compilation and execution and not for republication.
This eBook is licensed for your personal and professional use only. You may not resell or give this book away to other people. If you wish to give this book to another person, please buy an additional copy for each recipient. If you are reading this book and did not purchase it, or it was not purchased for your use only, then please visit http://www.writinghighperf.net/buy/ and purchase your own copy. If you wish to purchase this book for your organization, please contact me for licensing information. Thank you for respecting the hard work of this author.
Trademarks
Any trademarked names, logos, or images used in this book are assumed valid trademarks of their respective owners. There is no intention to infringe on the trademark.
Disclaimer
While care has been taking to ensure the information contained in this book is accurate, the author takes no responsibility for your use of the information presented.
Contact
For more information about this book, please visit .
Cover Design
Cover design by Claire Watson, http://www.bluekittycreations.co.uk.
Table of Contents
About the Author
Ben Watson has been a software engineer at Microsoft since 2008. On the Bing platform team, he has built one of the worlds 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 geocaching, books of all kinds, classical music, and spending time with his wife Leticia and daughter Emma. He is also the author of the book C# 4.0 How-To, published by Sams.
About the Technical Editor
Mike Magruder has been a software engineer since the early 90s. He worked on the .Net Runtime team from v1 thru v4, and he worked on the Bing platform team where he architected one of the worlds leading .NET-based, high-performance server applications. In his spare time, he enjoys snowboarding, building snowboards, and spending time with his wife Helen.
Acknowledgements
Thank you to my friend and technical editor Mike Magruder for his invaluable feedback on this book, and more importantly, for his three years of mentorship at Microsoft that changed my career. This has made me a hundred times the engineer I was.
I also owe a big thank you to Maoni Stephens for her in-depth guidance and feedback about the garbage collector. Thank you as well to Abhinaba Basu for information about the CLR on Windows Phone, and Brian Rasmussen for feedback on a number of topics.
I would not have started this book without a random conversation with my brother-in-law James Adams that got me thinking about actually writing a book like this. Thanks to my editors, my dad Michael Watson and my wife Leticia for their many hours of reading and rereading of the drafts.
Very special thanks to Leticia and our daughter Emma who supported many hours of absence while I worked on this book. I could not have done this without your support and encouragement.
Introduction
- Purpose of this Book
.NET is an amazing system for building software. It allows us to build functional, connected apps in a fraction of the time it would have taken us a decade ago. So much of it just works, and that is a great thing. It offers applications memory and type safety, a robust framework library, services like automatic memory management, and so much more.
Programs written with .NET are called managed applications because they depend on a runtime and framework that manages many of their vital tasks and ensures a basic safe operating environment. Unlike unmanaged , or native, software written directly to the operating systems APIs, managed applications do not have free reign of their process.
This layer of management between your program and the computers processor can be a source of anxiety for developers who assume that it must add some significant overhead. This book will set you at ease, demonstrate that the overhead is worth it, and that the supposed performance degradation is almost always exaggerated. Often, the performance problems developers blame on .NET are actually due to poor coding patterns and a lack of knowledge of how to optimize their programs on this framework. Skills gained from years of optimizing software written in C++, Java, or VB may not always apply to managed code, and some advice is actually detrimental. Sometimes the rapid development nature of .NET can encourage people to build bloated, slow, poorly optimized code faster than ever before. Certainly, there are other reasons why code can be of poor quality: lack of skill generally, time pressure, poor design, lack of developer resources, laziness, and so on. This book will explicitly remove lack of knowledge about the framework as an excuse and attempt to deal with some of the others as well. With the principles explained in this book, you will learn how to build lean, fast, efficient applications that avoid these missteps. In all types of code, in all platforms, the same thing is true: if you want performant code, you have to work for it.
This is not a language reference or tutorial. It is not even a detailed discussion of the CLR. For those topics, there are other resources (see Appendix CBibliography for a list of useful books, blogs, and people to pay attention to). To get the most out of this book you should already have in-depth experience with .NET.
There are many code samples, especially of underlying implementation details in IL or assembly code. I caution you not to gloss over these sections. You should try to replicate my results as you work through this book so that you understand exactly what is going on.
This book will teach you how to get maximum performance out of managed code, while sacrificing none or as little of the benefits of .NET as possible. You will learn good coding techniques, specific things to avoid, and perhaps most importantly, how to use freely available tools to easily measure your performance. This book will teach you those things with minimum fluff. This book is what you need to know, relevant and concise, with no padding of the content. Most chapters begin with general knowledge and background, followed by specific tips, and finally ending with a section on step-by-step measurement and debugging for many different scenarios.
Along the way you will deep-dive into specific portions of .NET, particularly the underlying Common Language Runtime (CLR) and how it manages your memory, generates your code, handles concurrency, and more. You will see how .NETs architecture both constrains and enables your software, and how your programming choices can drastically affect the overall performance of your application. As a bonus, I will share relevant anecdotes from the last six years of building very large, complex, high-performance .NET systems at Microsoft. You will likely notice that my bias throughout this book is for server applications, but nearly everything discussed in this book is applicable to desktop and mobile applications as well. Where appropriate, I will share advice for those specific platforms.
You will gain a sufficient understanding of .NET and the principles of well-performing code so that when you run into circumstances not specifically covered in this book, you can apply your newfound knowledge and solve unanticipated problems.
Next page