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

Better unsafe perform io #325

Merged
merged 2 commits into from Jul 21, 2019

Conversation

treeowl
Copy link
Contributor

@treeowl treeowl commented Jul 11, 2019

The previous code was struggling to create artificial dependencies
to prevent unsafePerformIO calls from being floated out of their
proper context. Push the context into each unsafePerformIO call
so the dependency is real. Reading a bit of the resulting Core,
I don't expect to see many (if any) performance regressions.
Performance will hopefully improve a bit if/when
GHC issue 15127
is fixed.

The previous code was struggling to create artificial dependencies
to prevent `unsafePerformIO` calls from being floated out of their
proper context. Push the context into each `unsafePerformIO` call
so the dependency is real. Reading a bit of the resulting Core,
I don't expect to see many (if any) performance regressions.
Performance will hopefully improve a bit if/when
[GHC issue 15127](https://gitlab.haskell.org/ghc/ghc/issues/15127)
is fixed.
@treeowl
Copy link
Contributor Author

treeowl commented Jul 11, 2019

I believe it should be safe to use unsafeDupablePerformIO rather than unsafePerformIO. What's less clear to me is whether that's a good idea for performance; feel free to experiment!

src/Reflex/Spider/Internal.hs Show resolved Hide resolved
src/Reflex/Spider/Internal.hs Show resolved Hide resolved
src/Reflex/Spider/Internal.hs Show resolved Hide resolved
unsafeBuildDynamic readV0 v' = Dyn $ unsafeNewIORef x $ UnsafeDyn x
where x = (readV0, v')
unsafeBuildDynamic readV0 v' =
Dyn $ unsafePerformIO $ newIORef $ UnsafeDyn (readV0, v')
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

If this gets called many times, then it might be worth working around GHC 15127 by using runRW# manually to unbox the IORef.

unsafeNewIORef :: a -> IORef a
unsafeNewIORef a = IORef $ STRef ( runRW# $ \s ->
  case newMutVar# a (noDuplicate# s) of
    (# _s', mv #) -> mv )

or

unsafeDupableNewIORef :: a -> IORef a
unsafeDupableNewIORef a = IORef $ STRef ( runRW# $ \s ->
  case newMutVar# a s of
    (# _s', mv #) -> mv )

Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't think it does - because unsafeBuildDynamic is not a very frequently used function. Current benchmarks show no significant difference (for any of these changes)

@treeowl treeowl mentioned this pull request Jul 13, 2019
@oliver-batchelor oliver-batchelor merged commit dc3ce15 into reflex-frp:develop Jul 21, 2019
ali-abrar added a commit that referenced this pull request Aug 1, 2019
* 'release/0.6.2.1' of github.com:ryantrinkle/reflex: (30 commits)
  Loosen monoidal-containers version bounds
  Update CONTRIBUTING.md
  Update CONTRIBUTING.md
  Use unsafePerformIO better (#325)
  Generalize fan following DMap (#318)
  Distribute more generally
  Generalize merge
  Drop *Tag classes
  Fix deprecation warnings related to updated Data.Some
  Bump dependent-sum to latest version
  fan: Rewrite haddock
  Replace hackage link with badge
  Use Some for more existential types (which should be efficient now that it is a newtype)
  Bump upper bounds
  Typo
  DRY conditional compilation
  Bump 'these' upper bound
  Typo
  Update changelog for holdDyn fix
  Make holdDyn lazy in its Event again
  ...
matthewbauer added a commit that referenced this pull request Jan 31, 2020
ryantrinkle added a commit that referenced this pull request May 27, 2020
ryantrinkle added a commit that referenced this pull request May 29, 2020
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

Successfully merging this pull request may close these issues.

None yet

3 participants