10/32
Part 3 of 6 · A growing windowLearn and do

Left only moves forward, so the inner loop runs at most n times in all

A while inside a for looks like O(n²). Here it isn't. Count what happened to each entry in the trace above:

IndexKeyAdded atDropped at
0u302
1u715
2u327
3u137
4 to 7u9, u7, u2, u14 to 7never
in all8 adds4 drops

Each entry is added once, when right reaches it. It's dropped at most once, when left passes it, because left never moves back. So the inner loop runs at most n times over the whole pass, not n times per step. On a million random keys drawn from 1,000 clients, it removed 999,939 entries in total.

The whole pass is O(n) time, with memory for the keys in the window. Here it is on logs where every key is different, the worst case for starting over:

nEvery startWindow with a setWindow with last positions
1,00013.2 ms0.111 ms0.111 ms
2,00053.4 ms0.229 ms0.234 ms
4,000213.8 ms0.455 ms0.463 ms

Write drops: your loop from windows, with a counter in its while loop, so you see how many entries it removes over a whole pass.