<?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: Rohan Singh Rajput</title>
    <description>The latest articles on DEV Community by Rohan Singh Rajput (@denx00).</description>
    <link>https://dev.to/denx00</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%2F4206110%2F4ba6074e-eff1-400a-bc6e-32836aae657a.png</url>
      <title>DEV Community: Rohan Singh Rajput</title>
      <link>https://dev.to/denx00</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/denx00"/>
    <language>en</language>
    <item>
      <title>From 11 allocations to 2: speeding up a FIX engine's price encoder in Go</title>
      <dc:creator>Rohan Singh Rajput</dc:creator>
      <pubDate>Sun, 25 Oct 2026 07:43:53 +0000</pubDate>
      <link>https://dev.to/denx00/from-11-allocations-to-2-speeding-up-a-fix-engines-price-encoder-in-go-2l46</link>
      <guid>https://dev.to/denx00/from-11-allocations-to-2-speeding-up-a-fix-engines-price-encoder-in-go-2l46</guid>
      <description>&lt;p&gt;FIX is the protocol that moves orders and quotes between brokers, exchanges and trading firms. &lt;a href="https://github.com/quickfixgo/quickfix" rel="noopener noreferrer"&gt;QuickFIX/Go&lt;/a&gt; is a widely used Go implementation of it. Every price and quantity on every outgoing message passes through one small function, so I went looking at what it costs.&lt;/p&gt;

&lt;p&gt;&lt;em&gt;Status: the change is open as &lt;a href="https://github.com/quickfixgo/quickfix/pull/787" rel="noopener noreferrer"&gt;quickfixgo/quickfix#787&lt;/a&gt;, discussed in &lt;a href="https://github.com/quickfixgo/quickfix/issues/786" rel="noopener noreferrer"&gt;#786&lt;/a&gt;. I'll update this post if it changes in review.&lt;/em&gt;&lt;/p&gt;

&lt;h2&gt;
  
  
  The function
&lt;/h2&gt;

&lt;p&gt;&lt;code&gt;FIXDecimal.Write&lt;/code&gt; turns a decimal into the bytes that go on the wire:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;func&lt;/span&gt; &lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt; &lt;span class="n"&gt;FIXDecimal&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="n"&gt;Write&lt;/span&gt;&lt;span class="p"&gt;()&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;byte&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Decimal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StringFixed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;d&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Scale&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 looks harmless. The benchmark says otherwise:&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;BenchmarkFIXDecimalWrite   155 ns/op   208 B/op   11 allocs/op
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Eleven heap allocations to format &lt;code&gt;-124.3456&lt;/code&gt;. &lt;code&gt;StringFixed&lt;/code&gt; rounds through &lt;code&gt;big.Int&lt;/code&gt; arithmetic, builds intermediate strings, and then the result is copied again into a &lt;code&gt;[]byte&lt;/code&gt;. On a path that runs per field, per message, that is a lot of garbage for the collector to chase, and GC pressure is exactly what latency-sensitive systems try to avoid.&lt;/p&gt;

&lt;h2&gt;
  
  
  The idea
&lt;/h2&gt;

&lt;p&gt;Most prices fit comfortably in an &lt;code&gt;int64&lt;/code&gt; coefficient with a small exponent. For those, there is no need for &lt;code&gt;big.Int&lt;/code&gt; at all:&lt;/p&gt;

&lt;ol&gt;
&lt;li&gt;Read the coefficient and exponent of the decimal.&lt;/li&gt;
&lt;li&gt;Shift the coefficient so its last digit lands on the last requested decimal place. Shifting right means rounding; shifting left means multiplying by a power of ten.&lt;/li&gt;
&lt;li&gt;Write the digits into a small stack buffer, insert the decimal point, and copy once into an exactly-sized result.&lt;/li&gt;
&lt;/ol&gt;

&lt;p&gt;The details that matter:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Rounding must match exactly.&lt;/strong&gt; &lt;code&gt;StringFixed&lt;/code&gt; rounds half away from zero. On the magnitude that is &lt;code&gt;if 2*remainder &amp;gt;= divisor { quotient++ }&lt;/code&gt;. I wrote the comparison as &lt;code&gt;r &amp;gt;= div-r&lt;/code&gt; so it cannot overflow for divisors above 2^63.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;&lt;code&gt;MinInt64&lt;/code&gt; has no positive counterpart.&lt;/strong&gt; Negating it as a signed value overflows, so the sign is stripped on a &lt;code&gt;uint64&lt;/code&gt; magnitude instead.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Multiplying up can overflow.&lt;/strong&gt; &lt;code&gt;bits.Mul64&lt;/code&gt; reports the high word, so overflow is detected rather than silently wrapped.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Negative zero must not appear.&lt;/strong&gt; A value like &lt;code&gt;-0.001&lt;/code&gt; at scale 2 rounds to &lt;code&gt;0.00&lt;/code&gt;, not &lt;code&gt;-0.00&lt;/code&gt;.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Everything else falls back.&lt;/strong&gt; Coefficients beyond int64, negative scale, and scales of 20 or more take the original path, so behaviour is unchanged outside the fast path's range.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Proving it is the same function
&lt;/h2&gt;

