ouroboros-network icon indicating copy to clipboard operation
ouroboros-network copied to clipboard

Implement AnchoredSeq.splitAtMeasure

Open facundominguez opened this issue 1 year ago • 1 comments

This PR implements a function that allows to split an AnchoredSeq at an arbitrary measure boundary.

It is helpful in the Genesis implementation to split AnchoredFragments. We need to count the amount of headers that a fragment contains in a window of slots (the genesis window). We currently do this with dropWhileNewest.

We use dropWhileNewest to cut the upper end of the fragment because we don't know the slot of the headers before or after the end of the window, which rules out splitBeforePoint, splitBeforeMeasure, etc.

Also tried filter to keep the headers in the window, but it was a bit slower than dropWhileNewest.

The implementation of splitAtMeasure provided here is at least as fast as dropWhileNewest in our measuring.

Alternatives

Expose a more general function split :: (v -> Bool) -> AnchoredSeq v a b -> AnchoredSeq v a b which delegates to FingerTree.split.

Or expose the implementation of AnchoredSeq, so we can implement this function in a "Utils" module without having to upgrade or fork ouroboros-network-api.

facundominguez avatar May 22 '24 12:05 facundominguez

I will try to review this, but please bear with me as I need to analyze the code in context as well to make sense of it

crocodile-dentist avatar May 27 '24 08:05 crocodile-dentist

Rebased master to resolve conflicts in a changelog.

facundominguez avatar Jul 05 '24 14:07 facundominguez

Should I try merging at another time?

facundominguez avatar Jul 05 '24 16:07 facundominguez