<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Daniel</title>
    <description>The latest articles on DEV Community by Daniel (@axitordev).</description>
    <link>https://dev.to/axitordev</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.us-east-2.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F4238580%2F29f2b88a-4e1e-4e2f-9c9f-c6622f3bca2a.jpg</url>
      <title>DEV Community: Daniel</title>
      <link>https://dev.to/axitordev</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/axitordev"/>
    <language>en</language>
    <item>
      <title>Rust is not faster by default: how my .NET client got 33x ahead of the official Rust SDK</title>
      <dc:creator>Daniel</dc:creator>
      <pubDate>Tue, 17 Nov 2026 13:59:25 +0000</pubDate>
      <link>https://dev.to/axitordev/rust-is-not-faster-by-default-how-my-net-client-got-33x-ahead-of-the-official-rust-sdk-360f</link>
      <guid>https://dev.to/axitordev/rust-is-not-faster-by-default-how-my-net-client-got-33x-ahead-of-the-official-rust-sdk-360f</guid>
      <description>&lt;p&gt;I open-sourced a Binance client for .NET 10. It's up to &lt;strong&gt;33x faster per message&lt;/strong&gt; than the official Binance Rust SDK, has &lt;strong&gt;43x better p99.9 latency&lt;/strong&gt;, and allocates &lt;strong&gt;zero bytes&lt;/strong&gt; on the hot path.&lt;/p&gt;

&lt;p&gt;Whenever I mention this, someone tells me Rust is fast by default and .NET is the managed, garbage-collected compromise. I've been building .NET systems for 25 years and I've never believed that. So I ran a benchmark that's hard to argue with.&lt;/p&gt;


&lt;div class="ltag-github-readme-tag"&gt;
  &lt;div class="readme-overview"&gt;
    &lt;h2&gt;
      &lt;img src="https://assets.dev.to/assets/github-logo-5a155e1f9a670af7944dd5e12375bc76ed542ea80224905ecaf878b9157cdefc.svg" alt="GitHub logo"&gt;
      &lt;a href="https://github.com/Axitor" rel="noopener noreferrer"&gt;
        Axitor
      &lt;/a&gt; / &lt;a href="https://github.com/Axitor/Trading.Gateway" rel="noopener noreferrer"&gt;
        Trading.Gateway
      &lt;/a&gt;
    &lt;/h2&gt;
    &lt;h3&gt;
      Binance client for .NET 10, faster than every major Binance client in .NET and Rust. Zero allocations on the hot path, resilience built in, Native AOT ready. REST, WebSocket API, JSON and SBE streams, user data and local order books.
    &lt;/h3&gt;
  &lt;/div&gt;
  &lt;div class="ltag-github-body"&gt;
    
