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

Catenable builder should track its length #2

Open
andrewthad opened this issue May 9, 2023 · 0 comments
Open

Catenable builder should track its length #2

andrewthad opened this issue May 9, 2023 · 0 comments

Comments

@andrewthad
Copy link
Member

I've tried to resist doing this because it causes more work to be done at the time that a catenable builder is constructed, but ultimately, this worth must be done as some point or another. Right now, flattening a catenable builder requires two steps:

  1. Compute the total length
  2. Allocate a byte array and paste the pieces into place

If every builder kept track of its own length, we could get rid of step 1. How should this change be made. Currently, Builder is defined as:

data Builder
  = Empty
  | Cons {-# UNPACK #-} !Bytes !Builder
  | Snoc !Builder {-# UNPACK #-} !Bytes
  | Append !Builder !Builder

We need something like this instead:

data Builder = Builder Int Body
data Body
  = Empty
  | Cons {-# UNPACK #-} !Bytes {-# UNPACK #-} !Builder
  | Snoc {-# UNPACK #-} !Builder {-# UNPACK #-} !Bytes
  | Append {-# UNPACK #-} !Builder {-# UNPACK #-} !Builder

That's all it should take. We unpack the length-tracking builders into the body data constructors so that we are not incurring additional indirections. In theory, we could create an unboxed newtype Builder# around a tuple of Int# and Body to guarantee that the builder type didn't cost us additional allocations. But I think GHC can probably do fine without the extra help.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant