<?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: Maksym Kosenko</title>
    <description>The latest articles on DEV Community by Maksym Kosenko (@maxie7).</description>
    <link>https://dev.to/maxie7</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%2F1203290%2F3c845a14-d5a7-427f-be99-62d5fa36ced1.jpg</url>
      <title>DEV Community: Maksym Kosenko</title>
      <link>https://dev.to/maxie7</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/maxie7"/>
    <language>en</language>
    <item>
      <title># 56 seconds to 0.7: when per-line cost grows with the number of sources</title>
      <dc:creator>Maksym Kosenko</dc:creator>
      <pubDate>Wed, 25 Nov 2026 20:36:40 +0000</pubDate>
      <link>https://dev.to/maxie7/-56-seconds-to-07-when-per-line-cost-grows-with-the-number-of-sources-o3h</link>
      <guid>https://dev.to/maxie7/-56-seconds-to-07-when-per-line-cost-grows-with-the-number-of-sources-o3h</guid>
      <description>&lt;p&gt;logscry is my open-source log triage tool in Go. Before a line is templated and scored, a grouping stage folds multi-line stack traces into one event. To do that it keeps a small buffer per log source and stream, and releases each one after 200 ms of silence.&lt;/p&gt;

&lt;p&gt;On my laptop's real logs it never held more than 20 buffers at once. Then I replayed a capture with 80,000 distinct source names on a single timestamp. It took &lt;strong&gt;57 seconds&lt;/strong&gt;.&lt;/p&gt;

&lt;h2&gt;
  
  
  Why would anyone have 80,000 sources?
&lt;/h2&gt;

&lt;p&gt;Because the source name is not always yours to choose. A journald entry carries its own identifier, so any program can log under as many names as it likes. A Docker host running &lt;code&gt;docker run --rm&lt;/code&gt; loops creates a new container name every few seconds. And a log tool should assume that a stream can be hostile.&lt;/p&gt;

&lt;h2&gt;
  
  
  The bug
&lt;/h2&gt;

&lt;p&gt;On every line, the grouper looked for buffers that had gone idle:&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="c1"&gt;// per line, before main's fix&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;key&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="k"&gt;range&lt;/span&gt; &lt;span class="nf"&gt;inOrder&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;buffers&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="kc"&gt;true&lt;/span&gt;&lt;span class="p"&gt;,&lt;/span&gt; &lt;span class="n"&gt;expired&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt; &lt;span class="c1"&gt;// walk the map, filter, sort&lt;/span&gt;
    &lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="nf"&gt;emit&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;g&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;buffers&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;key&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;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;&lt;code&gt;inOrder&lt;/code&gt; walked every buffer, kept the expired ones and sorted them. That is O(n log n) per line, which makes a stream of distinct names quadratic overall. Live, the timer walked all the buffers a second time to find the next deadline. And on a single timestamp nothing ever expires, so all 80,000 buffers stayed in memory until the end of input.&lt;/p&gt;

&lt;h2&gt;
  
  
  The fix, in three parts
&lt;/h2&gt;

&lt;p&gt;&lt;strong&gt;1. A deadline heap with the old order.&lt;/strong&gt; A &lt;code&gt;container/heap&lt;/code&gt; sorted by (deadline, arrival sequence), which is exactly the order the old sort used. The idle flush pops while the top has expired, and the timer only peeks at the top. This matches the old behaviour because expiry is monotone in the last-seen time: if one buffer has expired, every buffer with an earlier deadline has expired too.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;2. A bound that emits instead of dropping.&lt;/strong&gt; At most 4,096 buffers and 16 MiB. Past either limit, the least recently &lt;em&gt;touched&lt;/em&gt; buffer is released early. I deliberately did not use the heap's top for this: on a single timestamp the top is the oldest header even if a stack frame just landed on it, so evicting the top would split the trace in progress every time. An early flush emits the event, so no line is ever lost. The worst case is a trace split in two.&lt;/p&gt;

&lt;p&gt;&lt;strong&gt;3. The nil.&lt;/strong&gt; This is the classic &lt;code&gt;container/heap&lt;/code&gt; trap:&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;h&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;deadlines&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="nc"&gt;Pop&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="kt"&gt;any&lt;/span&gt; &lt;span class="p"&gt;{&lt;/span&gt;
    &lt;span class="n"&gt;old&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt;
    &lt;span class="n"&gt;n&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="nb"&gt;len&lt;/span&gt;&lt;span class="p"&gt;(&lt;/span&gt;&lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="p"&gt;)&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt; &lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="n"&gt;p&lt;/span&gt; &lt;span class="o"&gt;:=&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="kc"&gt;nil&lt;/span&gt; &lt;span class="c1"&gt;// without this, the backing array keeps every popped buffer alive&lt;/span&gt;
    &lt;span class="n"&gt;p&lt;/span&gt;&lt;span class="p"&gt;.&lt;/span&gt;&lt;span class="n"&gt;index&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="o"&gt;-&lt;/span&gt;&lt;span class="mi"&gt;1&lt;/span&gt;
    &lt;span class="o"&gt;*&lt;/span&gt;&lt;span class="n"&gt;h&lt;/span&gt; &lt;span class="o"&gt;=&lt;/span&gt; &lt;span class="n"&gt;old&lt;/span&gt;&lt;span class="p"&gt;[&lt;/span&gt;&lt;span class="p"&gt;:&lt;/span&gt;&lt;span class="n"&gt;n&lt;/span&gt;&lt;span class="p"&gt;]&lt;/span&gt;
    &lt;span class="k"&gt;return&lt;/span&gt; &lt;span class="n"&gt;p&lt;/span&gt;
&lt;span class="p"&gt;}&lt;/span&gt;
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Without that line, the heap held &lt;strong&gt;17.4 MiB&lt;/strong&gt; after every buffer had already been emitted, and the version with the nil held 0.68 MiB. A test now checks that nothing emitted stays reachable.&lt;/p&gt;

&lt;h2&gt;
  
  
  Proving nothing else moved
&lt;/h2&gt;

&lt;p&gt;A performance fix that changes output is a bug fix you didn't ask for. So:&lt;/p&gt;

&lt;ul&gt;
&lt;li&gt;The old grouper is copied &lt;strong&gt;verbatim&lt;/strong&gt; into a test file as a reference. A differential test feeds both implementations random interleavings of sources, streams, folds and timestamps, and compares every emitted line, timestamp included.&lt;/li&gt;
&lt;li&gt;A fuzz target runs the same comparison: 8.6 million executions, 0 differences.&lt;/li&gt;
&lt;li&gt;Ten real journald boots (63,017 entries) replay &lt;strong&gt;byte-identical&lt;/strong&gt;: export, stdout and stderr.&lt;/li&gt;
&lt;/ul&gt;

&lt;h2&gt;
  
  
  Results
&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;before&lt;/th&gt;
&lt;th&gt;after&lt;/th&gt;
&lt;/tr&gt;
&lt;/thead&gt;
&lt;tbody&gt;
&lt;tr&gt;
&lt;td&gt;Replay, 80k sources on one timestamp&lt;/td&gt;
&lt;td&gt;56.9 s&lt;/td&gt;
&lt;td&gt;0.72 s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Live, distinct sources&lt;/td&gt;
&lt;td&gt;14,358 lines/s&lt;/td&gt;
&lt;td&gt;1,522,116 lines/s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Live, one source&lt;/td&gt;
&lt;td&gt;1.09 M lines/s&lt;/td&gt;
&lt;td&gt;1.10 M lines/s&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;Heap, 80k pending sources&lt;/td&gt;
&lt;td&gt;20–330 MiB&lt;/td&gt;
&lt;td&gt;1.6–17 MiB&lt;/td&gt;
&lt;/tr&gt;
&lt;tr&gt;
&lt;td&gt;One ordinary line&lt;/td&gt;
&lt;td&gt;771 ns&lt;/td&gt;
&lt;td&gt;861 ns&lt;/td&gt;
&lt;/tr&gt;
&lt;/tbody&gt;
&lt;/table&gt;&lt;/div&gt;

&lt;p&gt;The last row is the honest one: an ordinary line now costs about 90 ns more. I'll take that in exchange for a cost that no longer depends on how many sources are open.&lt;/p&gt;

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

&lt;ul&gt;
&lt;li&gt;
&lt;strong&gt;Per-line work must not depend on the size of your state.&lt;/strong&gt; The tests count the buffers each line touches, so this can't come back unnoticed.&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;Keep the old implementation as an oracle.&lt;/strong&gt; A verbatim copy in a test, plus fuzzing, beats "the tests still pass".&lt;/li&gt;
&lt;li&gt;
&lt;strong&gt;When you bound something, decide what eviction costs.&lt;/strong&gt; Here an evicted buffer is emitted early, never dropped.&lt;/li&gt;
&lt;/ul&gt;

&lt;p&gt;logscry is Apache-2.0: &lt;a href="https://github.com/maxie7/logscry" rel="noopener noreferrer"&gt;https://github.com/maxie7/logscry&lt;/a&gt;&lt;/p&gt;

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