&lt;div id="readme" class="md"&gt;&lt;div class="markdown-heading"&gt;
&lt;h1 class="heading-element"&gt;Axitor.Trading.Gateway&lt;/h1&gt;
&lt;/div&gt;
&lt;p&gt;&lt;strong&gt;Faster than every major Binance client. In .NET, and in Rust.&lt;/strong&gt;&lt;/p&gt;
&lt;p&gt;A Binance gateway for .NET 10, built for trading bots where the slow message, the dead socket and the ban cost real money. Zero allocations on the hot path. Resilience in the core. Native AOT from day one.&lt;/p&gt;
&lt;p&gt;&lt;a href="https://github.com/Axitor/Trading.Gateway/actions/workflows/ci.yml" rel="noopener noreferrer"&gt;&lt;img src="https://github.com/Axitor/Trading.Gateway/actions/workflows/ci.yml/badge.svg" alt="CI"&gt;&lt;/a&gt;
&lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/7434d72ea2c9e35186c22a992fa4db2629024449957bd17244cb67031f2901dd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f2e4e45542d31302d353132424434"&gt;&lt;img src="https://camo.githubusercontent.com/7434d72ea2c9e35186c22a992fa4db2629024449957bd17244cb67031f2901dd/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f2e4e45542d31302d353132424434" alt=".NET 10"&gt;&lt;/a&gt;
&lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/d8ec2845301654650919199f1fa41d0d177c78ab9508ddb53df109e56ca26858/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4e6174697665253230414f542d72656164792d326561343466"&gt;&lt;img src="https://camo.githubusercontent.com/d8ec2845301654650919199f1fa41d0d177c78ab9508ddb53df109e56ca26858/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f4e6174697665253230414f542d72656164792d326561343466" alt="Native AOT"&gt;&lt;/a&gt;
&lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/09a1a3f2e5dc3283da5060021e3ebc76f2e73f741c7134d019f57196cd05dcc0/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f686f74253230706174682d7a65726f253230616c6c6f632d326561343466"&gt;&lt;img src="https://camo.githubusercontent.com/09a1a3f2e5dc3283da5060021e3ebc76f2e73f741c7134d019f57196cd05dcc0/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f686f74253230706174682d7a65726f253230616c6c6f632d326561343466" alt="Hot path"&gt;&lt;/a&gt;
&lt;a rel="noopener noreferrer nofollow" href="https://camo.githubusercontent.com/25f22433216f57b7e8ab05cd38038bf35055fcebc89a3139af90122b9342a7b0/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c7565"&gt;&lt;img src="https://camo.githubusercontent.com/25f22433216f57b7e8ab05cd38038bf35055fcebc89a3139af90122b9342a7b0/68747470733a2f2f696d672e736869656c64732e696f2f62616467652f6c6963656e73652d4d49542d626c7565" alt="License"&gt;&lt;/a&gt;&lt;/p&gt;
&lt;div class="markdown-heading"&gt;
&lt;h2 class="heading-element"&gt;In numbers&lt;/h2&gt;
&lt;/div&gt;
&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;33x lower cost per message&lt;/strong&gt; than the official Binance Rust SDK. &lt;strong&gt;15x&lt;/strong&gt; lower than Binance.Net.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;1.14 million messages per second&lt;/strong&gt; on a single connection, on a desktop CPU from 2014. Up to &lt;strong&gt;17x&lt;/strong&gt; the throughput of the Rust SDK, up to &lt;strong&gt;8.6x&lt;/strong&gt; Binance.Net, even in its high-performance mode.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;43x better p99.9 latency&lt;/strong&gt; than the Rust SDK, &lt;strong&gt;16x&lt;/strong&gt; better than Binance.Net, on the busiest stream.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;0 bytes, 0 GC&lt;/strong&gt; on the stream hot path. Binance.Net makes up to 285 collections per run, with GC pauses up to 72 ms.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;1,500 candles in 3 KB.&lt;/strong&gt; Binance.Net needs 1.76 MB, the…&lt;/li&gt;
&lt;/ul&gt;&lt;/div&gt;
  &lt;/div&gt;
  &lt;div class="gh-btn-container"&gt;&lt;a class="gh-btn" href="https://github.com/Axitor/Trading.Gateway" rel="noopener noreferrer"&gt;View on GitHub&lt;/a&gt;&lt;/div&gt;
&lt;/div&gt;


&lt;h2&gt;
  
  
  How I tested
&lt;/h2&gt;

&lt;p&gt;I didn't compare pieces of my code to pieces of someone else's. I ran whole clients through their public APIs, the same way a trading bot would use them:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Axitor.Trading.Gateway&lt;/strong&gt;, my client, .NET 10, JIT and Native AOT builds&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Binance.Net 13.8.0&lt;/strong&gt;, the standard .NET client, in its default mode and its high-performance mode, with its rate limiters turned off&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;binance-sdk 77.1.0&lt;/strong&gt;, the official Binance Rust connector&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;nautilus-binance 0.66.0&lt;/strong&gt;, NautilusTrader's Binance adapter, also in Rust&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;Every client reads the same recorded Binance mainnet traffic (about 640k book tickers and 98k depth updates across 20 symbols) from the same local exchange stub on the same machine. As a baseline I added a raw socket loop that just reads messages and throws them away.&lt;/p&gt;

