From 6959b7e4769289983f008d558a71fe0dd2e3d279 Mon Sep 17 00:00:00 2001 From: Mikolaj Konarski Date: Sat, 10 Jan 2026 01:14:24 +0100 Subject: 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. --- src/Data/Array/Nested/Mixed.hs | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) (limited to 'src/Data') 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 = -- cgit v1.2.3-70-g09d2