`zipWith5` and `zipWith6` allocate for each element with `-O2`, where `zipWith4` allocates only the result vector
Personne n'a encore pris cette issue.
Évaluation
- Difficulté
- 4/5
- Temps estimé
- 3-5 jours
- Accessibilité débutants
- 48/100
- Type d'issue
- Bug
- Clarté
- Plutôt claire
- Activité
- Active
- Stack technique
- haskell
- Domaine
- performance
Piste de recherche
Start by reading Data.Vector.Generic.zipWith5 and Data.Vector.Fusion.Bundle.Monadic.zipWith5M, then compare the vector-stream implementations of zipWithM, zipWith3M, and zipWith5M named in the issue. Use the supplied Repro.hs benchmark with GHC 9.12.4 and -O2 to inspect allocations and generated Core. Done means zipWith5 and zipWith6 allocate only the result vector, as expected; the issue does not identify a solution.
Rédigé par le modèle d'indexation à partir du texte de l'issue.
Description
By Claude Opus 5.5, based on our experience optimizing my code. Take with a grain of salt. Neither Claude nor me venture any solution. If this is a GHC limitation, maybe there are workarounds? If this is, in fact, a workaround for a GHC limitation, is there a less disastrous one?
Summary
zipWith5 and zipWith6 allocate memory for each element, also with -O2. zipWith4 and a loop over the indices allocate only the result vector.
The table gives the bytes allocated and the instructions executed for each element, for zipWith5 (\ x y z u w -> x + y + z + u + w) on five vectors of n = 200000 Doubles, on GHC 9.12.4 with -O2. "Index loop" is a loop over the indices that reads each vector with unsafeIndexM and writes each result into a new mutable vector. The values with -O1 -fspec-constr are the same.
| boxed | Unboxed | |
|---|---|---|
zipWith5, bytes |
472 | 496 |
| index loop, bytes | 64 | 8 |
zipWith5, instructions |
1619 | 449 |
| index loop, instructions | 149 | 33 |
zipWith6 allocates 632 bytes for each element of a boxed vector and 664 of an Unboxed vector, where the index loop allocates 72 and 8. At n = 200000, zipWith4 allocates the same bytes as the index loop, and executes 1.28 times its instructions at boxed and 1.75 times at Unboxed. For a short vector the difference is the same: at n = 9, Unboxed zipWith5 allocates 485 bytes for each element and the index loop 15.
The generic zipWith5 (lines 1215 to 1225) is unstream of Bundle.zipWith5. Bundle.zipWith5M (lines 364 to 369) and zipWith5M in vector-stream (lines 556 to 576, with zipWith4M and zipWith6M) are zipWithM of zip3 and zip:
zipWith5M f sa sb sc sd se
= zipWithM (\(a,b,c) (d,e) -> f a b c d e) (zip3 sa sb sc) (zip sd se)
zipWithM (lines 502 to 522) and zipWith3M (lines 528 to 554) keep each element that they read in a Just in their state, until they read the elements of the other streams. With -O2, GHC's SpecConstr makes four specialised copies of the loop of zipWith5. In the Core, a specialised copy jumps to the loop that is not specialised, and gives it the state as boxed Ints in tuples and the elements in Just. The loop of zipWith4, which is zipWithM of two zips, stays in its four specialised copies and allocates nothing.
Steps to reproduce
-
Save the program below as
Repro.hs. -
Run it. To select a compiler, add
-w ghc-VERSION.
cabal run -v0 Repro.hs
- On GHC 9.12.4, the output is:
n = 9 boxed zipWith4 72.9 bytes an element
n = 9 boxed index loop, 4 64.9 bytes an element
n = 9 boxed zipWith5 463.1 bytes an element
n = 9 boxed index loop, 5 72.9 bytes an element
n = 9 boxed zipWith6 624.9 bytes an element
n = 9 boxed index loop, 6 80.9 bytes an element
n = 9 Unboxed zipWith4 15.1 bytes an element
n = 9 Unboxed index loop, 4 15.1 bytes an element
n = 9 Unboxed zipWith5 485.3 bytes an element
n = 9 Unboxed index loop, 5 15.1 bytes an element
n = 9 Unboxed zipWith6 655.1 bytes an element
n = 9 Unboxed index loop, 6 15.1 bytes an element
n = 200000 boxed zipWith4 56.0 bytes an element
n = 200000 boxed index loop, 4 56.0 bytes an element
n = 200000 boxed zipWith5 472.0 bytes an element
n = 200000 boxed index loop, 5 64.0 bytes an element
n = 200000 boxed zipWith6 632.0 bytes an element
n = 200000 boxed index loop, 6 72.0 bytes an element
n = 200000 Unboxed zipWith4 8.0 bytes an element
n = 200000 Unboxed index loop, 4 8.0 bytes an element
n = 200000 Unboxed zipWith5 496.0 bytes an element
n = 200000 Unboxed index loop, 5 8.0 bytes an element
n = 200000 Unboxed zipWith6 664.0 bytes an element
n = 200000 Unboxed index loop, 6 8.0 bytes an element
{- cabal:
build-depends: base, vector ==0.13.2.0
ghc-options: -O2
-}
{-# LANGUAGE BangPatterns, RankNTypes #-}
-- Reproducer: zipWith5 and zipWith6 allocate for each element, where
-- zipWith4 and a loop over the indices allocate only the result vector.
--
-- Run: cabal run -v0 Repro.hs
module Main (main) where
import Control.Exception (evaluate)
import Control.Monad (forM_)
import Control.Monad.ST (ST)
import qualified Data.Vector as VB
import qualified Data.Vector.Generic as VG
import qualified Data.Vector.Generic.Mutable as VGM
import qualified Data.Vector.Unboxed as VU
import System.Mem (getAllocationCounter)
import Text.Printf (printf)
-- A vector of n elements, the ith what g i returns.
{-# INLINE indexLoop #-}
indexLoop :: VG.Vector v a => Int -> (forall s. Int -> ST s a) -> v a
indexLoop !n g = VG.create $ do
mv <- VGM.unsafeNew n
let go !i | i >= n = return mv
| otherwise = do
x <- g i
VGM.unsafeWrite mv i x
go (i + 1)
go 0
type V v = v Double
{-# NOINLINE zip4B #-}
{-# NOINLINE loop4B #-}
zip4B, loop4B :: V VB.Vector -> V VB.Vector -> V VB.Vector -> V VB.Vector -> V VB.Vector
zip4B a b c d = VB.zipWith4 (\ x y z u -> x + y + z + u) a b c d
loop4B a b c d = indexLoop (VB.length a) $ \ i -> do
x <- VB.unsafeIndexM a i; y <- VB.unsafeIndexM b i
z <- VB.unsafeIndexM c i; u <- VB.unsafeIndexM d i
return (x + y + z + u)
{-# NOINLINE zip5B #-}
{-# NOINLINE loop5B #-}
zip5B, loop5B :: V VB.Vector -> V VB.Vector -> V VB.Vector -> V VB.Vector -> V VB.Vector -> V VB.Vector
zip5B a b c d e = VB.zipWith5 (\ x y z u w -> x + y + z + u + w) a b c d e
loop5B a b c d e = indexLoop (VB.length a) $ \ i -> do
x <- VB.unsafeIndexM a i; y <- VB.unsafeIndexM b i
z <- VB.unsafeIndexM c i; u <- VB.unsafeIndexM d i
w <- VB.unsafeIndexM e i
return (x + y + z + u + w)
{-# NOINLINE zip6B #-}
{-# NOINLINE loop6B #-}
zip6B, loop6B :: V VB.Vector -> V VB.Vector -> V VB.Vector -> V VB.Vector -> V VB.Vector -> V VB.Vector -> V VB.Vector
zip6B a b c d e f = VB.zipWith6 (\ x y z u w t -> x + y + z + u + w + t) a b c d e f
loop6B a b c d e f = indexLoop (VB.length a) $ \ i -> do
x <- VB.unsafeIndexM a i; y <- VB.unsafeIndexM b i
z <- VB.unsafeIndexM c i; u <- VB.unsafeIndexM d i
w <- VB.unsafeIndexM e i; t <- VB.unsafeIndexM f i
return (x + y + z + u + w + t)
{-# NOINLINE zip4U #-}
{-# NOINLINE loop4U #-}
zip4U, loop4U :: V VU.Vector -> V VU.Vector -> V VU.Vector -> V VU.Vector -> V VU.Vector
zip4U a b c d = VU.zipWith4 (\ x y z u -> x + y + z + u) a b c d
loop4U a b c d = indexLoop (VU.length a) $ \ i -> do
x <- VG.unsafeIndexM a i; y <- VG.unsafeIndexM b i
z <- VG.unsafeIndexM c i; u <- VG.unsafeIndexM d i
return (x + y + z + u)
{-# NOINLINE zip5U #-}
{-# NOINLINE loop5U #-}
zip5U, loop5U :: V VU.Vector -> V VU.Vector -> V VU.Vector -> V VU.Vector -> V VU.Vector -> V VU.Vector
zip5U a b c d e = VU.zipWith5 (\ x y z u w -> x + y + z + u + w) a b c d e
loop5U a b c d e = indexLoop (VU.length a) $ \ i -> do
x <- VG.unsafeIndexM a i; y <- VG.unsafeIndexM b i
z <- VG.unsafeIndexM c i; u <- VG.unsafeIndexM d i
w <- VG.unsafeIndexM e i
return (x + y + z + u + w)
{-# NOINLINE zip6U #-}
{-# NOINLINE loop6U #-}
zip6U, loop6U :: V VU.Vector -> V VU.Vector -> V VU.Vector -> V VU.Vector -> V VU.Vector -> V VU.Vector -> V VU.Vector
zip6U a b c d e f = VU.zipWith6 (\ x y z u w t -> x + y + z + u + w + t) a b c d e f
loop6U a b c d e f = indexLoop (VU.length a) $ \ i -> do
x <- VG.unsafeIndexM a i; y <- VG.unsafeIndexM b i
z <- VG.unsafeIndexM c i; u <- VG.unsafeIndexM d i
w <- VG.unsafeIndexM e i; t <- VG.unsafeIndexM f i
return (x + y + z + u + w + t)
-- Bytes allocated for each element, the mean of reps calls, each result
-- evaluated and its last element read.
measure :: Int -> Int -> (Int -> IO ()) -> IO Double
measure n reps run = do
run 0
a0 <- getAllocationCounter
forM_ [1 .. reps] run
a1 <- getAllocationCounter
return (fromIntegral (a0 - a1) / fromIntegral reps / fromIntegral n)
main :: IO ()
main = forM_ [9, 200000] $ \ n -> do
let reps = max 10 (20000000 `div` n)
bs = [VB.generate n (\ j -> fromIntegral (j + k)) | k <- [1 .. 6 :: Int]]
us = [VU.generate n (\ j -> fromIntegral (j + k)) | k <- [1 .. 6 :: Int]]
run4B g _ = case bs of [a, b', c, d, _, _] -> evaluate (g a b' c d) >>= evaluate . VB.last >> return (); _ -> return ()
run5B g _ = case bs of [a, b', c, d, e, _] -> evaluate (g a b' c d e) >>= evaluate . VB.last >> return (); _ -> return ()
run6B g _ = case bs of [a, b', c, d, e, f] -> evaluate (g a b' c d e f) >>= evaluate . VB.last >> return (); _ -> return ()
run4U g _ = case us of [a, b', c, d, _, _] -> evaluate (g a b' c d) >>= evaluate . VU.last >> return (); _ -> return ()
run5U g _ = case us of [a, b', c, d, e, _] -> evaluate (g a b' c d e) >>= evaluate . VU.last >> return (); _ -> return ()
run6U g _ = case us of [a, b', c, d, e, f] -> evaluate (g a b' c d e f) >>= evaluate . VU.last >> return (); _ -> return ()
cases = [ ("boxed zipWith4", run4B zip4B), ("boxed index loop, 4", run4B loop4B)
, ("boxed zipWith5", run5B zip5B), ("boxed index loop, 5", run5B loop5B)
, ("boxed zipWith6", run6B zip6B), ("boxed index loop, 6", run6B loop6B)
, ("Unboxed zipWith4", run4U zip4U), ("Unboxed index loop, 4", run4U loop4U)
, ("Unboxed zipWith5", run5U zip5U), ("Unboxed index loop, 5", run5U loop5U)
, ("Unboxed zipWith6", run6U zip6U), ("Unboxed index loop, 6", run6U loop6U) ]
forM_ cases $ \ (name, run) -> do
bytes <- measure n reps run
printf "n = %6d %-22s %8.1f bytes an element\n" n name bytes
Expected behavior
zipWith5 and zipWith6 allocate only the result vector, as zipWith4 does.
Environment
- vector-0.13.2.0 and vector-stream-0.1.0.1, from Hackage. On master at d0f42e0562, the generic
zipWith5,Bundle.zipWith5Mand vector-stream'szipWithM,zipWith3M,zipWith4M,zipWith5MandzipWith6Mhave the same code. - GHC 9.12.4 and 9.14.1; cabal-install 3.18.2.0.
- Linux (kernel 7.0.0-38-generic), x86_64 (AMD Ryzen 7 5800X).
- Langage dominant
- Haskell
- Étoiles
- 406
- Forks
- 146
- Métriques de merge des PR
- Aucune PR mergée en 30 j
Préparer son environnement
Ce projet ne fournit ni conteneur de développement, ni Dockerfile, ni guide de contribution : l'installation est à votre charge. Commencez par son README, et consultez notre guide de la première contribution pour les étapes générales.
Par où commencer
- Lisez l'issue en entier, puis le guide de contribution du projet.
- Signalez en commentaire que vous la prenez — cela évite que deux personnes fassent le même travail.
- Forkez le dépôt et travaillez sur une branche.
- Ouvrez une pull request qui référence le numéro de l'issue.
Autres issues de haskell/vector
-
Difficulté 1/5 Moins d'une heure Accessibilité débutants 68/100
-
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 35/100
-
`Size` can be a newtype.Ouverte
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 35/100
-
Difficulté 3/5 1-2 jours Accessibilité débutants 55/100
-
Difficulté 5/5 Plus d'une semaine Accessibilité débutants 35/100
Toutes les issues de haskell/vector
Issues similaires
-
Difficulté 2/5 1-3 heures Accessibilité débutants 74/100
Les mainteneurs répondent en général sous 1 jour
-
Difficulté 2/5 1-3 heures Accessibilité débutants 72/100
haskell/bytestring#730 · 1 commentaire ·
-
Difficulté 2/5 1-3 heures Accessibilité débutants 84/100
seal-harness/seal-harness#399 ·
Les mainteneurs répondent en général sous 1 jour
-
add previously overlooked rulesOuvertebug
Difficulté 2/5 1-3 heures Accessibilité débutants 65/100
fmidue/codeworld-tasks#124 ·
-
allow haskell-src-exts-1.24Ouverte
Difficulté 2/5 1-3 heures Accessibilité débutants 62/100
reflex-frp/reflex#563 ·