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

Investigate rewrite rule for gathering size checks #16

Open
andrewthad opened this issue Feb 27, 2020 · 0 comments
Open

Investigate rewrite rule for gathering size checks #16

andrewthad opened this issue Feb 27, 2020 · 0 comments

Comments

@andrewthad
Copy link
Member

Consider a functions whose purpose is to ensure that N bytes are available in the builder:

ensure :: Int -> Builder

The text library calls this ensureFree and has a rewrite rule for it:

"ensureFree/ensureFree" forall a b .
    append (ensureFree a) (ensureFree b) = ensureFree (max a b)

I don't actually care about this rewrite rule though. I care about another one, one that text doesn't try to do, and I don't know if it was just an oversight or if there is a fundamental problem here. Any builder that can very quickly determine the maximum amount of space it needs can be rewritten to use ensure and an unsafe non-length-checking variant. For example:

copy :: Bytes -> Builder
copy b = ensure (Bytes.length b) <> noLengthCheckCopy b

With two calls to copy and then right phase restrictions on inlining, we should see:

copy a <> copy b
==> inlines to
(ensure (Bytes.length a) <> noLengthCheckCopy a) <> (ensure (Bytes.length b) <> noLengthCheckCopy b)
==> rewrite rule on ensure
ensure (Bytes.length a + Bytes.length b) <> (noLengthCheckCopy a <> noLengthCheckCopy b)

This should work not just for copy but also for fromBounded.

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