-
Notifications
You must be signed in to change notification settings - Fork 178
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
Add NonEmpty containers, without imbalancing or massive code duplication #608
Comments
It looks plausible, yes. It'll be a lot of work, though. Do you think you could implement some of the basic operations and get a sense of the performance? |
Glad to hear it! I'll start with something like the first commit of the DMap PR, obsidiansystems/dependent-map@5985aad, to see just the performance impact of just the type change. |
It would also be very nice to get a sense of the change in code size in one or two applications. I won't push too hard for that, but it'd be nice. |
Are pattern synonyms OK (not too new) to use? I couldn't immediately tell. |
We aim for more portability than that. But feel free to use them to get some quick benchmarks! |
Sorry for taking a bit; busy at work lately. Here's a branch with just the type change https://github.com/obsidiansystems/containers/tree/non-empty . Hope I'm not jinxing the optimizations being bullish, but know pattern synonyms so far. |
So what do your benchmarks show so far? |
Sorry for the delays. It turns out that just as I opened this, I was heading into an especially busy period at work, so I haven't had much time for such open source things. Relatedly, I was being a bit daft and didn't realize you wanted me to run the benchmarks (should have read Anyways, I did that now on GHC 8.6.3 with If the type change alone looks like a real and worthwhile performance benefit to you, I'd be happy to make the same change for all the containers, and only then sort out whether/how writing and exposing functions for the non-empty types makes sense. |
@treeowl light ping :) --- I feel like a hypocrite after I taking a bit to respond my self, but have you had a chance to glance at those benchmarks? Just waiting for the signal to go do the same transformation on the other types. |
I'm generally on board with this approach. |
Sweet! I'll convert all the other types and make a PR. |
You probably shouldn't bother with sequences, though. If it's possible to work a trick like that with sequences, it will be much more trouble than it's worth. It would make much more sense to fake non-empty sequences using smart constructors and such. |
Right, let me roll back "all the other types" to just sticking to the family of |
Er... Are you sure about those |
Oh oops completely forget that the |
When do you expect to submit a proposal for non-empty |
Sorry I've also been hacking on GHC's build system a bit, spreading myself too thin again. It is indeed not too hard, and I can take a bunch of text from the original issue I opened. |
Ping. |
Sorry I took so long. Finally sent in https://mail.haskell.org/pipermail/libraries/2019-April/029537.html |
Could someone please summarize how the libraries proposal went? Also, is there an overview of the proposed API changes? |
There's a number of packages on Hackage providing "non-empty" versions of containers, modeled after https://hackage.haskell.org/package/base/docs/Data-List-NonEmpty.html . There's a variety of use-cases, but perhaps my favorite is that they're needed to "curry" containers: for example,
Map (k0, k1) v
is isomorphic not toMap k0 (Map k1 v)
but toMap k0 (NonEmptyMap k1 v)
. I like this use-case because it comes from the containers themselves.Anyways, the problem with the existing implementations is that they tend to just prepend a minimal element to the container. This creates a less efficient, ill-balanced tree. But the alternative of copying the implementation creates a maintain burden. In https://github.com/mokus0/dependent-map/pull/31/files, I think I found an alternative which is the best of both worlds: mostly reused code and no degrade in balancing, namely making the types mutually recur.
For example, here are the types for
DMap
andNonEmptyDMap
:DMap
is likeMaybe NonEmptyDMap
but with the unboxed strict field.Functions are then also written mutually recursively. (Take the exact weird worker wrapper style here with a grain of salt. This me trying to strike a balance between brevity and cargo culting the way the functions were written before.)
One interesting side benefit is that rotations and other internal operations can be less partial. Take
rotateL
, for example:The non-empty argument proves that at least one left rotation is possible. (The double rotate is still partial.)
As my parenthetical above hints, I haven't yet investigated performance properly; rather my goal was to do just enough work to demonstrate that something like this might be possible. If this sort of refactor looks viable to you, maintainers, I'd be happy to take a stab at doing it for all of containers, along with hunting down any performance issues that arise.
The text was updated successfully, but these errors were encountered: