RE: [Haskell-cafe] Ord (IORef a)?

On 22 June 2004 10:38, Simon Peyton-Jones wrote:
From: John Meacham would it be possible to provide an Ord instance for (IORef a)? For things like loop detection, one may need to make many IORef comparasions and being able to use an efficient set would be a really big win.
Since IORefs are created in the IO monad, the actual order can be arbitrary without breaking referential transparency (as long as it doesn't change over the lifetime of the IORef.)
I think that'd be entirely reasonable. Two runs of the same program might give observably different behaviour wrt Ord, but that's ok because the refs are allocates in the monad, as you say.
There is an efficiency cost though. Each IORef would need to have an extra field, to record its allocation ID. (Address is not enough -- the garbage collector can mangle them.)
My own view is that this is fine -- IORefs shouldn't be in your inner loop, so an extra word in each is no big deal. Unless there are views to the contrary, I'd be happy to see this in GHC.
I'm torn. If it were free, it would be a no-brainer. But I know of several cases where IORefs are performance-critical (or at least performance-important). We should implement & measure. Cheers, Simon

On Tue, Jun 22, 2004 at 11:54:17AM +0100, Simon Marlow wrote:
I'm torn. If it were free, it would be a no-brainer. But I know of several cases where IORefs are performance-critical (or at least performance-important). We should implement & measure.
Yeah, i forgot about the GC moving things around. actually, if the GC could be coaxed to move things around, but never change the relative order of IORefs (like a compacting collector), then the address could still be used for Ord and Eq. how is Eq implemented such that the GC doesn't move one side after you read the address of the other? Actually, the problem is exasperated by the fact we still havn't come up with a good standard class to encapsulate references and the monads they work in. If this were solved, then it would be much easier to use a wrapper which includes an identity and would be a good addition to the libraries. John -- John Meacham - ⑆repetae.net⑆john⑈
participants (2)
-
John Meacham
-
Simon Marlow