Leanpub Header

Skip to main content

优化的艺术——深入.NET性能之巅

The Art of Optimization: A Deep Dive into the Peak of .NET Performance

Dive Deep into the Peak of .NET Performance. To truly optimize, you must understand the hardware and the runtime. From benchmarking basics to low-level mastery—reshape the way you write C#.

Minimum price

$15.99

$29.00

You pay

$29.00

Author earns

$23.20
$

...Or Buy With Credits!

You can get credits with a paid monthly or annual Reader Membership, or you can buy them here.
PDF
EPUB
About

About

About the Book

In the world of software development, performance is not just a metric—it is an art form of balance, insight, and control. The Art of Optimization stands as the definitive guide for C# developers who refuse to compromise on efficiency. Moving beyond superficial tips and tricks, this book takes you on a rigorous journey from the surface of managed code down to the metal of the CLR runtime and modern hardware.


This book systematically constructs a scientific and modern framework for performance optimization. Across six progressive sections, you will learn to master the tools of precise measurement with BenchmarkDotNet, before diving deep into the mysteries of Garbage Collection algorithms, JIT compiler intrinsics, and the memory revolution sparked by Span<T>. We pierce through the abstraction layers to explore CPU pipelines, cache locality, and SIMD instruction sets, revealing the hardware reality behind every line of high-performance code.


Whether you are a backend architect engineering high-throughput microservices, a game developer chasing the ultimate frame rate, or an HPC specialist processing massive datasets, this book provides a clear path from foundational theory to advanced mastery.


Key Features:

  • Micro-Architectural Sympathy: Optimization here isn't just about refactoring; it's about understanding memory models, CPU branch prediction, and instruction-level parallelism.
  • Cutting-Edge .NET Tech: Comprehensive coverage of modern .NET (8/9+) features, including Native AOT, Source Generators, and Zero-Copy programming paradigms.
  • Data-Driven Methodology: Say goodbye to "premature optimization." Establish a scientific loop of "Measure, Analyze, Optimize, and Verify."
  • Holistic Coverage: From microscopic bit-manipulation hacks to macroscopic concurrent architecture design, and framework-level tuning in ASP.NET Core and EF Core.

Who This Book Is For: This book is designed for intermediate to advanced software engineers, architects, and technical enthusiasts who possess a solid foundation in C# and yearn to break through technical bottlenecks. It is for those who want to understand the mechanics of the .NET runtime and possess the confidence to tackle any performance challenge.

Author

About the Author

lhx077

lhx077,目前圈内昵称"粉猪",资深系统架构师、引擎技术专家,拥有11年后端全栈开发经验。现任启游星科技有限公司技术总监及首席架构师,New Life Technologies 创始人,同时也是 Infinity Innovation Studio 的联合创始人及 Infinity Innovation Engine (IIE) 的主创开发者。

作为一名长期活跃于开源社区的极客,他是 .NET Foundation、Google Developer Groups 及 PCL Community 的核心成员,同时持有 CCF(中国计算机学会)会员身份。他的代码贡献遍布 GCC、Unreal Engine 及 Blender 等世界级开源项目,尤其在编译器优化与图形渲染管线领域有着深入研究。

在公众视野之外,他还担任独立内核审计顾问,长期致力于复杂系统的逆向工程与安全性分析。

Contents

Table of Contents

前言

如何阅读本书

作者的话

致谢

第1章:性能优化的世界观

  1. 1.1 重新定义“优化“:延迟、吞吐量、资源消耗与可伸缩性
  2. 1.2 “过早优化“的现代解读:何时优化,优化什么?
  3. 1.3 性能分析的黄金法则:测量、测量、再测量!
  4. 1.4 建立性能基线:你的“参照物“
  5. 1.5 80/20法则与性能瓶颈定位
  6. 1.6 优化与代码可维护性的权衡
  7. 本章总结

第2章:精通性能度量与分析

  1. 2.1 王者工具:BenchmarkDotNet 完全指南
  2. 2.2 可视化性能剖析器实战
  3. 2.3 日志与追踪:EventSource、EventListener与OpenTelemetry
  4. 2.4 生产环境监控:Application Insights、Prometheus与自定义指标
  5. 2.5 高级技巧:自定义诊断器与性能测试自动化
  6. 本章总结
  7. 思考题
  8. 实践练习

第3章:硬件的“契约“:程序员必须懂的CPU与内存

  1. 3.1 现代CPU流水线、超标量与乱序执行
  2. 3.2 分支预测的诅咒:为何if-else
  3. 3.3 CPU缓存层次结构:L1/L2/L3 Cache 的工作原理
  4. 3.4 内存模型与内存屏障 (Memory Barriers)
  5. 3.5 .NET与硬件:JIT编译概览
  6. 本章总结
  7. 思考题
  8. 实践练习

第4章:类型系统——值类型与引用类型的性能奥秘

  1. 4.1 栈与堆:内存分配的两个世界
  2. 4.2 struct的设计哲学与黄金法则
  3. 4.3 readonly struct与防御性拷贝
  4. 4.4 ref struct与stackalloc:栈上乾坤
  5. 4.5 in参数修饰符:避免大struct的拷贝开销
  6. 4.6 ref returns与ref locals:返回与操作引用
  7. 4.7 装箱与拆箱:性能的隐形杀手
  8. 4.8 泛型与接口:彻底告别装箱时代
  9. 本章总结
  10. 思考题
  11. 实践练习

第5章:字符串与文本处理

  1. 5.1 System.String的内存真相:不可变性的代价
  2. 5.2 字符串驻留机制:内存优化的双刃剑
  3. 5.3 StringBuilder的内部机制与最佳实践
  4. 5.4 高性能拼接策略:Concat、Join与string.Create
  5. 5.5 CompositeFormat与现代字符串格式化
  6. 5.6 解析与格式化:从TryParse到ISpanParsable
  7. 5.7 正则表达式性能优化:编译与Source Generators
  8. 5.8 Span革命:零拷贝文本处理
  9. 本章总结
  10. 思考题
  11. 实践练习

第6章 集合与数据结构

  1. 6.1 数组与List的性能对比
  2. 6.2 Dictionary深度剖析
  3. 6.3 LINQ的性能特征与优化策略
  4. 6.4 不可变集合的性能权衡
  5. 6.5 特殊场景下的集合选择
  6. 本章总结
  7. 思考题
  8. 实践练习

第7章:委托、Lambda与反射

  1. 7.1 委托的本质:Delegate、Action
  2. 7.2 Lambda表达式与闭包:捕获变量的代价
  3. 7.3 反射:性能的代价与缓存策略
  4. 7.4 表达式树:编译表达式以提升性能
  5. 本章总结
  6. 思考题
  7. 实践练习

第8章:.NET GC深度揭秘

  1. 8.1 垃圾回收的哲学:自动内存管理的优劣
  2. 8.2 GC的核心算法:标记-清除与标记-整理
  3. 8.3 分代收集:Gen 0、Gen 1、Gen 2的奥秘
  4. 8.4 大对象堆与固定对象堆:LOH与POH
  5. 8.5 GC模式:Workstation GC与Server GC
  6. 8.6 理解GC触发时机与STW暂停
  7. 8.7 GC调优:GCSettings、GC.Collect与NoGCRegion
  8. 本章总结
  9. 思考题
  10. 实践练习

第9章:识别与消除不必要的内存分配

  1. 9.1 使用Profiler定位内存热点
  2. 9.2 减少临时对象:常见分配陷阱分析
  3. 9.3 对象池的设计与使用
  4. 9.4 ArrayPool:重用大型数组,避免LOH碎片
  5. 9.5 字符串相关分配的治理
  6. 本章总结
  7. 思考题
  8. 实践练习

第10章:Span:现代.NET内存革命

  1. 10.1 Span的诞生:统一访问连续内存
  2. 10.2 ref struct的限制与stack-only特性
  3. 10.3 ReadOnlySpan与API设计
  4. 10.4 Memory
  5. 10.5 System.Buffers:IBufferWriter
  6. 10.6 实战案例:用Span实现零拷贝的文件与网络处理
  7. 本章总结
  8. 思考题
  9. 实践练习

第11章:IDisposable、终结器与资源管理

  1. 11.1 using语句与Dispose模式的正确实现
  2. 11.2 终结器的工作原理与性能影响
  3. 11.3 SafeHandle与CriticalFinalizerObject
  4. 11.4 内存泄漏的诊断与修复
  5. 本章总结
  6. 思考题
  7. 实践练习

第12章:System.IO.Pipelines:高性能IO的未来

  1. 12.1 传统IO模型的局限性
  2. 12.2 Pipelines的核心概念:Pipe、PipeReader与PipeWriter
  3. 12.3 背压机制与流量控制
  4. 12.4 实战:构建高性能网络服务器
  5. 本章总结
  6. 思考题
  7. 实践练习

第13章:JIT编译器的工作内幕

  1. 13.1 从IL到机器码:JIT编译流程解析
  2. 13.2 方法内联:最重要的优化技术
  3. 13.3 循环优化与边界检查消除
  4. 13.4 分层编译与动态PGO
  5. 13.5 死代码消除与常量传播
  6. 13.6 分支优化与条件移动
  7. 13.7 内存访问优化与寄存器分配
  8. 本章总结
  9. 思考题
  10. 实践练习

第14章:unsafe代码与指针——释放终极性能

  1. 14.1 unsafe上下文与指针类型
  2. 14.2 fixed语句:固定托管对象地址
  3. 14.3 stackalloc:栈内存分配的艺术
  4. 14.4 函数指针(delegate*):来自unsafe世界的高性能回调
  5. 14.5 System.Runtime.CompilerServices.Unsafe:高级内存操作
  6. 14.6 System.Runtime.InteropServices.MemoryMarshal:Span与原始内存的桥梁
  7. 14.7 本章小结
  8. 思考题
  9. 实践练习

第15章:SIMD——单指令多数据并行

  1. 15.1 SIMD技术原理
  2. 15.2 System.Numerics.Vector
  3. 15.3 硬件内部函数
  4. 15.4 SIMD应用实践
  5. 15.5 自动向量化
  6. 本章总结
  7. 思考题
  8. 实践练习

第16章:位运算的魔力

  1. 16.1 基础位操作
  2. 16.2 System.Numerics.BitOperations类
  3. 16.3 位掩码与位图应用
  4. 16.4 无分支算法与位运算技巧
  5. 本章总结
  6. 思考题
  7. 实践练习

第17章:结构体布局与数据对齐

  1. 17.1 StructLayout特性与内存布局控制
  2. 17.2 FieldOffset与精确布局控制
  3. 17.3 数据对齐与缓存行优化
  4. 17.4 面向数据的设计方法
  5. 本章总结
  6. 思考题
  7. 实践练习
  8. 实践练习

第18章:多线程与同步机制

  1. 18.1 线程模型:Thread、ThreadPool与Task
  2. 18.2 锁机制:从用户态到内核态
  3. 18.3 Interlocked原子操作
  4. 18.4 System.Threading.Channels
  5. 18.5 并发集合
  6. 本章总结
  7. 思考题
  8. 实践练习

第19章:async/await深度解析

  1. 19.1 状态机原理:编译器的魔法
  2. 19.2 ExecutionContext与异步上下文流转
  3. 19.3 SynchronizationContext与ConfigureAwait
  4. 19.4 ValueTask与IValueTaskSource:零分配异步的艺术
  5. 19.5 异步方法的性能陷阱与最佳实践
  6. 19.6 异步流与IAsyncEnumerable
  7. 本章总结
  8. 思考题
  9. 实践练习

第20章:并行计算

  1. 20.1 Parallel类的应用
  2. 20.2 并行LINQ
  3. 20.3 Task的组合与延续
  4. 20.4 数据流编程模型
  5. 20.5 并行编程中的数据布局优化
  6. 本章总结
  7. 思考题
  8. 实践练习

第21章:高级并发模式与无锁数据结构

  1. 21.1 .NET内存模型与volatile语义
  2. 21.2 无锁数据结构设计
  3. 21.3 读写锁的应用
  4. 21.4 伪共享问题的解决方案
  5. 本章总结
  6. 思考题
  7. 实践练习

第22章:ASP.NET Core高性能实践

  1. 22.1 Kestrel服务器优化
  2. 22.2 中间件性能考量
  3. 22.3 响应缓存与分布式缓存
  4. 22.4 gRPC与Web API的性能对比
  5. 22.5 SignalR性能调优
  6. 22.6 对象池与依赖注入集成
  7. 本章总结
  8. 思考题
  9. 实践练习

第23章:Entity Framework Core性能优化

  1. 23.1 查询执行机制
  2. 23.2 变更追踪的性能影响
  3. 23.3 批量操作优化
  4. 23.4 查询缓存与编译查询
  5. 23.5 Dapper与EF Core的对比分析
  6. 本章总结
  7. 思考题
  8. 实践练习

第24章:AOT时代:Source Generators与Native AOT

  1. 24.1 Source Generators原理与应用
  2. 24.2 Native AOT技术详解
  3. 24.3 程序集裁剪技术
  4. 24.4 编译模式选择与实践指南
  5. 本章总结
  6. 思考题
  7. 实践练习

第25章:总结与展望

  1. 25.1 性能优化文化的建立
  2. 25.2 .NET性能技术的演进方向
  3. 25.3 性能优化的工程哲学
  4. 本章总结
  5. 思考题
  6. 实践练习

后记

Get the free sample chapters

Click the buttons to get the free sample in PDF or EPUB, or read the sample online here

The Leanpub 60 Day 100% Happiness Guarantee

Within 60 days of purchase you can get a 100% refund on any Leanpub purchase, in two clicks.

Now, this is technically risky for us, since you'll have the book or course files either way. But we're so confident in our products and services, and in our authors and readers, that we're happy to offer a full money back guarantee for everything we sell.

You can only find out how good something is by trying it, and because of our 100% money back guarantee there's literally no risk to do so!

So, there's no reason not to click the Add to Cart button, is there?

See full terms...

Earn $8 on a $10 Purchase, and $16 on a $20 Purchase

We pay 80% royalties on purchases of $7.99 or more, and 80% royalties minus a 50 cent flat fee on purchases between $0.99 and $7.98. You earn $8 on a $10 sale, and $16 on a $20 sale. So, if we sell 5000 non-refunded copies of your book for $20, you'll earn $80,000.

(Yes, some authors have already earned much more than that on Leanpub.)

In fact, authors have earned over $14 million writing, publishing and selling on Leanpub.

Learn more about writing on Leanpub

Free Updates. DRM Free.

If you buy a Leanpub book, you get free updates for as long as the author updates the book! Many authors use Leanpub to publish their books in-progress, while they are writing them. All readers get free updates, regardless of when they bought the book or how much they paid (including free).

Most Leanpub books are available in PDF (for computers) and EPUB (for phones, tablets and Kindle). The formats that a book includes are shown at the top right corner of this page.

Finally, Leanpub books don't have any DRM copy-protection nonsense, so you can easily read them on any supported device.

Learn more about Leanpub's ebook formats and where to read them

Write and Publish on Leanpub

You can use Leanpub to easily write, publish and sell in-progress and completed ebooks and online courses!

Leanpub is a powerful platform for serious authors, combining a simple, elegant writing and publishing workflow with a store focused on selling in-progress ebooks.

Leanpub is a magical typewriter for authors: just write in plain text, and to publish your ebook, just click a button. (Or, if you are producing your ebook your own way, you can even upload your own PDF and/or EPUB files and then publish with one click!) It really is that easy.

Learn more about writing on Leanpub