haskell / array

Other
10 stars 10 forks source link

Missing NFData instances for unboxed arrays #8

Open meooow25 opened 2 months ago

meooow25 commented 2 months ago

NFData instances for unboxed arrays (UArray, STUArray, IOUArray, StorableArray) are currently missing.

Ideally, they would be defined here, but we can't do that because deepseq (which defines NFData) depends on array.
However, deepseq has agreed to drop this dependency (see https://github.com/haskell/deepseq/issues/102), which makes this possible in the future.

For array, this would mean:

  1. Wait for a new deepseq release which does not depend on array
  2. Depend on deepseq and define the instances.

Does this sound ok to the maintainers here?

July541 commented 2 months ago

Thanks for your work! I wonder if it has any affection on the ghc side since array is the package ghc dependency.

July541 commented 2 months ago

The latest deepseq supports base from 4.12-4.20, and array supports base from 4.9-4.20, which means we have to drop support before base-4.12(ghc-8.6) in the future release.

Bodigrim commented 2 months ago

I wonder if it has any affection on the ghc side since array is the package ghc dependency.

ghc-the-package depends on deepseq directly, so I would not expect any complications.

we have to drop support before base-4.12(ghc-8.6) in the future release.

I think it's reasonable, pretty much every other core package has done it already.

meooow25 commented 2 months ago

The latest deepseq supports base from 4.12-4.20, and array supports base from 4.9-4.20, which means we have to drop support before base-4.12(ghc-8.6) in the future release.

That's a good point. If you have reasons to keep supporting older base, this will have to wait.

July541 commented 1 week ago

I'd like to implement this for the next coming release, but not sure if rnf = rwhnf is enough for unboxed arrays since I don't have a counterexample:(

meooow25 commented 1 week ago

There is no need to force the data for unboxed arrays but we should force the stored index. For example,

instance NFData i => NFData (STUArray s i e) where
  rnf (STUArray l u _ _) = rnf l `seq` rnf u

On a related note, it seems StorableArray is lazy in the Int number of elements, I can't imagine why.

https://github.com/haskell/array/blob/c86635e25d6292f8f8248c426e8a4bfbc802dbc9/Data/Array/Storable/Internals.hs#L42

Probably it should be made strict.