TL;DR: Low-level .NET performance work is worth doing when the code runs often enough, the improvement reaches enough users, and measurements justify the added complexity. Open source libraries give each improvement unusually broad reach. They also make careless optimization unusually expensive.
I learned performance optimization by sending pull requests to the Azure .NET SDK. Some were merged. Some were not. The rejected ones were often more educational because a maintainer would ask the question I had managed to avoid: is this complexity actually worth it?
That question is the start of this series. The earlier performance loop series explained how profiling and benchmarking turn a suspicion into evidence. This series picks up after a profiler has found a hot path. It covers the code-level tricks, but every trick has to earn its place.
Library code multiplies small decisions
A line of application code may run a few times per request. The same line in a messaging client can run once for every message received by every process using that client. This is where a small allocation stops being small.
The examples in this series come from the Azure Service Bus and Azure Event Hubs clients. They sit on paths that complete message lock tokens, retry operations, or select an Event Hubs partition. A client may execute those paths thousands of times per second. Improving the library gives every application using the affected version the benefit without asking each team to find the same problem.
That reach cuts both ways. A clever change can also make the library harder to maintain for everyone. Public compatibility rules constrain the design, unusual inputs still need to work, and a benchmark that represents one common workload may punish another.
Open source review was the classroom
I call myself a practical learner because reading gives me vocabulary, while changing real code exposes everything I have misunderstood. The Azure SDK was a useful classroom because the code had production constraints and maintainers willing to challenge an optimization.
A pull request had to preserve public behavior, work across supported target frameworks, include benchmarks, and survive review by people who knew the subsystem. Sometimes the best outcome was learning that my faster version optimized an uncommon case or violated an API rule.
You do not need to become an SDK contributor to use the same method. Pick a small path in code you own, recover its assumptions, measure it, and let another developer challenge the trade-off. Performance awareness grows through those repetitions.
Scale is a property of the path
David Fowler describes scale in terms of concurrent users, input size, or how often data must be processed. For engineers, he says, that means knowing “what to ignore” and “what to pay close attention to.” The last category of scale is easy to miss during code review. A method looks harmless because one call is cheap, while the product turns it into a hot path through repetition.
The code also changes around its original assumptions. A helper written for ten calls per second may later handle ten thousand. The original author was not foolish; the workload moved. Performance work starts by recovering those assumptions and checking whether they still hold.
- How often does this path execute under real load?
- Which input types and sizes appear most often?
- Who pays for the extra CPU, allocation, or copy?
- What complexity will future maintainers inherit?
These questions are more useful than asking whether a language feature is fast or slow in isolation.
Start one level higher
In an ordinary application, I would investigate slow input/output before shaving allocations from C# code. A database query or network call can cost hundreds or thousands of times more than a small in-process operation. Fixing that path usually buys more than replacing a lambda.
Once input/output is no longer dominant, memory is a practical next place to look. Allocations consume CPU when objects are created and later when the garbage collector processes them. Only after that would I spend time tightening an algorithm or instruction-heavy loop.
This order is a guide rather than a law. A profile can always point somewhere unexpected. Its purpose is to stop a team from polishing nanoseconds beside a database call that takes half a second.
One optimization can improve one input and hurt another
One Azure Service Bus path accepted lock tokens as IEnumerable<string>. Most callers passed an array, list, or another materialized collection. Changing the internal path to use the known count avoided work for those common inputs.
Lazy enumerables became slower because the method first had to materialize them. The optimization did not win universally. Telemetry and knowledge of actual SDK usage made the trade-off reasonable because production callers overwhelmingly used materialized collections.
This is the kind of decision a benchmark table cannot make on its own. The table describes the cases you included. Product context tells you how much each case matters.
Readability belongs in the cost model
Replacing a short query with twenty lines of collection handling creates a maintenance bill. Someone has to understand the type checks, empty cases, capacity choices, and fallback behavior during the next change.
I would accept that bill in a shared hot path when measurements show a meaningful gain and the common input is well understood. I would keep the readable version in setup code, an administrative command, or a path that runs once a day.
Performance optimization is addictive because every profile contains another clue. A stopping rule protects the codebase from the investigation. Stop when the expected gain no longer pays for the complexity, or when another path has become the real bottleneck.
A small time budget helps. Give the investigation a few hours, make one measured improvement, and ship it if the result holds. Repeating that habit builds knowledge of the codebase without turning every feature into a performance project.
Two categories cover most of this series
The examples from the talk fall into two broad categories: avoid repeated allocations that increase garbage collection work, and avoid copying memory when the code can safely operate on the existing data.
The final post moves to the RabbitMQ .NET client, where a queue shrank to an eight-byte slot because the protocol seemed to supply strict bounds. A later concurrency bug showed that one of those bounds was weaker than we thought, and the queue came back. That story is about the assumptions behind a data structure as much as the structure itself.
The next post starts with Language Integrated Query (LINQ), but it is not an argument to remove LINQ. Modern .NET releases keep making LINQ faster. The useful skill is learning what the source code becomes so you can spot an allocation or enumeration that still matters in your runtime and workload.
Several earlier articles show where that process can lead. The Event Hubs partition-key work first removed temporary encoding allocations, then tightened the hash loop. A separate Azure Service Bus investigation used smaller fixes to earn an internal redesign. This series explains the reusable thinking behind those outcomes.
Further reading
These links connect the decision framework to the talk repository and the earlier performance work.
- PerformanceTricksAzureSDK repository
- David Fowler: Implementation details matter
- Stop guessing: the performance loop for production code
- Azure Service Bus: Earn the redesign
- RabbitMQ .NET client pull request #1939
- Next post: Before replacing LINQ, read what it becomes
Common questions
Isn’t this premature optimization?
It is premature when no evidence connects the change to an important path. Profiling first and benchmarking the proposed change turns it into an engineering decision.
Should application developers care about library-level tricks?
Yes, when an application has framework-like code that runs for every request, message, or record. Most applications have at least one such path.
What should I remember?
Make the hot path prove that it deserves harder code.