{'fraction_non_alphanumeric': 0.04374025121090223, 'fraction_numerical': 0.014695016829488547, 'mean_word_length': 4.246446722370575, 'pattern_counts': {'":': 0, '<': 8, '<?xml version=': 0, '>': 1, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 10, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
SALSA: Self-Adjusting Lean Streaming Analytics

Counters are the fundamental building block of many data sketching schemes, which hash items to a small number of counters and account for collisions to provide good approximations for frequencies and other measures. Most existing methods rely on fixed-size counters, which may be wasteful in terms of space, as counters must be large enough to eliminate any risk of overflow. Instead, some solutions use small, fixed-size counters that may overflow into secondary structures. This paper takes a different approach. We propose a simple and general method called SALSA for dynamic re-sizing of counters and show its effectiveness. SALSA starts with small counters, and overflowing counters simply merge with their neighbors. SALSA can thereby allow more counters for a given space, expanding them as necessary to represent large numbers. Our evaluation demonstrates that, at the cost of a small overhead for its merging logic, SALSA significantly improves the accuracy of popular schemes (such as Count-Min Sketch and Count Sketch) over a variety of tasks. Our code is released as open-source [1].


I. INTRODUCTION
Analysis of large data streams is essential in many domains, including natural language processing [2], load balancing [3], and forensic analysis [4]. Typically, the data volume renders exact analysis algorithms too expensive. However, often it is sufficient to estimate measurements such as per-item frequency [5], item distribution entropy [6], or top-k/heavy hitters [7] by using approximation algorithms often referred to as sketches. Sketching schemes reduce the space requirements by sharing counters that keep frequency counts of the (potentially multiple) associated items [8], [9].
That is, rather than use a counter for each item, which may be space-prohibitive, sketches bound the effect of collisions to guarantee good approximations.
A common approach for sketch design is to consider counters as the basic building block. Namely, the goal is to optimize the accuracy for a given number of counters (e.g., [7], [10]). However, these works do not discuss how many bits each counter should have, a quantity whose optimal value depends on the workload and optimization metric.
For fixed-size counters, if they are too large, space is wasted. Conversely, if they are too small, there are risks of overflow. Instead, some solutions use small fixed-size counters that may overflow into secondary structures (e.g., [11], [12]).
Our Contributions: We present Self-Adjusting Lean Streaming Analytics (SALSA), a simple and general framework for dynamic re-sizing of counters. In a nutshell, SALSA starts with small (e.g., 8-bit) counters and merges overflowing ones with their neighbors to represent larger numbers. This way, more counters fit in a given space without limiting the counting range. To do so efficiently, we employ novel methods for representing merges with low memory and computation overheads. These methods also respects byte boundaries making them readily implementable in software and some hardware platforms.
SALSA integrates with popular sketches and probabilistic counter-compression techniques to improve their precision to memory tradeoff. We prove that SALSA stochastically improves the accuracy of standard schemes, including the Count Min Sketch [10], the Conservative Update Sketch [13], and the Count Sketch [14]. Using different workloads, metrics, and tasks, we also show significant accuracy improvements for the above schemes as well as for state of the art solutions like Univmon [15], Cold Filter [9], and AEE [16]. We also compare against Pyramid Sketch [12] and ABC [17], recent variable-counter-size solutions, and show that SALSA is more accurate than both. Finally, we release our code as open source [1].

II. RELATED WORK
The term sketch here informally describes an algorithm that uses shared counters, such that each item is associated with a subset of the counters via hash functions [10], [13]- [15]. Sketches offer tradeoffs between update speed, accuracy, and space, where each of these parameters is important in some scenarios. For example, in software-based network measurement, we are primarily concerned about update speed [18]. Conversely, in hardware-based measurements, space is often the bottleneck [19], [20].
Some sketches optimize the update speed at the expense of space. For example, Randomized Counter Sharing [21] uses multiple hash functions but only updates a random one. NitroSketch [18] extends this idea and only performs updates for sampled packets using a novel sampling technique that asymptotically improves over uniform sampling. Other solutions aim to maximize the accuracy for a given space allocation. For example, Counter Braids [22] and Counter Tree [23] aim to fit into the fast static RAM (SRAM) while optimizing the precision. These solutions estimate element sizes using complex offline procedures that, while being highly accurate, may be too slow for online applications.
Most relevant to our setting are ABC [17] and Pyramid Sketch [12], which vary the size of counters on the fly. In ABC, an overflowing counter is allowed to "borrow" bits from the next counter. If there are not enough bits to represent both values, the counters "combine" to create a larger counter. However, the encoding of ABC is cumbersome. It requires three bits to mark combined counters (e.g., when starting with 8-bits, combined counters can count to 2 13 − 1) and slows the sketch down significantly (see Section VI). Moreover, it does not allow counters to combine more than once. Pyramid Sketch [12] has several layers for extending overflowing counters. An overflowing counter increases a counter at the next layer. Each pair of same-layer counters are associated with a single counter at the next layer. If both overflow, they will share their most significant bits in that counter while keeping the least significant bits separately. Critically, the counters of all layers are pre-allocated regardless of the access patterns. This results in inferior memory utilization since many of the upper layers' counters may never be used. Further, when reading a counter, Pyramid may make multiple non-sequential memory accesses, thus slowing the processing down. SALSA improves over these solutions due to its efficient encoding and the fact that its counting range is not limited by the initial configuration (e.g., counter size).
An orthogonal line of works reduces the size of counters by using probabilistic estimators that only increment their value with a certain probability on an update [16], [24]- [26]. Such an approach saves space as estimators can represent large numbers with fewer bits, at the cost of a higher error.

III. PRELIMINARIES
We consider a data stream S consisting of updates in the form of x, v , where x ∈ U is an element (or item) and v ∈ Z is a value. Here, U {1, . . . , u} is the universe and u is the universe size. For x ∈ {1, . . . , u}, f x x,v ∈S v denotes the frequency of x. Additionally, f f 1 , . . . , f u is the frequency vector of S. We denote by N x∈U |f x | the volume of the stream. The above is called the Turnstile model. Other models include the Strict Turnstile model, where frequencies are non-negative at all times, and the Cash Register model, where updates are strictly positive.
The p'th moment of the frequency vector is defined as F p x∈U |f i | p (e.g., F 1 = N ) and the p'th norm (defined for p ≥ 1) is L p p √ F p . We say that an algorithm estimates frequencies with an ( , δ) L p guarantee if for any element x ∈ U it produces an estimate f x that satisfies Throughout the paper, we assume the standard RAM model and that each counter value fits into O(1) machine words.
We survey several popular sketches that SALSA extends. Count Min Sketch (CMS) [10]: CMS is arguably the simplest and most popular sketch. It provides an L 1 guarantee in the Strict Turnstile model. The sketch consists of a d × w matrix C of counters and d random hash functions h 1 , . . . , h d : U → [w] that map elements into counters. Each element x is associated with one counter in each row: When processing the update x, v , CMS adds v to all of x's counters. Since CMS operates in the Strict Turnstile model where all frequencies are non-negative, each of x's counters provides an over-estimation for its true frequency (i.e., ∀i ∈ [d] : Therefore, CMS uses the minimum of x's counters to estimate For its analysis, denote by Notice that E[E i ] = N −fx w ≤ N w , and according to Markov's inequality we have that We note that CMS, like all the algorithms below, provides a curve of guarantees, in that setting δ determines the value for which we have an ( , δ) guarantee with the d × w configuration. Setting = δ −1/d /w and c = δ −1/d , equation (1) gives that Pr[E i ≥ N ] ≤ δ 1/d , and as the d rows are independent we get that Pr[∀i : For fixed ( , δ) values, setting w = e/ and d = ln δ −1 minimizes the space required by the sketch, but CMS is often configured with a smaller number of rows d since its update and query time are O(d).
Conservative Update Sketch (CUS) [13]: CUS improves the accuracy of CMS but is restricted to the Cash Register model. Intuitively, when all the update values are positive, we may not need to increase all the counters of the current element. For example, assume that C[1, h 1 (x)] = 7 and C[2, h 2 (x)] = 4, and the update x, 2 arrives. In such a scenario, we know that f x ≤ 4 before the update, and thus should not increase C[1, h 1 (x)]. In general, given an update x, v , CUS sets each counter is the estimate for x before the update. While CUS improves the accuracy of CMS, its updates are slower due to the need to compute f x before increasing the counters. Since an estimate of CUS is always bounded by CMS's estimates from above (and by f x from below), the analysis of CMS holds for CUS as well. We refer the reader to [27] for a refined analysis.
Count Sketch (CS) [14]: CS works in the more general Turnstile model and provides the stronger L 2 guarantee. As with CMS and CUS, each element x is associated with a set of counters {C[i, h i (x)]} i∈ [d] . However, the update process is slightly different. Each row i ∈ [d] in CS has another pairwise independent hash function g i : U → {+1, −1} that associates each element with a sign. When processing an update x, v , CS increases each counter C[i, h i (x)] by v · g i (x). Intuitively, this "unbiases" the noise from all other elements as they increase or decrease the counters with equal probabilities. As a result, each counter now gives an unbiased estimate and therefore CS estimates the size as Assuming without loss of generality that g i (x) = 1, the standard CS analysis bounds the error of the i'th row, Universal Sketch (UnivMon) [15], [28]: UnivMon summarizes the data once and supports many functions of the frequency vectors (e.g., its entropy or number of non-zero entries) in the Cash Register model. Importantly, when using UnivMon, we provide a function G : Z → R as an input, and estimate the G-sum, given by x∈U G(f x ). Not all functions of the frequency vector can be computed in poly-log space in a one-pass streaming setting (a class called Stream-PolyLog). The surprising result of [28] is that any function G in Stream-PolyLog is supported by UnivMon.
UnivMon leverages O(log u) sketches with an L 2 guarantee (e.g., Count Sketch), which are applied to different subsets of the universe. We refer the reader to [15], [28] for details.
Cold Filter [9]: A recent framework for fast and accurate stream processing. It consists of two stages, where the first stage is designed to filter cold items and the second measures heavy hitters accurately. To accelerate the computation, it uses an aggregation buffer and employs SIMD parallelism.
Finding Heavy Hitters: Often, we care about finding the most significant elements in a data stream, which has applications for load balancing [3], accounting, and security. That is, in addition to estimating the frequency of elements, we wish to track the most frequent elements without needing to query each x ∈ U . For p ≥ 1, the L p -heavy hitter problem asks to return all elements with frequency larger than θL p and no element smaller than (θ − )L p , where θ ∈ [0, 1] is given at query time. In the Cash Register model, we can store a min-heap with the 1/ elements with the highest estimates. Whenever an update arrives, we query the item and update the heap if necessary. As a result, we can find the L 1 heavy hitters using CMS and CUS, or the L 2 heavy hitters using CS.
Counting Distinct Items: Estimating the number of distinct items in a data stream (defined as F 0 ≡ f 0 ) is a fundamental primitive for applications such as discovering denial of service attacks [29]. While UnivMon can natively support such a function, we can also estimate it from CMS and CUS. By observing the fraction of zero-valued counters in a sketch's row p, we can estimate the number of distinct elements (as additional occurrences of the same element would not change this quantity). Specifically, a common approach (e.g., [30]) is use the Linear Counting algorithm [31] that estimates the distinct count as  [31] that improves when w grows.

IV. TECHNIQUES
The description of the above sketches does not address the fundamental question of sizing the counters. A common practice is to assume some upper bound on the maximal frequency (e.g., N ) and allocate each counter with n = O(log N ) bits. For performance, this upper bound is often rounded up to be a multiple of the word size. For example, practitioners often allocate 32-bit counters when estimating the unit-count of elements, and 64-bit counters for measuring their weighted-frequency (e.g., [32], [33]). When space is tight, estimators are sometimes integrated into sketches to allow smaller (e.g., 16-bit) per-counter overhead at the cost of additional error [16]. However, these solutions miss the potential of allowing counters' bit sizes to vary and adjust dynamically. Intuitively, the largest counter value is often considerably larger than the average value, especially in highly skewed workloads where many counter values remain small as most of the volume belongs to a small set of heavy hitters.
Alternatively, one can use address-calculation coding (e.g., see [34], [35]) to encode a variable length counter array in near-optimal space (compared to the information theoretic lower bound). Such schemes require an upper bound N max on the volume, and use w log 2 (1 + N max /w) + O(w). However, the update time of such encoding is Ω(log 2 N max ) which may be prohibitive for high-performance applications. To the best of our knowledge, no implementation that combines such encoding with sketches has been proposed. In comparison, SALSA allows for dynamic counter sizing by merging overflowing counters with their neighbors, and optimizes for performance by respecting word alignments. A simple SALSA encoding requires one bit per counter, and an optimized encoding requires less than 0.6 bits per counter while still allowing for constant-time read and update operations. Importantly, SALSA resolves overflows without dynamic memory allocations (e.g., [36]), without relying on additional data structures (as in [20]), and without requiring global rescaling operations for all the counters (e.g., [16]).
The SALSA encoding: SALSA starts with all counters having s bits (e.g., s = 8), where s may be significantly smaller than the intended counting range (e.g., N = 2 32 ). Here, we describe an encoding that requires one bit of overhead per counter (e.g., 12.5% for s = 8 bit counters); we later explain how to reduce it to less than 0.6 bits (7.5% for s = 8).
Each counter i is associated with a merge bit m i . Once a counter needs to represent a value of 2 s , we say that the counter overflows. In principle, an overflowing counter can merge with its left-neighbor or right-neighbor. In SALSA, we select the merge direction to maximize byte and word alignment, which improves performance. We also make counters grow in powers of two (e.g., from s bits to 2s, then to 4s, etc.). In Section IV, we explore a slower but more fine-grained approach. Specifically, when an s-bit counter i overflows, it merges with i+(1−2·(i mod 2)). For example, if counter 6 overflows, it merges with 7, while if counter 7 overflows, it merges with 6. More generally, when an s · 2bit counter with indices i · 2 , i · 2 + 1 . . . (i + 1) · 2 − 1 overflows, it merges with the counter-set at indices j · 2 , j · 2 + 1 . . . (j + 1) · 2 − 1 , for j = (1 − 2 · (i mod 2)). As an example, if we started from s = 8 bit counters and counter 6 overflows, it right-merges with 7 to create a 16 bit counter with indices 6, 7 . If this counter overflows, it left-merges into a 32 bit counter with indices 4, 5, 6, 7 , and if this overflows, it left-merges into a 64 bit counter with indices 0, . . . , 7 .
Reducing the Encoding Overhead: The encoding we used in SALSA so far is efficient as well as amenable for simple implementation. The cost of this encoding is a single merge bit per counter. This is, in fact, within a factor of 2 of the optimal encoding, as we show in Appendix A. That is, we prove that any encoding for SALSA must use at least log 2 1.5 ≈ 0.585 overhead bits per counter and show a somewhat more complex O(1)-time encoding with at most 0.594 overhead bits per counter. For a given memory allocation, this encoding provides improved accuracy as the lower overhead allows fitting more counters, but may be somewhat slower.
Fine-grained Counter Merges: The SALSA encoding we presented in Section IV doubles the counter size upon an overflow, which may be wasteful when the overflowing counter could benefit from a smaller increase in size. Thus, we suggest the more refined Tango algorithms to explore the benefits of a more fine-grained merging strategy. In Tango, counters can be merged into sizes that are arbitrary multiples of s. For example, if we start from s = 8 bit counters, Tango can merge a 16 bit counter into a 24 bit counter while SALSA would merge from 16 bits to 32. The encoding of Tango is simple: each counter j is associated with a merge bit m j that denotes whether the counter is merged with its right-neighbor. To compute the counter size and offset in Tango of j = h(x), we scan the number of set bits to the left and right of m j until we hit a zero at both sides. For example, if j = 5 and m 4 = m 5 = m 6 = m 7 = 1 while m 3 = m 8 = 0 then the counter consists of s · 5 bits, spanning 4, 5, 6, 7, 8 . In general, one can use complex logic to decide whether to merge with the left or right neighbor once a counter overflows. However, we design Tango to evaluate the potential benefits of fine-grained merging and therefore enforce a merging logic that mimics SALSA. Specifically, Tango always tries to be aligned to the smallest possible power of two. For example, if counter 9 overflows, it merges with 8 to be aligned with the 2-block 8, 9 . If it overflows again, it merges with 10 (creating a s · 3 bits sized counter) and then with 11. If more bits are needed it will merge with 12 then with 13, 14 and 15 (being aligned to the 8-block 8, . . . , 15 ). Then it merges with 7, 6, . . ., etc. Notice that at every point in time, the Tango counters are contained in the corresponding SALSA counters.
In particular, this allows us to produce an estimate that is at least as accurate as SALSA. We note that Tango poses a tradeoff -while it allows more accurate sketches (e.g., as a counter may not exceed 2 24 − 1 and thus it could be wasteful to merge it into 32 bits), it also has slower decoding time and cannot use the efficient encoding of the previous section.

V. SALSA-FYING SKETCHES
We now describe how SALSA integrates with existing sketches, and specifically how to set the value of merged counters in each sketch. We also state and prove accuracy guarantees for the resulting SALSA sketches. We employ hash functions h i : U → [w] similarly to the original sketches. Given a merged counter with indices L, L + 1, . . . , R , we consider all elements x with L ≤ h i (x) ≤ R to be mapped into it. Hereafter, we often refer to the underlying sketch as following: If the largest merged counter size is s · 2 , the underlying sketch is a vanilla (fixed counter size) sketch where each counter is of size s · 2 and its hashes are . Count Min Sketch (CMS): SALSA CMS and Tango CMS are identical to CMS as long as no counter overflows. We have already defined the merge operation with regard to encoding (Section IV), and with regard to hash mapping in the previous section. However, we still need to define how we determine the value of a merged counter, which provides a degree of freedom we leverage to increase measurement accuracy according to the specific model requirements. A natural merging operation is to sum the merged counter values (illustrated in Figure 2a). We formalize the correctness of this approach for the Strict Turnstile model via the following theorem.
Theorem V.1. Assume that SALSA and Tango use sum merge to unify counters. Let 2 · s be the maximal bit-size of any counter in SALSA CMS, and ∀i ∈ [d] let h i (x) = h i (x)/2 be hash functions that map items into a standard CMS with (2 · s)-sized counters. Then for any are the estimates of Tango, SALSA, and the underlying CMS (with functions h i (x)).
Proof. The sum merge maintains an invariant where the value of each merged counter is the total frequency of all elements mapped to it. In the worst case, a merge results in a counter of size equal to that of the corresponding counter in the underlying CMS. In this case, the values of the counters are identical. Otherwise, the value of a Tango counter is upper bounded by a SALSA counter which, in turn, is upper bounded by the corresponding value in the underlying CMS.
For Cash Register streams (with only positive updates), rather than sum the counters when merging, we can take the maximum value of the merged counters to gain more accuracy (exemplified in Figure 2b) while maintaining guarantees, as formalized in the following theorem.
Theorem V.2. Assume that SALSA and Tango use max merge to unify counters. Let 2 · s be the maximal bit-size of any counter in SALSA CMS, and ∀i ∈ are the estimates of Tango, SALSA, and the underlying CMS (with functions h i (x)).
Proof. After each merge, the counter value upper bounds the frequency of any element mapped to the hash range of the merged counter. In addition, the value of SALSA and Tango counters when using the max merge are upper bounded by the corresponding value of SALSA and Tango counters when using the sum merge.
Theorems V.1 and V.2 show that SALSA CMS and Tango CMS are at least as accurate as the underlying CMS for both merge operations. Intuitively, by sum-merging every consecutive n bits, we obtain estimates that are identical to a CMS sketch that uses n bit counters. Therefore, sum-merging SALSA's estimates are upper bounded by the CMS estimates. In Cash Register streams, max-merging estimates are upper bounded by the sum-merging ones. Finally, for any given element, the estimates of SALSA CMS and Tango CMS are lower bounded by its true frequency, which implies that our approach provides the same error guarantee as the underlying sketch.
SALSA CMS also improves the performance of count distinct queries for Linear Counting [37] using CMS. Recall Linear Counting estimates the number of distinct queries using the fraction p of zero counters. We consider running Linear Counting using SALSA CMS staring with s = 8 bit counters, compared to a standard CMS implementation using 32-bit counters. Unlike standard CMS, SALSA may be unable to determine the exact number of (s-bit) counters that remain zero, as some are merged into other counters. Instead, we compute the fraction f of s-bit counters that remained zero from the overall number of counters that did not merge. For every counter that is the result of one or more merges, we know that at least one of its sub-counters is not zero; we optimistically assume that a fraction f of its remaining subcounters are zero. So, for example, our estimate of the number of counters that are 0 is the number of s-bit counters that remained zero, plus f times the number of 2s-bit counters, plus 3f times the number of 4s-bit counters, and so on if there are larger counters. Note that this approach is heuristic and its accuracy guarantees are left as future work.
Conservative Update Sketch (CUS): SALSA CUS is similar to the standard CUS -whenever an update x, v arrives, being the previous frequency estimate for x. Unlike the CMS variant, the correctness of SALSA CUS is not immediate as not all counters are increased for each packet. Theorem V.3 shows that SALSA CUS is correct in the Cash Register model when working with the max-merge method.
are the estimates of SALSA and the underlying CUS (with functions h i (x)).
Proof. It is sufficient to consider only updates with v = 1 since each x, v update is identical to v consecutive x, 1 updates. The proof is by induction on the number of updates. Specifically, we show that after each update it holds that where we denote by C SALSA and C CU S the counters of SALSA and the underlying CUS, respectively.
As a base case, initially We show that if Equation (2) holds, it continues to hold after an additional update.
Case 1: In this case, on update x, 1 , C CU S [i,h i (x)] is increased by CUS. Therefore the claim trivially holds if there is no overflow in SALSA. If there is an overflow, the claim holds by the virtue of the max-merge. That is, the value of the merged counter grows by exactly 1. This also means that the inequality holds for all counters involved in this merge since they are all upper bounded by In this case, on update x, 1 , the claim trivially holds if there is no overflow in SALSA. If there is an overflow, by the virtue of the max-merge, the value of the merged counter still only grows by 1. This also means that the inequality holds for all counters involved in this merge since they are all upper bounded by prior to the update, and therefore are upper bounded by Count Sketch (CS): SALSA can also extend the CS, with a minor modification. Unlike most existing implementations, which use the standard Two's Complement encoding, SALSA CS uses a sign-magnitude representation of counters (as counters can be negative), with the most significant bit for the sign and the rest as magnitude. While Two's Complement represents values in the range −2 s−1 , . . . , 2 s−1 − 1 , signmagnitude does not allow a representation of −2 s−1 . However, our use of sign-magnitude is critical for us to ensure that the overflow event is sign-symmetric, which allow us to prove that our sketch is unbiased. When an s · 2 bits counter exceeds an absolute value of 2 s·2 −1 − 1, we merge to double its size. When merging counters in SALSA CS, we use sum-merge; note max-merge may not be correct as counters may have opposite signs. We prove the correctness of SALSA CS. For simplicity, we focus on the main variant. That is, a counter merges at most twice, starting from s = 8 bits and assuming that no counter reaches an absolute value of 2 31 , which is the common implementation assumption. Let x ∈ U be an element mapped to counter A, which may be merged with counter B to create A, B , which in turn may later merge with C, D to make the 4s-bit counter A, B, C, D . This setting is illustrated to the right: We wish to show that the estimates of each row in SALSA CS are unbiased, and further that each estimate has variance with SALSA CS that is no larger than the corresponding variance with CS. As mentioned, here give a full analysis for starting with s bit counters and allowing counters to grow to 4s bits, as this is the focus in our implementation, but the approach generalizes readily to additional levels. we now introduce some notation to analyze SALSA CS.
We use O AB to denote the event that A and B have been merged at query time (either into A, B, C, D or just as A, B ) and O ABCD for the event that A, B, C, and D have been merged into A, B, C, D . We also denote the value of A by (the random variable) X A , the value of A, B by X AB , and similarly for X B , X CD , and X ABCD . We emphasize that X S represents the value of the count mapped to S, regardless of whether the counter overflows (e.g., X A y∈U :h(y)=A f y g(y) even if X A ≥ 2 s ). Without loss of generality, we also assume that the sign of x is g(x) = 1 (and thus E[ . This allows us to express the estimate given in a row for SALSA CS as: We continue by proving that the estimate is unbiased.
Proof. Due to the sign-symmetry of the sign function g, We continue by simplifying the expression for E f x Since x is mapped to A, we can use the sign-symmetry of X B and X CD to , where the inequality follows from (5). Together with (4), this concludes the proof.
Because the theorem shows the error variance is no larger for each row, following the same analysis as for CS (using Chebyshev's inequality to bound the error of the row and then Chernoff's inequality to bound the error of the median) yields the same error bounds for SALSA CS. Indeed, we expect better estimates using SALSA as the inequality from the proof of the theorem (Var[CS] ≥ Var[ f x ]) is usually a strict inequality. In our experimental evaluation, we show that SALSA CS obtains better estimates than CS.
Theorem V.6. Let 2 · s be the maximal bit-size of any counter in sum-merge SALSA CS, and ∀i ∈ be hash functions that map items into a standard CS with (2 · s)-sized counters. Then for any and C CS [i,h i (x)] are the counters of SALSA and the underlying CS (with functions h i (x)).
We note that SALSA CS can also provide other derived results, similarly to CS. For example, by using a heap, we can find the L 2 -heavy hitters in Cash Register streams similarly to the original version.
Universal Sketch (UnivMon): The universal monitoring sketch (UnivMon) uses several L 2 sketches that are applied on different subsets of the universe. By improving the accuracy of CS, we can also improve the performance of UnivMon. We note that since SALSA CS provides an accuracy guarantee that is at least as good as the underlying sketch, SALSA Univmon provides the same accuracy guarantee as the vanilla Univmon.
Merging and Subtracting SALSA Sketches: Given streams A, B and their sketches s(A), s(B), we may then wish to derive statistics on A ∪ B (for example, we can parallelize the sketching of A and B and then merge them), or on A \ B (for example, to detect changes in our network traffic compared to the previous epoch). By A \ B, we refer to computing the frequency difference; e.g., if x appeared twice in A and three times in B, its frequency in A \ B is SALSA can also merge and subtract sketches. For merging s(A) and s(B), SALSA traverses the counters and merges them according sum-merging. Specifically, each counter in the merged sketches has a size at least as large as its size in s(A) and its size in s(B). Additionally, when summing or subtracting counters an overflow may occur, triggering another merge to make sure we have enough bits to encode the resulting values. CS, as a Turnstile sketch, also supports general subtracting that is done similarly to merging, while  Figure 3.
Integrating Estimators into SALSA: Thus far, we have described a single strategy to handle overflows: when a counter reaches a value that can not be represented with the current number of bits, it merges with a neighbor. However, there are alternatives that allow one to increase the counting range. Specifically, estimators can represent large numbers using a smaller number of bits, at the cost of introducing an error.
The state of the art Additive Error Estimators (AEE) [16] offer a simple and efficient technique to increase the counting range. For simplicity, we describe the technique for CMS and unit-weight streams (where all updates are of the form x, 1 ), although AEE can support weighted updates and other L1 sketches as well. Throughout the execution, incoming updates are sampled with probability p. If an update is sampled, it increases the sketch, and otherwise, it is ignored. Whenever a counter overflows, a downsampling event happens. When downsampling, p is halved and any counter C[i, j] is replaced by either Bin(C[i, j], 1/2) (called probabilistic downsampling) or by C[i, j]/2 (deterministic downsampling). Since the counter values are reduced as a result of the downsampling, new updates can be processed, and no additional counter bits are needed. For any δ est > 0, we have an implied estimation error for AEE given by est Another motivation for AEE comes from the processing speed. Since the sampling probability is independent of the value of the current counter, one can compute the hash functions h i (x) only if a packet is sampled. Since hash functions are a major bottleneck for sketches [18], AEE is faster than the baseline sketches. Another version of the estimator, called AEE MaxSpeed, aims to maximize the processing speed while bounding the error. Therefore, instead of waiting for a counter to overflow, it downsamples all counters once enough updates have been processed. In comparison with the original variant (called AEE MaxAccuracy), MaxSpeed is faster but less accurate [16].
Intuitively, downsampling and merging increase the error in different ways. While downsampling increases the inherent error of a counter, merging adds noise from other elements that previously have not collided with the counter. SALSA selects how to handle overflows in a way that minimizes the theoretical error increase by either downsampling or merging. Specifically, as our accuracy theorems suggest, the sketch error in SALSA depends on the size of the largest counter. Therefore, unless a largest counter overflows, SALSA opts for merging as its overflow strategy. When a largest counter overflows, SALSA computes the estimator error difference ∆ est = √ 2 · est , which is the increase in error if we downsample. Similarly, if the currently largest counter is of size s·2 , SALSA computes CM S δ −1/d ·2 /w, which is the current accuracy guarantee (see Theorem V.1 and Section III) and ∆ CMS = CM S is then the difference in error guarantee that results from merging. We pick δ est = δ/d to allow all counters of the current element to be estimated within est with probability 1 − δ. Finally, SALSA chooses to merge if ∆ CMS ≤ ∆ est , and otherwise it downsamples. As a result, SALSA estimates element sizes to within N · ( est + CM S ) with a probability of at least 1 − 2δ.
As an optimization, when downsampling, SALSA may be able to split counters if the resulting values can be represented using fewer bits. For example, if s = 8 and a value of 300 was represented in the 16-bit counter 4, 5 , and if it is then downsampled to a value of 150, we can split the counter and set both counter 4 and counter 5 to 150. We note that this only works for max-merging, where the accuracy guarantees seamlessly follow.

VI. EVALUATION
In this section, we extensively evaluate SALSA's performance on real and synthetic datasets and compare it to that of the underlying sketches. We first document the methodology.
Sketch Configuration Parameters: Unless specified otherwise, all CMS and CUS sketches are configured with d = 4 rows, as is used e.g., in the Caffeine caching library [38]. Since CS requires taking median over the rows, all CS experiments are configured with d = 5 rows as done, e.g., in [39]. We configure UnivMon with 16 CS instances, each configured with d = 5 and a heap of size 100, following the implementation of [15]. Such settings are standard for applications that aim for speed rather than being memoryoptimal. For the ABC [17] and Pyramid [12] sketches, as well as the Cold Filter [9] framework, we use the configurations recommended by the authors. We pick s = 8 bit counters as the default configuration of SALSA, motivated by the synthetic results. We use the simple encoding (1 bit of overhead per counter) of Section IV, which uses slightly more space but is faster. The Baseline implementations use 32-bit counters, a choice we justify later in Figure 6, and that is also common in existing implementations [18], [33]. Nonetheless, our SALSA implementation allows counters to grow further, up to 64 bits. For implementation efficiency, all row widths w are powers of two. When we give figures where an x-axis is allocated memory, we include the encoding overheads. For the integration with AEE, we configure SALSA AEE with δ = 4 · δ est = 0.001 (see Section V).

Datasets:
We evaluate our algorithms using four real datasets and several synthetic ones. In particular, we use three network packet traces: two from major backbone routers in the US, denoted NY18 [40] and CH16 [41], and a data center network trace denoted Univ2 [42]. In these traces, we define items using the "5-tuples" of the packets (srcip, dstip, srcport, dstport, proto). Additionally, we use a YouTube video trace [43,US category]. As the video data does not have a recorded order (just view-count), we use a random order where each item is a video independently sampled according to the view-count distribution. Finally, we use random order Zipfian traces. All traces have 98M elements for consistency with the shortest real dataset. In our evaluation, we use unit-weight Cash Register streams (i.e., all updates are of the form x, 1 ). We also experiment with the task of evaluating change detection, which requires a SALSA sketch under the Turnstile model.
Metrics: For frequency estimates, we use the On-arrival model that asks for an estimate of the size of each arriving element (e.g., [5], [7], [16], [44]). Intuitively, this model is motivated by the need to take per-packet actions in networking, e.g., to restrict the allowed bandwidth to prevent denial of service attacks. Given a stream with n updates, we obtain errors e 1 , e 2 , . . . , e n ; the Mean Square Error is defined as M SE n −1 · i e 2 i , the Root Mean Square Error is then RM SE √ M SE, while the Normalized RMSE is NRMSE n −1 · RM SE. Similar metrics are used, e.g., in [5], [7], [16], [44]. Notice that NRMSE is a unitless quantity in the interval [0, 1]. For fairness, we also evaluate using the error metrics used in Pyramid and ABC: Average Absolute Error (AAE) and Average Relative Error (ARE). AAE averages the error over all the elements with non-zero frequency i.e., AAE Similarly, ARE is defined as 1 . For tasks such as Count Distinct, Entropy, and Frequency Moments estimation, we use the Average Relative Error (ARE) metric that averages over the relative error of the ten runs. 2 For turnstile evaluation, we evaluate the capability of SALSA to improve sketches for the Change Detection task (e.g., see [15], [45]) in which we partition the workload into two equal-length parts A and B, sketch each, and test the NRMSE of the estimates of the frequency changes between A and B. Each data point is the result of ten trials; we report the mean and 95% confidence intervals according to Student's t-test [46]. Implementation: We leverage existing CMS and CUS implementations from [16] and extend them to implement SALSA. We also extend these to create a Baseline and SALSA implementation of CS. We also used the authors' code for the Pyramid [12], ABC [17], and Cold Filter [9] algorithms. Particularly, for error measurements, we used the code as-is, while for speed measurements, we applied our optimizations for a fair comparison. All sketches use the same hash functions (BobHash) and index computation methods.  When evaluating against the AEE estimators [16], we use the provided open-source code. Similarly, we obtained the UnivMon code from [39] and replaced its CS sketches with SALSA CS to create 'SALSA UnivMon'. All speed measurements were performed using a single core on a PC with an Intel Core i7-7700 CPU @3.60GHz (256KB L1 cache, 1MB L2 cache, and 8MB L3 cache) and 32GB DDR3 2133MHz RAM.
How to Configure SALSA? We perform preliminary experiments to determine the default SALSA configuration.
How Large Should Counters Be? We first determine the most effective minimal counter size (s) for SALSA. Intuitively, for a fixed row width w, smaller s results in lower error but also in larger encoding overheads. With this tradeoff, it may not be profitable to reduce s. In this experiment, we fixed the memory of the counters and deliberately ignored the encoding overheads for SALSA. The goal is to quantify the attainable improvement from using smaller counters. We used synthetic Zipfian trace with skews varying from 0.6 to 1.4.
As shown in Figure 4, most of the improvement comes from reducing the counter sizes from 32 bits to 8 bits. These results were consistent across different memory footprints. This is not surprising as almost all counters merge up to at least 8 bits, but then many do not overflow further. We also observe that SALSA offers more gains for low-skew traces and that CS is  better suited for lower skews, while CMS offers comparable accuracy with less space for high skew. Hereafter, we use s = 8 bits as the default SALSA configuration. While SALSA with s = 4 bits is slightly more accurate for low-skew workloads and high memory footprints, its encoding overhead of about 25% of the sketch size (compared to 12.5% for s = 8) is too large to justify the benefits. Which Merging Should We Use? As mentioned above, SALSA CS must use sum-merging, and so does SALSA CMS for Strict Turnstile streams. Similarly, in SALSA CUS, we need to use max-merging. This leaves only the choice for SALSA CMS in Cash Register streams, where we can use either sum-merging or max-merging. We quantify the difference in accuracy in Figure 5. As shown, max-merging is slightly more accurate, especially for low-skew workloads. We conclude that if one only targets Cash Register streams, it is better to use max-merging, but the accuracy of sum-merging is not far behind.
Is Fine-grained Merging Worth It? To understand the accuracy improvement attainable by fine-grained merging (as opposed to SALSA's approach of doubling the counter size at each overflow), we compare SALSA with Tango. As the results in Figure 7 indicate, Tango also offers the best accuracyspace tradeoff when starting with s = 8 bits (Tango 16 is equivalent to SALSA 16 and is omitted). However, while it is slightly more accurate, the gains seem marginal considering the computationally expensive operations of determining the counter's size and offset. Further, Tango has an overhead of 1 bit per counter and does not obviously allow an efficient    encoding like SALSA does (Section IV).
Can one simply use small counters? In our evaluation, SALSA starts from s = 8 bit counters. We now compare SALSA with a baseline sketch that uses 8-bit or 16-bit counters (as proposed, e.g., in [47]). In such a sketch, the counter is only incremented if it does not overflow (i.e., its value is bounded by 2 b − 1 for b-bit counters). We show that such solutions cannot capture the sizes of the heavy hitters -elements whose frequency is at least a φ fraction -which are often considered the most important elements [48]. First, we show (Figure 6a) that when estimating heavy hitters, even with a loose definition of φ = 10 −4 , it is best to use 32-bit counters for CMS. Similarly, as shown in Figure 6b, when the measurement is longer than 10M elements, the 16-bit variant becomes less accurate. Figure 6 is depicted for Zipfian trace with skew=1, and we observed similar behavior for other traces, thresholds, and memory footprints.
Comparison with Pyramid Sketch and ABC: We used the authors' original implementations for both Pyramid Sketch and ABC. We present results for CMS on the NY18 and CH16 datasets; similar results are obtained for additional sketches and workloads.
As shown in Figure 8a, and Figure 8b, Pyramid Sketch and SALSA are about 20% slower than the baseline, while ABC is about 75% slower. Intuitively, the slowdown is expected, as all these algorithms bring additional complexity to the baseline. ABC is significantly slower due is additional encoding overheads as its bit-borrowing technique does not allow bytealignment for counters, forcing it to make additional bitwise operations for reading and updating counters.
In terms of the NRMSE metric (8c and 8d), SALSA achieves the best results. Next is the baseline following by Pyramid Sketch, and ABC. The on-arrival NRMSE metric gives more weight to the frequent elements, and is more sensitive to larger errors than AAE and ARE. Our results indicate that SALSA is also more accurate than Pyramid Sketch and the baseline in terms of AAE and ARE for the entire memory range. Note that Pyramid Sketch is better than the baseline in the memory range 0.5MB-2MB, which is the range it is optimized for according to the paper. ABC is slightly more accurate than SALSA for small memory sizes but less accurate than SALSA for large memory sizes, and is comparable in between. Our conclusion is that SALSA is the best in the NRMSE metric and is competitive in the AEE, and ARE, and speed metrics.
Understanding the differences: Our first observation, is that the AAE and ARE metrics are not suitable when estimating the size of the heavy hitters, which are often considered the most important elements [48]. This is because both metrics give equal weight to all items, making impact of the error of the largest ones vanish due to averaging. This is evident from Figure 6, in which the leftmost point (φ = 10 −8 ) corresponds to the AAE and ARE metrics (as all items are accounted for). As shown, in such a case, using 8-bit counters yields lower error rates. Nevertheless, such a solution cannot count beyond the value of 255, which results in excessive error for the heavy hitters (e.g., φ = 10 −3 ). In fact, as we show in Appendix B, for CMS, and this dataset, it is better to estimate all sizes as 0 without performing any measurement. To illustrate the differences that make Pyramid Sketch and ABC competitive in the AAE/ARE metrics, but not in NRMSE, we visualize the errors of estimating individual element frequencies. We       sampled one random element from each possible frequency to reduce clutter. The results, showing in Figure 9, demonstrate the differences between the algorithms. SALSA has a low error-variance and is consistently more accurate than the Baseline. In contrast, Pyramid Sketch (as shown in region A) has much higher variance, as elements whose counters overflow share the most significant bits with other elements. ABC, as evident in region B, has a high error on heavy hitters as its counters can at most double in size by combining with their neighbors. We configured ABC to start with 8-bit counters as suggested by the authors, limiting its estimation to at most 2 13 − 1 (as three bits are spent on overhead). While one could use larger counters, it would decrease their number and diminish the benefit over the baseline sketch. To conclude, both ABC and Pyramid Sketch have elements with high estimations errors, making them less attractive for (Mean Square Error)-like metrics.
L1 Sketches: We proceed by testing the impact that SALSA (with s = 8 bit counters) has on the accuracy and speed of L1 sketches, such as CMS and CUS. The results, depicted in Figure 10, show that SALSA CMS is substantially more accurate (roughly requiring half the space for the same error) than the Baseline for the NY18, CH16, and YouTube datasets. For Univ2, SALSA's improvement is less noticeable, and due to its encoding overheads, the tradeoff is not statistically significant. SALSA CUS is better than the Baseline on all traces, and often requires half the space for a given error. SALSA's accuracy comes at the cost of additional operations that are required to maintain the counter layout. We measured SALSA to be 17%-23% slower than the corresponding Baseline variants, but can nonetheless handle 10-17.5 million elements per second on a single core, which is sufficient to support the high link rate forwarding at modern large-scale clusters, such as Google, which is estimated at 9M packets per second (see [49,Sec. 3.2]) . We note that by combining SALSA with estimators (Section VI), we can make faster counter sketches. We conclude that SALSA offers an appealing accuracy to space tradeoff.
Count Sketch: Next, we evaluate SALSA for Count Sketch, whose L2 guarantee is important for low-skew workloads and more complex algorithms such as UnivMon. As shown in Figure 11, SALSA offers statistically significant improvement for the NY18, CH16, and YouTube datasets. For Univ2, the accuracy improvement is offset by the encoding overhead, and it is not clear which variant is better.
UnivMon: We use SALSA CS to extend the Universal Monitoring (UnivMon) sketch that supports estimating a wide variety of functions of the frequency vector. Our experiment includes estimating the element size entropy and F p moments, for 0 ≤ p ≤ 2. The result in Figure 12 indicates that SALSA improves the accuracy of both tasks. Interestingly, for entropy estimation, we observe that SALSA's accuracy (and variance) improve when using smaller (s = 2 or s = 4 bit) counters. When using a large amount of memory, SALSA has roughly the same accuracy as the baseline, as both hit a bottleneck  in the size of the sketches' heaps (set to 100 elements, as in the implementation of [18]). For estimating F p moments, we measure similar accuracy for small values of p while SALSA improves the accuracy for large p values. To explain this, notice that the element size estimates mainly affect the F p for large p, while for p ≈ 0, the value is determined primarily by the cardinality.
Cold Filter: We extend Cold Filter by replacing the second-stage CUS (denoted CM-CU in the original paper) algorithm with our SALSA variant. The results in Figure 13 use the AAE and ARE metrics suggested by its authors [9]. The results show that SALSA saves up to 50% of the space for a similar error. However, the improvement is more evident when the allocated memory is small, as in these cases the second stage algorithm plays a significant role. When the memory size is large (compared to the measurement length), the firststage algorithm handles most of the flows, and improving the second-stage CUS algorithm yields marginal benefits. We observed negligible differences in the processing speed, which is expected as many elements only touch the first stage and do not reach the second. We also tested Cold Filter versus its SALSA variant using the NRMSE metric; there, SALSA yields even larger accuracy gains. However, Cold Filter's aggregation buffer needs to be drained upon query, which negates its speedup potential in the on-arrival model.
Count Distinct and Heavy Hitters using Count Min: We evaluate the performance of SALSA CMS on additional applications such as counting distinct elements and estimating the size of the heavy hitters. As shown in the count, distinct results (Figures 14(a)-(c)), neither SALSA CMS nor the Baseline are effective with low memory footprints. This is because no counters remain zero-valued, and the Linear Counting estimator fails. Nevertheless, SALSA CMS can work with less memory (4.5MB for NY18 and 1.125MB for CH16) and reduce the estimation error when the Baseline does produce estimates. Intuitively, Linear Counting with w  buckets can count up to w ln w elements, so the number of elements in the datasets (6.5M for NY18 and 2.5M for CH16) imposes a lower bound on the amount of space needed. We evaluate the accuracy for estimating the heavy hitters (elements with frequency of at least φ · N ) frequencies, while varying φ between 10 −4 and 10 −2 as in [48]. SALSA CMS is more accurate, especially for small values of φ. The lower improvement for large φ values can be explained as φ·N ≥ 2 16 for φ ≥ 2 16 98·10 6 ≈ 7 · 10 −4 , which means that all such heavy hitters cause their counters to merge to 32 bits (the same as the Baseline). The plot of Figure 14d stops around φ ≈ 3.16 · 10 −4 as no element in the NY18 dataset has frequency larger than 5.62 · 10 −4 · N ≈ 551K packets.
Top-k and Change Detection using Count Sketch: We also examine SALSA's effect on other uses of CS such as Topk and Change Detection (which requires Turnstile support). For Top-k, our experiments indicate that using sufficient mem-  ory (e.g., 2MB), the Baseline CS detects the largest elements accurately for reasonable k values. Therefore, we focus on a constrained memory setting (640KB). As shown in Figure 15 (a) and (b), SALSA detects the top-k accurately, especially for large values of k and low-skew workloads.
We also evaluate SALSA CS on a Change Detection task. Here, we split the input into two equal-length intervals A and B, the algorithm needs to estimate the change in the frequency of an element x between the first and second halves. To that end, we create sketches s(A) and s(B) and the difference sketch s(A \ B) as described in Section V. Intuitively, the frequency difference can be small compared with the frequencies of each interval, and thus directly subtracting the estimates of s(A) and s(B) could yield a poor result compared to taking the difference sketch (as the desired error is a fraction of the L 2 norm of the frequency difference). In Figure 15 (c) and (d), we compute the NRMSE error 3 over the set of elements that appear in either A or B. As shown, SALSA provides a statistically significant accuracy improvement in all tested memory allocations and dataset skews.
Estimators: We now experiment with integrating estimators, specifically AEE [16], into SALSA CMS. Under certain conditions, AEE increases the accuracy and processing speed of the sketch. Intuitively, the accuracy can be increased as the sketch can use more estimators than it would use counters, and the speed is increased because some packets are ignored without updating the estimators. Our estimator-integrated solution SALSA AEE (from Section V) optimizes the accuracy by interleaving estimator downsampling and estimator merges. Roughly speaking, SALSA AEE aims to be at least as accurate as the best of SALSA CMS and AEE MaxAccuracy by choosing the best method to cope with each overflow. Similarly to AEE MaxSpeed, we create a speed-optimized variant called SALSA AEE d that downsamples on the first d overflows (and selects whether to merge or downsamples afterward according to the logic presented in Section V). This allows the algorithm to reach a sampling rate of 2 −d and thus obtain speedups by reducing hash computations. The results, shown in Figure 16, illustrate that SALSA AEE is always as accurate as SALSA (when SALSA only merges counters), and more accurate for small amounts of memory. For large amounts of memory, SALSA AEE only merges, and therefore its accuracy is identical to SALSA while it is slightly slower due to the added logic. Compared with AEE MaxAccuracy, SALSA AEE has comparable accuracy for small memory allocations (where it is mostly better to downsample than merge). Further, for large memory allocations (e.g., 100KB or higher), SALSA AEE is more accurate than AEE MaxAccuracy, as in such scenarios it is better to merge than to downsample. Compared with AEE MaxSpeed, SALSA AEE 10 provides improved accuracy (by up to 25%), especially for small amounts of memory, while also being faster (by up to 7%), except when using large space (2MB+ in this experiment).
Should We Split Counters? Finally, we check the accuracy gains obtainable by splitting counters. Intuitively, once a counter is downsampled, it may require fewer bits to represent. Therefore, if previously the counter had s · 2 bits and the downsampled value is lower than 2 s·2 −1 − 1 (and ≥ 1), we can split the counter into two s · 2 −1 -bit counters. As a result, there are now fewer collisions between elements, and SALSA AEE has better accuracy. However, as the results in Figure 17 suggest, this effect is minor, and in most cases, the accuracy gains are insignificant.

VII. CONCLUSIONS
We have presented SALSA, an efficient framework for dynamically re-sizing counters in sketching algorithms, extending counters only when needed to represent larger numbers. SALSA starts from small counters and gradually adapts its memory layout to optimize the space-accuracy tradeoff. By evaluating across multiple real-world traces, sketches, and tasks, we have shown that, for a small overhead for its merging logic, SALSA reduces considerably the measurement error. In particular, our evaluation indicates that SALSA improves on state-of-the-art solutions such as Pyramid Sketch [12], ABC [17], Cold Filter [9], and the AEE estimators [16].
We believe that SALSA can replace and enhance existing sketches in more complex algorithms, such as L psamplers [50] and database systems (e.g., [51], [52]). All of our code is released as open source [1].

APPENDIX A IMPROVED ENCODING
Here, we lower bound the space required to encode SALSA, and then suggest a near-optimal encoding that has O(1) time operations.
Lower bound. For n ∈ N, we define by a n the number of possible layouts for a consecutive block of 2 n · s bits (i.e., a block that started from 2 n counters of size s-bits each). Observe that either all 2 n counters are merged together, or it is enough to specify the layouts of the first 2 n−1 counters and last 2 n−1 counters. Therefore, we get the recursive relation a n = a 2 n−1 + 1 and 1 Fig. 18: An encoding example for m = 5. This layout is encoded by X5 = 449527 < a5. To compute the size of counter 9, we first check that X5 < a5 − 1 and thus not all counters are merged. Next, we have that X4 = X5/a4 = 663 < a4 − 1 and thus counters 0-15 are not all merged. Then, we check that X 3 = X4 mod a3 = 13 < a3 − 1 which means that counters 8-15 are not all merged. We continue with X2 = X 3 /a2 = 2 < a2 − 1 (thus 8-11 are not merged) and finally get X1 = X2/a1 = 1 = a1 − 1 which implies that 9 is merged with 8.
a 0 = 1. Given that we start from w counters, this implies that the number of possible layouts is lower bounded by a log 2 w .
Proof. The inequality is easy to verify for n ≤ 3. By induction, one can then easily prove that ∀n ≥ 3 : 1.5 2 n + 1 < a n < 1.51 2 n − 1.
This suggests a lower bound of log 2 a log 2 w ≤ 2 log 2 w log 2 1.5 bits. Specifically, for w ≥ 2 4 values which are powers of 2 any encoding must use at least log 2 1.5 ≈ 0.585 bits per counter.
Near-optimal encoding. Denote by m the maximal number of merges a single counter may go through during the execution. We note that m = O(1) as we assumed the final counters must fit into O(1) machine words. For example, if we start from s = 2 bits counters and we assume that counters grow up to 128 bits, then m = 6. Let m = max {5, m}. Intuitively, we encode every 2 m counters separately, thereby allowing O(m) = O(1) time size computation. According to Lemma A.1, we have that z m log 2 a m bits are enough to encode the counter-set layout; for example, z 5 = 19 bits are enough to encode the layout of 2 5 = 32 counters. Specifically, for n = m, m − 1, . . . , 0 we write a z n -bits value X n such that X n = a n − 1 means that all 2 n counters are encoded, and otherwise X n−1 X n /a n−1 encodes the layout of the first 2 n−1 counters while X n−1 X n mod a n−1 encodes the layout of the rest (i.e., they are the base-(a n−1 ) digits of X n ). As a result, we use z m bits for each consecutive set of 2 m counters, giving an overhead of z m /2 m . For n ≥ 5, we have that z n /2 n < 0.594, i.e., we require at most 0.594 overhead bits per counter. Computing the size of a counter then becomes simple: we start from n = m and every time check if the value is a n − 1, or recurse into either the left or right half depending on the counter index. An example of this process is illustrated in Figure 18. While this approach reduces the overhead, the decoding process involves division and modulo operations that may reduce the speed.

APPENDIX B UNDERSTANDING THE DIFFERENCES -EXTENDED RESULTS
For completeness, we repeat the experiment in Figure 6 using even smaller (4-bit) counters. The results are shown in  figures 19 and 20. We measured the error on all heavy hitters -elements larger than a φ fraction of the input. The leftmost point (φ = 10 −8 ) of Figure 19 corresponds to the ARE metric (i.e., all flows will be considered). As shown, in this case, the best algorithm is 0, which corresponds to returning 0 estimates for all element sizes. That is, according to this metric, one can reduce the error by not running measurements at all. A similar result was observed for AAE, in Figure 20), where the 0 algorithm outperforms the baseline when considering all flows (leftmost point).