[GHC] #13908: sameMutableArray equivalent for Array

GHC ghc-devs at haskell.org
Mon Jul 3 13:43:46 UTC 2017


#13908: sameMutableArray equivalent for Array
-------------------------------------+-------------------------------------
        Reporter:  winter            |                Owner:  (none)
            Type:  bug               |               Status:  closed
        Priority:  normal            |            Milestone:  8.4.1
       Component:  Compiler          |              Version:  8.0.1
      Resolution:  wontfix           |             Keywords:
Operating System:  Unknown/Multiple  |         Architecture:
                                     |  Unknown/Multiple
 Type of failure:  None/Unknown      |            Test Case:
      Blocked By:                    |             Blocking:
 Related Tickets:                    |  Differential Rev(s):  Phab:D3697
       Wiki Page:                    |
-------------------------------------+-------------------------------------
Changes (by bgamari):

 * status:  patch => closed
 * resolution:   => wontfix


@@ -8,1 +8,1 @@
- {{{
+ {{{#!hs

New description:

 Sometime it is useful to compare reference equality for immutable arrays,
 for example when comparing vectors which are slices of arrays, we may want
 to know if two vectors are slices of the same array. But currently we only
 have `sameMutableXXXArray` primitives.

 A workaround may look like this:

 {{{#!hs
 sameArray :: Array a -> Array a -> Bool
 sameArray arr1 arr2 = runST (do
         marr1 <- unsafeThawArray arr1
         marr2 <- unsafeThawArray arr2
         return (sameMutableArray marr1 marr2)
     )
 }}}

 But the problem is for boxed arrays, this code will push an unchanged
 array to mutable list if it's already on older generations. Next GC will
 have to scan it(at least will scan its card table).

 I can see two solutions here, first one is to add `sameXXXArray` for all
 array types. The second one is somehow more complex:

 Currently we simply rewrite array's header to
 `MUT_ARR_PTRS_FROZEN0/SMALL_MUT_ARR_PTRS_FROZEN0` when we do
 `unsafeFreeze`. But for mutable array with no write happend, e.g. the ones
 with `MUT_ARR_PTRS_CLEAN/SMALL_MUT_ARR_PTRS_CLEAN` header. we can safely
 rewrite the header to `MUT_ARR_PTRS_FROZEN/SMALL_MUT_ARR_PTRS_FROZEN`, so
 that we can keep it from next GC.

 Then to fix previous code, we can add a freeze before returning.

 Of course we can do these two solutions all together ; )

--

Comment:

 > Mutable arrays have identity so it makes sense to ask if they are "the
 same".

 That is a fair point. I suppose this is a sensible reason for this
 asymmetry.

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/13908#comment:7>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list