&lt;p&gt;I look at two numbers:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Client cost per message.&lt;/strong&gt; How much time a client adds on top of that raw socket read. The transport is the same for everyone, so this shows what each client's own code costs.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Tail latency at live pace.&lt;/strong&gt; Frames are replayed with their original timing, and I measure from send to handler.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The box is an Intel Core i7-5820K from 2014 on Windows 10, with .NET 10.0.14 and BenchmarkDotNet 0.15.8. Old hardware makes waste easy to see.&lt;/p&gt;

&lt;p&gt;Full method, all tables and the fairness rules are in &lt;a href="https://github.com/Axitor/Trading.Gateway/blob/main/docs/adr/ADR-0018-Benchmarks-that-measure-the-real-thing.md" rel="noopener noreferrer"&gt;ADR-0018&lt;/a&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  The numbers
&lt;/h2&gt;

&lt;div class="table-wrapper-paragraph"&gt;&lt;table&gt;
&lt;thead&gt;
&lt;tr&gt;
&lt;th&gt;&lt;/th&gt;
&lt;th&gt;&lt;strong&gt;Axitor&lt;/strong&gt;&lt;/th&gt;
&lt;th&gt;Binance.Net&lt;/th&gt;
&lt;th&gt;binance-sdk (Rust)&lt;/th&gt;
&lt;th&gt;Nautilus (Rust)&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Cost per depth update&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0.97 µs&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;15.0 µs&lt;/td&gt;
&lt;td&gt;32.1 µs&lt;/td&gt;
&lt;td&gt;20.3 µs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Book ticker, one connection&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;1.14M msg/s&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;291k&lt;/td&gt;
&lt;td&gt;129k&lt;/td&gt;
&lt;td&gt;181k&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Depth, one connection&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;484k msg/s&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;62.2k&lt;/td&gt;
&lt;td&gt;30.1k&lt;/td&gt;
&lt;td&gt;46.7k&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Book ticker p99.9 at live pace&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;237 µs&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;3,911 µs&lt;/td&gt;
&lt;td&gt;10,223 µs&lt;/td&gt;
&lt;td&gt;7,042 µs&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Memory per book ticker&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;0 B&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;1,056 B&lt;/td&gt;
&lt;td&gt;3,584 B&lt;/td&gt;
&lt;td&gt;1,751 B&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;1,500 candles over REST&lt;/td&gt;
&lt;td&gt;&lt;strong&gt;638 µs, 3 KB&lt;/strong&gt;&lt;/td&gt;
&lt;td&gt;5.49 ms, 1.76 MB&lt;/td&gt;
&lt;td&gt;8.18 ms, 3.97 MB&lt;/td&gt;
&lt;td&gt;3.66 ms, 2.91 MB&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;Against the official Rust SDK that's &lt;strong&gt;33x faster&lt;/strong&gt; per message, up to &lt;strong&gt;17x faster&lt;/strong&gt; throughput, &lt;strong&gt;43x better&lt;/strong&gt; at p99.9, and &lt;strong&gt;1,270x better&lt;/strong&gt; on memory for 1,500 candles: 3 KB against 3.97 MB.&lt;/p&gt;

&lt;p&gt;Against NautilusTrader's adapter it's &lt;strong&gt;21x&lt;/strong&gt;, &lt;strong&gt;11x&lt;/strong&gt; and &lt;strong&gt;30x&lt;/strong&gt;. Against Binance.Net it's &lt;strong&gt;15x&lt;/strong&gt; per message and up to &lt;strong&gt;8.6x&lt;/strong&gt; on throughput, even with Binance.Net in its high-performance mode.&lt;/p&gt;

&lt;h2&gt;
  
  
  Where the Rust SDK loses time
&lt;/h2&gt;

