Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Please, make the statement "Monoid is not []" true #880

Open
Anton-Latukha opened this issue Mar 11, 2021 · 5 comments
Open

Please, make the statement "Monoid is not []" true #880

Anton-Latukha opened this issue Mar 11, 2021 · 5 comments
Labels
API breaking refactoring good first issue Suggested for someone who is not yet familiar with the codebase

Comments

@Anton-Latukha
Copy link
Collaborator

Anton-Latukha commented Mar 11, 2021

One of the core stones HNix stumbles on is pretty pervasive String (as exceptions that gather provenance info, and in REPL) and, especially, List being used across the whole project.

The List is famously a one-directionally linked chain structure.

That means that per every cell used there is a cell that holds a pointer.
And there is no way to append to list without traverse of the whole list.

Appending x meaning traverse every time, which is doing x^2 actions, so indeed - exponential append.

The most frequent thing the structure builder does is appending.

So, lets look at what actions happen with the Monoid, and most probably replace the List with an efficient Monoid, for example: https://hackage.haskell.org/package/acc

@Anton-Latukha Anton-Latukha changed the title Please, make statement "Monoid is not List" true Please, make the statement "Monoid is not List" true Mar 11, 2021
@Anton-Latukha
Copy link
Collaborator Author

nikita-volkov/acc#1 (comment)

nikita-volkov:

There is a common pattern to construct data structures in two phases: first build, then freeze. With that in mind it helps to separate data structures into modification-optimized and frozen.

The first category is your builders, maps, sets, lists and what not, all optimized for various kinds of modification operations. Frozen types are optimized for querying operations and compact representation in memory, their examples are monolithic types like vectors, text, bytestring and ADTs around them.

Your NExprF and NString seem to fall into the frozen category. That's why I recommend using Vector instead of list or Acc in it.

Since Vector is not optimized for any sort of modification, you'll need a data structure optimized for building it. Acc paired with Int (for the vector size) would do for such purpose, however I've already released a package dedicated to building vectors and it is optimized even better for the task: "vector-builder".

I sense the way you construct is by parsing. It is a typical case for the build/freeze approach. More so, you'll find utilities like many and some in the "vector-builder" package that are particularly useful in parsing.

@Anton-Latukha Anton-Latukha added the refactoring No API breakages. Work that only makes code to be nicer or allows for future functionality label Jul 8, 2021
@Anton-Latukha
Copy link
Collaborator Author

The codebase got refactored. It maximally uses polymorphic one for singletons & conversion to NonEmpty started. HLint guards for people not to put [s] singletons in code - so part of the work is done & protected.

It is now should be easier to convert [] to chosen types.

@Anton-Latukha
Copy link
Collaborator Author

@Anton-Latukha Anton-Latukha changed the title Please, make the statement "Monoid is not List" true Please, make the statement "Monoid is not []" true Nov 22, 2021
@Anton-Latukha
Copy link
Collaborator Author

Added a DList & Acc benchmarks into: https://github.com/haskell-perf/sequences

Encouraging.

@Anton-Latukha
Copy link
Collaborator Author

Talked with Nikita (the Acc author), and become sure & understood that Acc is really to be used.

Also because of the published benchmarks he updated Acc & it is even faster now, & fromList does additional optimization of the structure, so [] became more costly, but that is amortization to have effective Acc monoid construction phase.

@Anton-Latukha Anton-Latukha added the good first issue Suggested for someone who is not yet familiar with the codebase label Nov 24, 2021
@Anton-Latukha Anton-Latukha added API breaking refactoring and removed refactoring No API breakages. Work that only makes code to be nicer or allows for future functionality labels Jan 18, 2022
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
API breaking refactoring good first issue Suggested for someone who is not yet familiar with the codebase
Projects
None yet
Development

No branches or pull requests

1 participant