diff options
| author | Mikolaj Konarski <mikolaj.konarski@funktory.com> | 2026-01-10 01:14:24 +0100 |
|---|---|---|
| committer | Mikolaj Konarski <mikolaj.konarski@funktory.com> | 2026-01-12 18:45:16 +0100 |
| commit | 6959b7e4769289983f008d558a71fe0dd2e3d279 (patch) | |
| tree | d9143abeb0852dfe5084017c82f2bb7df63407e6 /src | |
| parent | 53221892788fdc02f21a00f3026ab17f5696490e (diff) | |
Don't force a list of identical elements
This makes a big difference when a pair of lists is attempted
to be streamed and the first one is of trivial primitive elements
(e.g., when implementing a fold as a special case of mapAccum with
the output list containing only ()). Forcing the first trivial list
would cause the second non-trivial list to be represented as nested
thunks, burdening GC greatly.
Diffstat (limited to 'src')
| -rw-r--r-- | src/Data/Array/Nested/Mixed.hs | 11 |
1 files changed, 8 insertions, 3 deletions
diff --git a/src/Data/Array/Nested/Mixed.hs b/src/Data/Array/Nested/Mixed.hs index 113a6e3..b0d32db 100644 --- a/src/Data/Array/Nested/Mixed.hs +++ b/src/Data/Array/Nested/Mixed.hs @@ -39,6 +39,7 @@ import Data.Vector.Storable qualified as VS import Data.Vector.Storable.Mutable qualified as VSM import Foreign.C.Types (CInt) import Foreign.Storable (Storable) +import Foreign.Storable qualified as Storable import GHC.Float qualified (expm1, log1mexp, log1p, log1pexp) import GHC.Generics (Generic) import GHC.TypeLits @@ -931,11 +932,15 @@ mfromList1PrimN n l = Just sn -> mcastPartial (SKnown sn :!% ZKX) (SUnknown () :!% ZKX) Proxy (mfromList1PrimSN sn l) Nothing -> error $ "mfromList1PrimN: length negative (" ++ show n ++ ")" -mfromList1PrimSN :: PrimElt a => SNat n -> [a] -> Mixed '[Just n] a +mfromList1PrimSN :: forall n a. PrimElt a => SNat n -> [a] -> Mixed '[Just n] a mfromList1PrimSN sn l = let ssh = SKnown sn :$% ZSX - xarr = X.fromList1SN sn l - in fromPrimitive $ M_Primitive ssh xarr + in fromPrimitive $ M_Primitive ssh + $ if Storable.sizeOf (undefined :: a) > 0 + then X.fromList1SN sn l + else case l of -- don't force the list if all elements are the same + a0 : _ -> X.replicateScal ssh a0 + [] -> X.fromList1SN sn l mfromListPrimLinear :: forall sh a. PrimElt a => IShX sh -> [a] -> Mixed sh a mfromListPrimLinear sh l = |
