
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