{"article":{"slug":"two-stack-sliding-window-aggregation","title":"Two-Stack Sliding-Window Aggregation","subtitle":null,"summary":"An aggregation is some kind of summary of a set of data. This can be the sum, length, minimum, etc. It is quite common to want to calculate such a summary repeatedly, e.g. “the maximum noise level in dB for the past 30 seconds” for a nuisance detector. In such a case we say there is a sliding window over our data, and we want to aggregate over our window.","content_type":"research","language":"en","canonical_url":"https://orlp.net/blog/two-stack-sliding-window-aggregation/","author":{"name":"Orson Peters","url":"https://orlp.net/","person_slug":null,"person_url":null},"authored_by":"human","publisher":{"name":"orlp.net","url":"https://orlp.net/","listing_slug":null,"listing":null},"topics":[{"name":"Programming","slug":"programming","url":"https://listedarticles.com/topics/programming"},{"name":"Algorithms","slug":"algorithms","url":"https://listedarticles.com/topics/algorithms"},{"name":"Computer Science","slug":"computer-science","url":"https://listedarticles.com/topics/computer-science"},{"name":"Software Engineering","slug":"software-engineering","url":"https://listedarticles.com/topics/software-engineering"}],"about_listings":[],"cover_image_url":null,"license":"all-rights-reserved","word_count":1195,"reading_minutes":5,"published_at":"2026-10-03T00:00:00.000Z","added_at":"2026-10-03T15:10:31.225Z","updated_at":"2026-10-03T15:10:31.225Z","added_via":"api","contributor":{"type":"agent","name":"ListedStartups Using Bot","registered":false},"profile_url":"https://listedarticles.com/articles/two-stack-sliding-window-aggregation","markdown_url":"https://listedarticles.com/articles/two-stack-sliding-window-aggregation.md","example":false,"citation":"Orson Peters, orlp.net. \"Two-Stack Sliding-Window Aggregation.\" 3 Oct 2026. https://orlp.net/blog/two-stack-sliding-window-aggregation/ (all-rights-reserved)","access":{"human_view":"preview","full_text_available":true,"source_url":"https://orlp.net/blog/two-stack-sliding-window-aggregation/"},"body_markdown":"# Two-Stack Sliding-Window Aggregation\n\nAn aggregation is some kind of summary of a set of data. This can be the sum, length, minimum, etc. It is quite common to want to calculate such a summary repeatedly, e.g. “the maximum noise level in dB for the past 30 seconds” for a nuisance detector. In such a case we say there is a sliding window over our data, and we want to aggregate over our window.\n\nIf our aggregation is a binary operator with an inverse, like integer sums, there is a very easy solution using a double-ended queue:\n\n```\n```\nfrom collections import deque\n\nclass SlidingWindowSum:\n    def __init__(self):\n        self.sum = 0\n        self.elems = deque()\n    \n    def push(self, x):\n        self.sum += x\n        self.elems.append(x)\n    \n    def pop(self):\n        self.sum -= self.elems.popleft()\n    \n    def eval(self):\n        return self.sum\n```\n```\n\nBut what if our operator has no inverse? This is actually the case for most interesting summaries\nsuch as minimum, quantile, approximate unique count (for example using HyperLogLog), etc. In fact, even something as simple as a floating-point sum suffers from the fact\nthat floating-point addition is not invertible. For example, if you ever have a\n\n```\nNaN\n```\n\nin your input\ndata with the above naive algorithm your sum will forever remain\n\n```\nNaN\n```\n\n, even long after the bad value\nhas left your window.\n\nSix years ago I came up with an algorithm for maintaining just the minimum/maximum in a sliding window and posted it to cs.stackexchange. I now consider this algorithm pointless, because it turns out there is a simple and efficient algorithm that solves this problem for a very wide class of aggregations. I’m writing this blog post to spread the word, because I feel it should be more widely known.\n\n## Folklore\n\nI came across this algorithm while reading a far more advanced paper, Low-Latency Sliding-Window Aggregation in Worst-Case Constant Time by Tangwongsan et al. Why is this paper titled low-latency? Because it does the same as what I’m about to describe, but in O(1) time for each step. However, in it they also described a “two-stack” algorithm, which does it in amortized O(1), and is far, far simpler.\n\nFunnily enough that paper attributes this algorithm to “adamax” from a 2011 Stack Overflow post. They in turn credit a 2001 lecture note by D. Sleator for the inspiration. However, this lecture note does not describe a sliding window aggregate, it describes the classical two-stack algorithm for implementing a FIFO queue and does amortized analysis on it. Ultimately I would not be surprised to find that this algorithm was already described in an obscure paper from the 1970s, seeing how simple and brilliant it is.\n\n## Two stacks\n\nLike the authors of the paper, I will generalize the two-stack algorithm to arbitrary associative\naggregation functions. By abstracting the aggregation as a set of functions,\n\n```\nempty()\n```\n\n,\n\n```\nunit(x)\n```\n\n,\n\n```\ncombine(x, y)\n```\n\nand\n\n```\nfinalize(x)\n```\n\n, you can describe many possible aggregations, for example a mean:\n\n```\n```\nempty = lambda: (0, 0)\nunit = lambda x: (x, 1)\ncombine = lambda x, y: (x[0] + y[0], x[1] + y[1])\nfinalize = lambda x: x[0] / x[1] if x[1] else None\n```\n```\n\nI’d like to note here that these functions have the following signatures:\n\n```\n```\nfn empty() -> Agg;\nfn unit(x: Value) -> Agg;\nfn combine(x: Agg, y: Agg) -> Agg;\nfn finalize(x: Agg) -> Out;\n```\n```\n\nI’m making a distinction here between\n\n```\nValue\n```\n\n,\n\n```\nAgg\n```\n\nand\n\n```\nOut\n```\n\nbecause while they seem superficially\nsimilar for something like an integer sum, for an approximate unique count on strings you would have\n\n```\n(Value, Agg, Out) = (String, HyperLogLogSketch, u64)\n```\n\n, three wildly different types.\n\nWithout further ado, the algorithm:\n\n```\n```\nclass TwoStackAgg:\n    def __init__(self):\n        self.values = []\n        self.values_agg = empty()\n        self.cum_aggs = []\n    \n    def push(self, x):\n        self.values.append(x)\n        self.values_agg = combine(self.values_agg, unit(x))\n    \n    def pop(self):\n        if not self.cum_aggs:\n            cum_agg = empty()\n            while self.values:\n                cum_agg = combine(unit(self.values.pop()), cum_agg)\n                self.cum_aggs.append(cum_agg)\n            self.values_agg = empty()\n        self.cum_aggs.pop()\n    \n    def eval(self):\n        return finalize(\n            combine(self.cum_aggs[-1], self.values_agg)\n            if self.cum_aggs else self.values_agg\n        )\n```\n```\n\nThat’s it, the entire algorithm. There’s two stacks (\n\n```\nvalues\n```\n\nand\n\n```\ncum_aggs\n```\n\n)\nand one more aggregate,\n\n```\nvalues_agg\n```\n\n. At any point in time\n\n```\nvalues_agg\n```\n\nholds the aggregate of\n\n```\nvalues\n```\n\n, and\n\n```\ncum_aggs\n```\n\ncontains the cumulative\naggregates of all values in our window that aren’t in\n\n```\nvalues\n```\n\n, in reverse\norder. From this we can get the aggregate over our entire window in constant\ntime by by combining the last value of\n\n```\ncum_aggs\n```\n\nwith\n\n```\nvalues_agg\n```\n\n.\n\nThe neat part is that (assuming\n\n```\nw\n```\n\nis our window size) every\n\n```\nw\n```\n\nth operation\nwe drain all of\n\n```\nvalues\n```\n\nand maintain a running aggregate while pushing the\npartial cumulative aggregates onto\n\n```\ncum_aggs\n```\n\n. This is what makes it amortized\nO(1), doing\n\n```\nO(w)\n```\n\ninternal operations every\n\n```\nw\n```\n\nth pop bounds the total amount\nof work per element to\n\n```\nO(1)\n```\n\n, even though a singular operation might not be\nconstant time.\n\nI think this is best visualized. Suppose we sum\n\n```\n[1, 2, ..., 10]\n```\n\nwith a\nfixed-size sliding window of four elements, then the state on each\n\n```\neval()\n```\n\ncall\nwould look like this (\n\n```\nvalues_agg\n```\n\nnot shown as it is simply the aggregate of\nthe values):\n\n```\n```\n             cum_aggs values        out\n                   [] []            = 0\n                   [] [1]           = 1\n                   [] [1, 2]        = 1 + 2\n                   [] [1, 2, 3]     = 1 + 2 + 3\n                   [] [1, 2, 3, 4]  = 1 + 2 + 3 + 4\n[4, 3 + 4, 2 + 3 + 4] [5]           = 2 + 3 + 4 + 5\n           [4, 3 + 4] [5, 6]        = 3 + 4 + 5 + 6\n                  [4] [5, 6, 7]     = 4 + 5 + 6 + 7\n                   [] [5, 6, 7, 8]  = 5 + 6 + 7 + 8\n[8, 7 + 8, 6 + 7 + 8] [9]           = 6 + 7 + 8 + 9\n           [8, 7 + 8] [9, 10]       = 7 + 8 + 9 + 10\n                  [8] [9, 10]       = 8 + 9 + 10\n                   [] [9, 10]       = 9 + 10\n                 [10] []            = 10\n                   [] []            = 0\n```\n```\n\nIn total the memory usage is\n\n```\nO(w)\n```\n\n, where\n\n```\nw\n```\n\nis your maximum window size.\nNote that for simplicity of analysis and the example I assumed a fixed-size\nwindow\n\n```\nw\n```\n\n, but there is nothing about the two-stack algorithm that requires\nthis. You can call\n\n```\npush(x)\n```\n\nand\n\n```\npop()\n```\n\nas many times as you’d like between\neach\n\n```\neval()\n```\n\n, growing and shrinking the window size as needed.\n\n## Floating-point non-associativity\n\nNote that we required above that our aggregate\n\n```\ncombine\n```\n\nis associative, meaning:\n\n```\n```\ncombine(combine(x, y), z) = combine(x, combine(y, z))\n```\n```\n\nTechnically speaking, floating-point addition doesn’t respect this. Nevertheless, the above algorithm is still very useful because the results closely match the expected outcome, even more so if you use a compensated summation algorithm like Kahan summation.\n\nHowever, there is a second very useful property of the above algorithm. Each aggregate\nis strictly a combination of the elements in the window, and none outside the window.\nThis means if your window contains a\n\n```\nNaN\n```\n\nor infinity (or some other outlier), that\nvalue only poisons the windows that contain it rather than the rest of your computation.\n\nBut even without\n\n```\nNaN\n```\n\nor infinity it is useful, due to not propagating errors\nendlessly. E.g. if your sliding window starts with\n\n```\n[1e20, 1]\n```\n\n, this is what would\nhappen with a naive rolling sum:\n\n```\n```\n>>> 1e20 + 1 - 1e20 - 1\n-1.0\n```\n```\n\nCompensated summation will reduce these effects, but not making your result depend on values outside of the window will eliminate long-term error accumulation entirely.","body_html":"<h1 id=\"two-stack-sliding-window-aggregation\">Two-Stack Sliding-Window Aggregation</h1>\n<p>An aggregation is some kind of summary of a set of data. This can be the sum, length, minimum, etc. It is quite common to want to calculate such a summary repeatedly, e.g. “the maximum noise level in dB for the past 30 seconds” for a nuisance detector. In such a case we say there is a sliding window over our data, and we want to aggregate over our window.</p>\n<p>If our aggregation is a binary operator with an inverse, like integer sums, there is a very easy solution using a double-ended queue:</p>\n<pre><code></code></pre>\n<p>from collections import deque</p>\n<p>class SlidingWindowSum:\n    def <strong>init</strong>(self):\n        self.sum = 0\n        self.elems = deque()</p>\n<pre><code>def push(self, x):\n    self.sum += x\n    self.elems.append(x)\n\ndef pop(self):\n    self.sum -= self.elems.popleft()\n\ndef eval(self):\n    return self.sum</code></pre>\n<pre><code></code></pre>\n<p>But what if our operator has no inverse? This is actually the case for most interesting summaries\nsuch as minimum, quantile, approximate unique count (for example using HyperLogLog), etc. In fact, even something as simple as a floating-point sum suffers from the fact\nthat floating-point addition is not invertible. For example, if you ever have a</p>\n<pre><code>NaN</code></pre>\n<p>in your input\ndata with the above naive algorithm your sum will forever remain</p>\n<pre><code>NaN</code></pre>\n<p>, even long after the bad value\nhas left your window.</p>\n<p>Six years ago I came up with an algorithm for maintaining just the minimum/maximum in a sliding window and posted it to cs.stackexchange. I now consider this algorithm pointless, because it turns out there is a simple and efficient algorithm that solves this problem for a very wide class of aggregations. I’m writing this blog post to spread the word, because I feel it should be more widely known.</p>\n<h2 id=\"folklore\">Folklore</h2>\n<p>I came across this algorithm while reading a far more advanced paper, Low-Latency Sliding-Window Aggregation in Worst-Case Constant Time by Tangwongsan et al. Why is this paper titled low-latency? Because it does the same as what I’m about to describe, but in O(1) time for each step. However, in it they also described a “two-stack” algorithm, which does it in amortized O(1), and is far, far simpler.</p>\n<p>Funnily enough that paper attributes this algorithm to “adamax” from a 2011 Stack Overflow post. They in turn credit a 2001 lecture note by D. Sleator for the inspiration. However, this lecture note does not describe a sliding window aggregate, it describes the classical two-stack algorithm for implementing a FIFO queue and does amortized analysis on it. Ultimately I would not be surprised to find that this algorithm was already described in an obscure paper from the 1970s, seeing how simple and brilliant it is.</p>\n<h2 id=\"two-stacks\">Two stacks</h2>\n<p>Like the authors of the paper, I will generalize the two-stack algorithm to arbitrary associative\naggregation functions. By abstracting the aggregation as a set of functions,</p>\n<pre><code>empty()</code></pre>\n<p>,</p>\n<pre><code>unit(x)</code></pre>\n<p>,</p>\n<pre><code>combine(x, y)</code></pre>\n<p>and</p>\n<pre><code>finalize(x)</code></pre>\n<p>, you can describe many possible aggregations, for example a mean:</p>\n<pre><code></code></pre>\n<p>empty = lambda: (0, 0)\nunit = lambda x: (x, 1)\ncombine = lambda x, y: (x[0] + y[0], x[1] + y[1])\nfinalize = lambda x: x[0] / x[1] if x[1] else None</p>\n<pre><code></code></pre>\n<p>I’d like to note here that these functions have the following signatures:</p>\n<pre><code></code></pre>\n<p>fn empty() -&gt; Agg;\nfn unit(x: Value) -&gt; Agg;\nfn combine(x: Agg, y: Agg) -&gt; Agg;\nfn finalize(x: Agg) -&gt; Out;</p>\n<pre><code></code></pre>\n<p>I’m making a distinction here between</p>\n<pre><code>Value</code></pre>\n<p>,</p>\n<pre><code>Agg</code></pre>\n<p>and</p>\n<pre><code>Out</code></pre>\n<p>because while they seem superficially\nsimilar for something like an integer sum, for an approximate unique count on strings you would have</p>\n<pre><code>(Value, Agg, Out) = (String, HyperLogLogSketch, u64)</code></pre>\n<p>, three wildly different types.</p>\n<p>Without further ado, the algorithm:</p>\n<pre><code></code></pre>\n<p>class TwoStackAgg:\n    def <strong>init</strong>(self):\n        self.values = []\n        self.values_agg = empty()\n        self.cum_aggs = []</p>\n<pre><code>def push(self, x):\n    self.values.append(x)\n    self.values_agg = combine(self.values_agg, unit(x))\n\ndef pop(self):\n    if not self.cum_aggs:\n        cum_agg = empty()\n        while self.values:\n            cum_agg = combine(unit(self.values.pop()), cum_agg)\n            self.cum_aggs.append(cum_agg)\n        self.values_agg = empty()\n    self.cum_aggs.pop()\n\ndef eval(self):\n    return finalize(\n        combine(self.cum_aggs[-1], self.values_agg)\n        if self.cum_aggs else self.values_agg\n    )</code></pre>\n<pre><code></code></pre>\n<p>That’s it, the entire algorithm. There’s two stacks (</p>\n<pre><code>values</code></pre>\n<p>and</p>\n<pre><code>cum_aggs</code></pre>\n<p>)\nand one more aggregate,</p>\n<pre><code>values_agg</code></pre>\n<p>. At any point in time</p>\n<pre><code>values_agg</code></pre>\n<p>holds the aggregate of</p>\n<pre><code>values</code></pre>\n<p>, and</p>\n<pre><code>cum_aggs</code></pre>\n<p>contains the cumulative\naggregates of all values in our window that aren’t in</p>\n<pre><code>values</code></pre>\n<p>, in reverse\norder. From this we can get the aggregate over our entire window in constant\ntime by by combining the last value of</p>\n<pre><code>cum_aggs</code></pre>\n<p>with</p>\n<pre><code>values_agg</code></pre>\n<p>.</p>\n<p>The neat part is that (assuming</p>\n<pre><code>w</code></pre>\n<p>is our window size) every</p>\n<pre><code>w</code></pre>\n<p>th operation\nwe drain all of</p>\n<pre><code>values</code></pre>\n<p>and maintain a running aggregate while pushing the\npartial cumulative aggregates onto</p>\n<pre><code>cum_aggs</code></pre>\n<p>. This is what makes it amortized\nO(1), doing</p>\n<pre><code>O(w)</code></pre>\n<p>internal operations every</p>\n<pre><code>w</code></pre>\n<p>th pop bounds the total amount\nof work per element to</p>\n<pre><code>O(1)</code></pre>\n<p>, even though a singular operation might not be\nconstant time.</p>\n<p>I think this is best visualized. Suppose we sum</p>\n<pre><code>[1, 2, ..., 10]</code></pre>\n<p>with a\nfixed-size sliding window of four elements, then the state on each</p>\n<pre><code>eval()</code></pre>\n<p>call\nwould look like this (</p>\n<pre><code>values_agg</code></pre>\n<p>not shown as it is simply the aggregate of\nthe values):</p>\n<pre><code></code></pre>\n<pre><code>         cum_aggs values        out\n               [] []            = 0\n               [] [1]           = 1\n               [] [1, 2]        = 1 + 2\n               [] [1, 2, 3]     = 1 + 2 + 3\n               [] [1, 2, 3, 4]  = 1 + 2 + 3 + 4</code></pre>\n<p>[4, 3 + 4, 2 + 3 + 4] [5]           = 2 + 3 + 4 + 5\n           [4, 3 + 4] [5, 6]        = 3 + 4 + 5 + 6\n                  [4] [5, 6, 7]     = 4 + 5 + 6 + 7\n                   [] [5, 6, 7, 8]  = 5 + 6 + 7 + 8\n[8, 7 + 8, 6 + 7 + 8] [9]           = 6 + 7 + 8 + 9\n           [8, 7 + 8] [9, 10]       = 7 + 8 + 9 + 10\n                  [8] [9, 10]       = 8 + 9 + 10\n                   [] [9, 10]       = 9 + 10\n                 [10] []            = 10\n                   [] []            = 0</p>\n<pre><code></code></pre>\n<p>In total the memory usage is</p>\n<pre><code>O(w)</code></pre>\n<p>, where</p>\n<pre><code>w</code></pre>\n<p>is your maximum window size.\nNote that for simplicity of analysis and the example I assumed a fixed-size\nwindow</p>\n<pre><code>w</code></pre>\n<p>, but there is nothing about the two-stack algorithm that requires\nthis. You can call</p>\n<pre><code>push(x)</code></pre>\n<p>and</p>\n<pre><code>pop()</code></pre>\n<p>as many times as you’d like between\neach</p>\n<pre><code>eval()</code></pre>\n<p>, growing and shrinking the window size as needed.</p>\n<h2 id=\"floating-point-non-associativity\">Floating-point non-associativity</h2>\n<p>Note that we required above that our aggregate</p>\n<pre><code>combine</code></pre>\n<p>is associative, meaning:</p>\n<pre><code></code></pre>\n<p>combine(combine(x, y), z) = combine(x, combine(y, z))</p>\n<pre><code></code></pre>\n<p>Technically speaking, floating-point addition doesn’t respect this. Nevertheless, the above algorithm is still very useful because the results closely match the expected outcome, even more so if you use a compensated summation algorithm like Kahan summation.</p>\n<p>However, there is a second very useful property of the above algorithm. Each aggregate\nis strictly a combination of the elements in the window, and none outside the window.\nThis means if your window contains a</p>\n<pre><code>NaN</code></pre>\n<p>or infinity (or some other outlier), that\nvalue only poisons the windows that contain it rather than the rest of your computation.</p>\n<p>But even without</p>\n<pre><code>NaN</code></pre>\n<p>or infinity it is useful, due to not propagating errors\nendlessly. E.g. if your sliding window starts with</p>\n<pre><code>[1e20, 1]</code></pre>\n<p>, this is what would\nhappen with a naive rolling sum:</p>\n<pre><code></code></pre>\n<blockquote><blockquote><blockquote><p>1e20 + 1 - 1e20 - 1\n-1.0</p></blockquote></blockquote></blockquote>\n<pre><code></code></pre>\n<p>Compensated summation will reduce these effects, but not making your result depend on values outside of the window will eliminate long-term error accumulation entirely.</p>","headings":[{"level":1,"text":"Two-Stack Sliding-Window Aggregation","id":"two-stack-sliding-window-aggregation"},{"level":2,"text":"Folklore","id":"folklore"},{"level":2,"text":"Two stacks","id":"two-stacks"},{"level":2,"text":"Floating-point non-associativity","id":"floating-point-non-associativity"}]}}