[Git][ghc/ghc][wip/mod-rem-strict] Make `Int`'s `mod` and `rem` strict in their first arguments

Sebastian Graf gitlab at gitlab.haskell.org
Fri May 15 10:26:45 UTC 2020



Sebastian Graf pushed to branch wip/mod-rem-strict at Glasgow Haskell Compiler / GHC


Commits:
dcef6615 by Sebastian Graf at 2020-05-15T12:26:29+02:00
Make `Int`'s `mod` and `rem` strict in their first arguments

They used to be strict until 4d2ac2d (9 years ago).

It's obviously better to be strict for performance reasons.
It also blocks #18067.

NoFib results:

```
--------------------------------------------------------------------------------
        Program         Allocs    Instrs
--------------------------------------------------------------------------------
        integer          -1.1%     +0.4%
   wheel-sieve2         +21.2%    +20.7%
--------------------------------------------------------------------------------
            Min          -1.1%     -0.0%
            Max         +21.2%    +20.7%
 Geometric Mean          +0.2%     +0.2%
```

The regression in `wheel-sieve2` is due to reboxing that likely will go
away with the resolution of #18067. See !3282 for details.

Fixes #18187.

- - - - -


1 changed file:

- libraries/base/GHC/Real.hs


Changes:

=====================================
libraries/base/GHC/Real.hs
=====================================
@@ -334,11 +334,12 @@ instance  Integral Int  where
                                                   -- in GHC.Int
      | otherwise                  =  a `quotInt` b
 
-    a `rem` b
+    !a `rem` b  -- banged a, because the second branch isn't strict in a
      | b == 0                     = divZeroError
        -- The quotRem CPU instruction fails for minBound `quotRem` -1,
        -- but minBound `rem` -1 is well-defined (0). We therefore
        -- special-case it.
+       -- But this branch is lazy in a, hence the bang to guarantee unboxing.
      | b == (-1)                  = 0
      | otherwise                  =  a `remInt` b
 
@@ -348,11 +349,12 @@ instance  Integral Int  where
                                                   -- in GHC.Int
      | otherwise                  =  a `divInt` b
 
-    a `mod` b
+    !a `mod` b  -- banged a, because the second branch isn't strict in a
      | b == 0                     = divZeroError
        -- The divMod CPU instruction fails for minBound `divMod` -1,
        -- but minBound `mod` -1 is well-defined (0). We therefore
        -- special-case it.
+       -- But this branch is lazy in a, hence the bang to guarantee unboxing.
      | b == (-1)                  = 0
      | otherwise                  =  a `modInt` b
 



View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/dcef6615d765d98a3614291e8ad3256a9ee8561c

-- 
View it on GitLab: https://gitlab.haskell.org/ghc/ghc/-/commit/dcef6615d765d98a3614291e8ad3256a9ee8561c
You're receiving this email because of your account on gitlab.haskell.org.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/ghc-commits/attachments/20200515/9c041fe5/attachment-0001.html>


More information about the ghc-commits mailing list