acc: Sequence optimized for monoidal construction and folding

[ library, mit, unclassified ] [ Propose Tags ] [ Report a vulnerability ]

Data structure intended for accumulating a sequence of elements for later traversal or folding. Useful for implementing all kinds of builders on top. . The benchmarks show that for the described use-case it is on average 2 times faster than DList and Seq, is on par with list when you always prepend elements and is exponentially faster than list when you append.


[Skip to Readme]

Downloads

Note: This package has metadata revisions in the cabal description newer than included in the tarball. To unpack the package including the revisions, use 'cabal get'.

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees

Candidates

  • No Candidates
Versions [RSS] 0.1, 0.1.0.1, 0.1.0.2, 0.1.1, 0.1.2, 0.1.3, 0.1.3.1, 0.2, 0.2.0.1, 0.2.0.2, 0.2.0.3
Dependencies base (>=4.13 && <5), deepseq (>=1.4 && <2), semigroupoids (>=5.3 && <7) [details]
License MIT
Copyright (c) 2020 Nikita Volkov
Author Nikita Volkov <nikita.y.volkov@mail.ru>
Maintainer Nikita Volkov <nikita.y.volkov@mail.ru>
Revised Revision 1 made by AndreasAbel at 2024-07-06T15:01:28Z
Home page https://github.com/nikita-volkov/acc
Bug tracker https://github.com/nikita-volkov/acc/issues
Source repo head: git clone https://github.com/nikita-volkov/acc.git
Uploaded by NikitaVolkov at 2023-11-29T19:03:16Z
Distributions LTSHaskell:0.2.0.3, NixOS:0.2.0.3, Stackage:0.2.0.3
Reverse Dependencies 3 direct, 4 indirect [details]
Downloads 2021 total (23 in the last 30 days)
Rating 2.0 (votes: 1) [estimated by Bayesian average]
Your Rating
  • λ
  • λ
  • λ
Status Docs available [build log]
Last success reported on 2023-11-29 [all 1 reports]

Readme for acc-0.2.0.3

[back to package description]

Summary

Data structure intended for accumulating a sequence of elements for later traversal or folding. A great basis for implementing many custom monoids, most notably of the Builder pattern.

It shines with its Monoid instance, which relieves the user from caring about from which side to append. This is important because, different data-structures exhibit very different performance depending on that. Most notably linked list. Acc on the other hand is neutral and performs well in all scenarios.

For such purposes it is common to use Seq or DList. The benchmark results below show that Acc is a better fit.

Benchmark results

These benchmarks compare the performance of Acc vs. various other structures as used for aggregation with intent of reduction.

In other words a two-step process of the following structure is measured as a whole:

  1. Construct the measured data-structure using a particular method (cons, snoc, fromList)
  2. Fold the data-structure into a final result (sum, length)

Following are the highlights from the benchmark results grouped by the method of construction of the datastructure.

Consing 1000 elements

acc               12.40 μs
list              18.70 μs
dlist             43.95 μs
sequence          27.54 μs

Snocing 1000 elements

acc               17.02 μs
dlist             38.93 μs
sequence          27.15 μs

No list here because it will blow up the memory.

Construction from a list of 1000 elements

acc               13.27 μs
list              12.97 μs
dlist             27.57 μs
sequence          10.70 μs

Appending chunks of 1000 elements 1000 times from left

acc               4.256 ms
list              553.7 ms
dlist             315.9 ms
sequence          10.05 ms

Appending chunks of 1000 elements 1000 times from right

acc               4.305 ms
list              5.126 s
dlist             360.4 ms
sequence          7.209 ms

For complete results see the dump.

Executed on an AWS c6i.2xlarge instance running Ubuntu.

Conclusions

Given the preconditions of the benchmarks, the following can be concluded:

  • Neither list or DList are suitable as monoidal structures, due to exponential performance degradation on appends from both sides
  • Snocing and even consing Acc is better than all alternatives
  • Acc performs better than Seq on both left- and right-appends (2-3x)
  • Seq gets constructed from list faster than Acc (1.5x)