&lt;p&gt;A faster function that is subtly wrong is worse than a slow one in a trading system. So the test is differential: for a set of awkward coefficients (zero, ±1, rounding ties like 5, 50 and 995, &lt;code&gt;MaxInt64&lt;/code&gt;, &lt;code&gt;MinInt64&lt;/code&gt;, and a few hundred random ones), every exponent from -25 to 25, and a spread of scales, the new output must equal &lt;code&gt;decimal.StringFixed&lt;/code&gt; byte for byte. That is on the order of a hundred thousand comparisons.&lt;br&gt;
&lt;/p&gt;

&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight go"&gt;&lt;code&gt;&lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;c&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="n"&gt;coefs&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;exp&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="kt"&gt;int32&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;25&lt;/span&gt;&lt;span class="p"&gt;);&lt;/span&gt; &lt;span class="n"&gt;exp&lt;/span&gt; &lt;span class="o"&gt;&amp;lt;=&lt;/span&gt; &lt;span class="m"&gt;25&lt;/span&gt;&lt;span class="p"&gt;;&lt;/span&gt; &lt;span class="n"&gt;exp&lt;/span&gt;&lt;span class="o"&gt;++&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
        &lt;span class="k"&gt;for&lt;/span&gt; &lt;span class="n"&gt;_&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;scale&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="p"&gt;[]&lt;/span&gt;&lt;span class="kt"&gt;int32&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;0&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;1&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;2&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;4&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;8&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;12&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;18&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;19&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="m"&gt;20&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
            &lt;span class="n"&gt;dec&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;FIXDecimal&lt;/span&gt;&lt;span class="p"&gt;{&lt;/span&gt;&lt;span class="n"&gt;Decimal&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;decimal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;New&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;c&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;exp&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="n"&gt;Scale&lt;/span&gt;&lt;span class="o"&gt;:&lt;/span&gt; &lt;span class="n"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;}&lt;/span&gt;
            &lt;span class="n"&gt;assert&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Equal&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;t&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;dec&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Decimal&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;StringFixed&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;scale&lt;/span&gt;&lt;span class="p"&gt;),&lt;/span&gt; &lt;span class="kt"&gt;string&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;dec&lt;/span&gt;&lt;span class="o"&gt;.&lt;/span&gt;&lt;span class="n"&gt;Write&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="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;It passed on the first run, which is the point of designing against the edge cases above before verifying them.&lt;/p&gt;

&lt;h2&gt;
  
  
  The result
&lt;/h2&gt;

&lt;p&gt;Measured with &lt;code&gt;benchstat&lt;/code&gt; over 10 runs on an Apple M5, Go 1.27:&lt;/p&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;before&lt;/th&gt;
&lt;th&gt;after&lt;/th&gt;
&lt;th&gt;change&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;time/op&lt;/td&gt;
&lt;td&gt;155 ns&lt;/td&gt;
&lt;td&gt;20.5 ns&lt;/td&gt;
&lt;td&gt;-86.8%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;B/op&lt;/td&gt;
&lt;td&gt;208&lt;/td&gt;
&lt;td&gt;24&lt;/td&gt;
&lt;td&gt;-88.5%&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;allocs/op&lt;/td&gt;
&lt;td&gt;11&lt;/td&gt;
&lt;td&gt;2&lt;/td&gt;
&lt;td&gt;-81.8%&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The two remaining allocations are the final result slice and the digit storage behind the &lt;code&gt;big.Int&lt;/code&gt; copy that &lt;code&gt;Coefficient()&lt;/code&gt; returns. Getting the second one to zero would need an API from the decimal library that exposes the coefficient without copying.&lt;/p&gt;

&lt;h2&gt;
  
  
  Takeaways
&lt;/h2&gt;

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Benchmark before you guess.&lt;/strong&gt; The benchmark was already in the repo; the number just needed reading.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;A fast path with a fallback is a safe way to optimise a library you do not own.&lt;/strong&gt; Behaviour outside the fast path is untouched.&lt;/li&gt;
&lt;li&gt;&lt;strong&gt;Differential testing against the slow implementation is the cheapest way to trust a rewrite.&lt;/strong&gt;&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;If you work on Go services that touch FIX, I'd like to hear what else on the hot path is worth profiling.&lt;/p&gt;

</description>
      <category>go</category>
      <category>performance</category>
      <category>opensource</category>
      <category>fintech</category>
    </item>
  </channel>
</rss>