&lt;p&gt;Rust doesn't have a garbage collector, but allocating still isn't free.&lt;/p&gt;

&lt;p&gt;binance-sdk is generated from Binance's OpenAPI spec. For every frame it copies the bytes into a string, parses that into a JSON tree, clones the tree twice and turns every number into its own heap string. One book ticker message ends up with &lt;strong&gt;69 heap allocations&lt;/strong&gt;. You won't see GC pauses, because there is no GC. You pay with time instead, on every &lt;code&gt;malloc&lt;/code&gt; and every &lt;code&gt;free&lt;/code&gt;, inside every message.&lt;/p&gt;

&lt;p&gt;NautilusTrader's adapter does better at 40 allocations per book ticker, but it still copies every frame and sends it through unbounded queues.&lt;/p&gt;

&lt;p&gt;Binance.Net, meanwhile, is the .NET stereotype in action. It creates an object for every message: about 1 KB per book ticker and 6 to 10 KB per depth update. Under load that turns into up to &lt;strong&gt;285 garbage collections per run&lt;/strong&gt;, Gen1 and Gen2 included, with &lt;strong&gt;pauses up to 72 ms&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;None of that comes from the runtime. It comes from the design. Here's what I did differently.&lt;/p&gt;

&lt;h2&gt;
  
  
  What I did differently
&lt;/h2&gt;

&lt;h3&gt;
  
  
  Parse in place, into flat structs
&lt;/h3&gt;

&lt;p&gt;Messages go straight from the UTF-8 bytes in the socket buffer into a flat struct with public fields. There's no JSON tree, no strings and no reflection anywhere. I wrote the readers by hand, and prices go through an exact fast-path decimal parser (Clinger's fast path). Even the symbol is packed into one &lt;code&gt;UInt128&lt;/code&gt;, so &lt;code&gt;BTCUSDT&lt;/code&gt; never becomes a string.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;BookTickerModel&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;Symbol&lt;/span&gt; &lt;span class="nc"&gt;Symbol&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="nc"&gt;UpdateId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="nc"&gt;EventTime&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="nc"&gt;TransactionTime&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nc"&gt;BidPrice&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nc"&gt;BidQuantity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nc"&gt;AskPrice&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="kt"&gt;double&lt;/span&gt; &lt;span class="nc"&gt;AskQuantity&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It's a plain struct with public fields, so there are no property getters or defensive copies, and the GC has nothing to track.&lt;/p&gt;

&lt;h3&gt;
  
  
  No virtual calls on dispatch
&lt;/h3&gt;

&lt;p&gt;Your handler is a &lt;code&gt;readonly struct&lt;/code&gt;, and so is the dispatcher. It's generic over both the parser and the handler:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight csharp"&gt;&lt;code&gt;&lt;span class="k"&gt;internal&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt; &lt;span class="nc"&gt;BinanceDepthDispatcher&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="nc"&gt;TParser&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;THandler&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="nc"&gt;IWsStreamDispatcher&lt;/span&gt;
    &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="nc"&gt;TParser&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;IBinanceDepthParser&lt;/span&gt;
    &lt;span class="k"&gt;where&lt;/span&gt; &lt;span class="nc"&gt;THandler&lt;/span&gt; &lt;span class="p"&gt;:&lt;/span&gt; &lt;span class="k"&gt;struct&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;IBinanceDepthHandler&lt;/span&gt;
&lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nc"&gt;THandler&lt;/span&gt; &lt;span class="n"&gt;_handler&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;_lastUpdateIds&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nc"&gt;DepthLevel&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;_bids&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="k"&gt;private&lt;/span&gt; &lt;span class="k"&gt;readonly&lt;/span&gt; &lt;span class="nc"&gt;DepthLevel&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;?&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;_asks&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

    &lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="nc"&gt;MethodImpl&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="nc"&gt;MethodImplOptions&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AggressiveInlining&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;public&lt;/span&gt; &lt;span class="nc"&gt;WsDispatchResult&lt;/span&gt; &lt;span class="nc"&gt;Dispatch&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;subscription&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="nc"&gt;ReadOnlySpan&lt;/span&gt;&lt;span class="o"&gt;&amp;lt;&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="o"&gt;&amp;gt;&lt;/span&gt; &lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
    &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;ref&lt;/span&gt; &lt;span class="nc"&gt;DepthLevel&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;bids&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;ref&lt;/span&gt; &lt;span class="n"&gt;_bids&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;subscription&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;ref&lt;/span&gt; &lt;span class="nc"&gt;DepthLevel&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="n"&gt;asks&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;ref&lt;/span&gt; &lt;span class="n"&gt;_asks&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;subscription&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;!&lt;/span&gt;&lt;span class="nc"&gt;TParser&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;TryParse&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;data&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="nc"&gt;DepthUpdateHeader&lt;/span&gt; &lt;span class="n"&gt;header&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;ref&lt;/span&gt; &lt;span class="n"&gt;bids&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;bidCount&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;ref&lt;/span&gt; &lt;span class="n"&gt;asks&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;out&lt;/span&gt; &lt;span class="kt"&gt;int&lt;/span&gt; &lt;span class="n"&gt;askCount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;WsDispatchResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Unreadable&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="k"&gt;ref&lt;/span&gt; &lt;span class="kt"&gt;long&lt;/span&gt; &lt;span class="n"&gt;lastUpdateId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="k"&gt;ref&lt;/span&gt; &lt;span class="n"&gt;_lastUpdateIds&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;subscription&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;

        &lt;span class="k"&gt;if&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;header&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FinalUpdateId&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="n"&gt;lastUpdateId&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;
        &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;WsDispatchResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Handled&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="p"&gt;}&lt;/span&gt;

        &lt;span class="n"&gt;lastUpdateId&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;header&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;FinalUpdateId&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="n"&gt;_handler&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;OnData&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;subscription&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="k"&gt;in&lt;/span&gt; &lt;span class="n"&gt;header&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bids&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AsSpan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;bidCount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;asks&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;AsSpan&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="mi"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;askCount&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
        &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="nc"&gt;WsDispatchResult&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nc"&gt;Handled&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt;
    &lt;span class="p"&gt;}&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;When the type arguments are structs, .NET doesn't share generic code between them. The JIT (or the AOT compiler, ahead of time) builds a separate copy of this method for your exact parser and handler. In that copy &lt;code&gt;OnData&lt;/code&gt; is a direct call to a known method, and if it's small it gets inlined right into the receive loop. The parser is a &lt;code&gt;static abstract&lt;/code&gt; interface member, so there's no parser object either, just a static call resolved at compile time.&lt;/p&gt;

&lt;p&gt;The interfaces are there for the compiler to check. At runtime nothing goes through them: no events, no delegates, no virtual calls.&lt;/p&gt;

&lt;h3&gt;
  
  
  Zero allocations per message
&lt;/h3&gt;

&lt;p&gt;Level buffers are created once per subscription and reused. The order book uses a seqlock, so any thread can read the best price or the whole book without taking a lock and without slowing down the receive loop. Each stream message costs &lt;strong&gt;0 bytes&lt;/strong&gt;, and the GC simply never runs on the hot path.&lt;/p&gt;

&lt;h3&gt;
  
  
  Native AOT from the start
&lt;/h3&gt;

&lt;p&gt;The build enforces trimming and AOT compatibility, so reflection is a compile error instead of a surprise in production. The AOT build actually has the best p50 and p99 on every stream I measured: 84 µs and 198 µs on book ticker at live market pace.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why I care most about the tail
&lt;/h2&gt;

&lt;p&gt;At the median every client looks okay. The network and the kernel cost everyone the same, so the gap there is just 1.2x to 2x.&lt;/p&gt;

&lt;p&gt;Bursts are a different story. When the market moves fast, messages come in clumps, and your tail latency is roughly the time per message multiplied by the size of the burst. If a client handles each message 10x faster, it clears a burst 10x faster. That's why at p99.9 on the busiest stream the gap grows to &lt;strong&gt;16x to 43x&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;Trading bots rarely lose money on the average message. They lose it on the slow one, and the slow one tends to show up right when the price is moving.&lt;/p&gt;

&lt;h2&gt;
  
  
  So is Rust slow?
&lt;/h2&gt;

&lt;p&gt;No, and that's not what I'm saying. Rust is a great language. What I'm saying is that the language doesn't decide how fast your code is. Your design does.&lt;/p&gt;

&lt;p&gt;A Rust client that allocates 69 times per message will lose to a C# client that allocates nothing. Modern .NET gives you everything you need for that kind of code: structs, spans, generic specialization, &lt;code&gt;static abstract&lt;/code&gt; members, &lt;code&gt;InlineArray&lt;/code&gt;, &lt;code&gt;SearchValues&lt;/code&gt; and Native AOT. Write C# this way and you don't have to settle for "close to Rust". In this benchmark it beat both Rust clients by more than an order of magnitude.&lt;/p&gt;

&lt;h2&gt;
  
  
  Speed is only half of it
&lt;/h2&gt;

&lt;p&gt;The other half is surviving the failures no benchmark will show you. The gateway has a watchdog that catches sockets that look alive but carry no data, one network gate shared by REST, the WebSocket API and reconnects, and make-before-break connection rotation. On top of that there's a rate limiter that can't get your IP banned, and orders can't be sent twice by accident. I wrote up every one of these decisions as an ADR: the problem, what I decided, what it costs and what I rejected.&lt;/p&gt;

&lt;h2&gt;
  
  
  Try it
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;Repo: &lt;a href="https://github.com/Axitor/Trading.Gateway" rel="noopener noreferrer"&gt;github.com/Axitor/Trading.Gateway&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Why I built it: &lt;a href="https://github.com/Axitor/Trading.Gateway/blob/main/docs/adr/ADR-0001-Why-another-Binance-client.md" rel="noopener noreferrer"&gt;ADR-0001&lt;/a&gt;
&lt;/li&gt;
&lt;li&gt;Benchmark method and all tables: &lt;a href="https://github.com/Axitor/Trading.Gateway/blob/main/docs/adr/ADR-0018-Benchmarks-that-measure-the-real-thing.md" rel="noopener noreferrer"&gt;ADR-0018&lt;/a&gt;
&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;The benchmarks are reproducible. Record your own mainnet data with the recorder in &lt;code&gt;bench/tools&lt;/code&gt;, build the Rust competitors, and run one script.&lt;/p&gt;

&lt;h3&gt;
  
  
  ⭐ If you liked this, star the repo
&lt;/h3&gt;

&lt;p&gt;Seriously, it's the best way to help. Stars are how other .NET and trading folks find the project, and they tell me to keep going. Next up: the rest of the Binance API, FIX, and more exchanges on the same core.&lt;/p&gt;

&lt;p&gt;👉 &lt;strong&gt;&lt;a href="https://github.com/Axitor/Trading.Gateway" rel="noopener noreferrer"&gt;Star Axitor.Trading.Gateway on GitHub&lt;/a&gt;&lt;/strong&gt;&lt;/p&gt;




&lt;p&gt;&lt;em&gt;I'm Daniel Lebedev, a Principal .NET Architect with 25+ years in low-latency and high-load systems. I make .NET systems faster and more reliable, and I'm open to remote roles and contracts. &lt;a href="https://www.linkedin.com/in/axitor/" rel="noopener noreferrer"&gt;LinkedIn&lt;/a&gt;&lt;/em&gt;&lt;/p&gt;

</description>
      <category>dotnet</category>
      <category>csharp</category>
      <category>rust</category>
      <category>performance</category>
    </item>
  </channel>
</rss>
