Repository navigation
Add keepAlive - #363
Add keepAlive#363
Conversation
|
Looks good. I noticed that |
Co-authored-by: Alexey Kuleshevich <lehins@yandex.ru>
|
I'll leave this open for another day or two in case anyone else with more knowledge about this primop has any feedback, and I'll merge if I don't hear anything. |
|
Hey! Just curious, why does this combinator have the type keepAlive :: PrimBase m => a -> (a -> m r) -> m rand not keepAlive :: PrimBase m => a -> m r -> m r |
|
@mitchellwrosen , that's an extremely good question. It looks like a mistake..... @andrewthad ? |
|
It is analogous to the "with" family of combinators, e.g. withFile. keepAlive x k ensures x is kept alive as long as it is referenced by the body of k. To my knowledge, the signature suggested would not provide better guarantees than the flawed |
|
Okay, maybe there's a real idea here, but I'm not sure it's expressed well enough. The only reasons I can think of to keep a lifted value alive are to keep a weak reference active or (relatedly) to delay calling an attached finalizer. Getting that right may involve some |
For a more thorough explanation, see https://www.haskell.org/ghc/blog/20210607-the-keepAlive-story.html |
|
@chessai I think we all know what the primop is for.... |
Well, the link goes into a detailed breakdown of why the type is the way it is. So I thought it might be helpful. Maybe I'm missing something though. |
|
@chessai , I guess you're talking about the situation where the value isn't supposed to be used after its finalizer runs, like a file handle? I don't know if that's the only use case for the primop. |
|
It is a bit weird to attach finalizers to lifted things, though; this primop is probably more useful in |
|
@chessai , the type of the function is not analogous to the type of the primop, which is unusual. I can imagine a reason for such a function, but as I said I don't think this realizes that exactly. |
|
I'd expect the fancy version to look more like keepAlive x k = unsafeIOToPrim $ primitive $ \s0 -> keepAlive# x s0 $ internal $ unsafePrimToIO $ k (lazy x)The |
|
@chessai Thanks for the information. I (tried to) read the article you linked and I admit I didn't get exact clarity on your response:
I believe the documentation of
ðĪ |
|
I agree that the type of because it most closely matches the type of the primop but with @treeowl describes attaching finalizers to lifted things as "weird". I would go one step further and call it "dangerous". But it's something that GHC explicitly allows, so I think it's reasonable to support it. I think that a useful addition would be Which would call |
|
@andrewthad I think it would probably be best to have a discussion of such things as part of a broader discussion of the fate of |
|
Since you raised the issue, here are my thoughts. If there's a reasonable way to do it, I think it would be nice to fold pieces of |
|
fromUnlifted# x = fromShortByteStringUnsafe (SBS x)bothers me. I'd be much happier if the |
|
Yes, the instance for |
|
Ah, you're right. I didn't look closely at the primop before commenting, instead working off memory. This is really unfortunate. Could we do an impact analysis to determine how much it's actually used? It's so new, I doubt many are even using it. There are 4 of some of the most active users of primitive in this thread and it just got noticed. Fixing it would have to be a major version bump, unless we soft deprecated and waited, like Andrew said. Let me know if an impact assessment would be useful, and I can do it. |
|
I think we should fix it and do a major version bump. |
This is my preference as well |
|
@andrewthad Yeah, maybe we should keep |
Closes #362.