purescript-record icon indicating copy to clipboard operation
purescript-record copied to clipboard

Skip copyRecord for buildFromScratch in Builder

Open jvliwanag opened this issue 4 years ago • 5 comments

Description of the change

Skip needing to copyRecord for buildFromScratch.


Checklist:

  • [X] Added the change to the changelog's "Unreleased" section with a reference to this PR (e.g. "- Made a change (#0000)")
  • [ ] Linked any existing issues or proposals that this pull request should close
  • [ ] Updated or added relevant documentation
  • [ ] Added a test for the contribution (if applicable)

jvliwanag avatar Mar 25 '21 14:03 jvliwanag

In general, I think it's a little dodgy to reason about allocation and references within PureScript like this, since it's outside the semantics of the language, and there's otherwise no indication in the types what's happening. For example, if an optimization were to lift out the {} to the top-level, since it can be trivially shared, you'd wind up with a broken program, where every call is mutating the same reference.

I'm not necessarily giving a thumbs down, because I think it's likely we have issues like this all over the place in core.

natefaubion avatar Mar 25 '21 14:03 natefaubion

One option could be to just move buildFromScratch to the FFI.

natefaubion avatar Mar 25 '21 14:03 natefaubion

If we follow that line of thought though, perhaps we shouldn't be declaring: newtype Builder a b = Builder (a -> b) as well? Since we're hiding the mutation.

jvliwanag avatar Mar 25 '21 15:03 jvliwanag

If we follow that line of thought though, perhaps we shouldn't be declaring: newtype Builder a b = Builder (a -> b) as well? Since we're hiding the mutation.

Right, which is why I provided the caveat:

because I think it's likely we have issues like this all over the place in core.

I'm aware that core uses unsafe functions for builder stuff like this, but gives them pure types within PureScript's type system, which is semantically dodgy. It's like declaring type Effect a = Unit -> a, which is technically true, but I don't think anyone would agree it's a good idea.

natefaubion avatar Mar 25 '21 15:03 natefaubion

Perhaps the cleanest representation of Builder would involve ST. My worry is it might not be as efficient.

Alternatively, we can make Builder a foreign type and move everything onto FFI.

jvliwanag avatar Mar 26 '21 07:03 jvliwanag