Skip to content

Commit

Permalink
Make result of floating point computation strict
Browse files Browse the repository at this point in the history
Leaving a thunk around for the computed floating point value leads to a
significant performance degradation when generating many floating point
values and also lead to low and high range values being retained in
memory for longer than necessary.
  • Loading branch information
lehins committed Dec 23, 2024
1 parent efe6cf7 commit 1859217
Showing 1 changed file with 2 additions and 6 deletions.
8 changes: 2 additions & 6 deletions src/System/Random/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1400,7 +1400,7 @@ instance UniformRange Double where
return $! h + l
| otherwise = do
x <- uniformDouble01M g
return $ x * l + (1 -x) * h
return $! x * l + (1 - x) * h
{-# INLINE uniformRM #-}
isInRange = isInRangeOrd

Expand Down Expand Up @@ -1437,14 +1437,10 @@ instance UniformRange Float where
uniformRM (l, h) g
| l == h = return l
| isInfinite l || isInfinite h =
-- Optimisation exploiting absorption:
-- (-Infinity) + (anything but +Infinity) = -Infinity
-- (anything but -Infinity) + (+Infinity) = +Infinity
-- (-Infinity) + (+Infinity) = NaN
return $! h + l
| otherwise = do
x <- uniformFloat01M g
return $ x * l + (1 - x) * h
return $! x * l + (1 - x) * h
{-# INLINE uniformRM #-}
isInRange = isInRangeOrd

Expand Down

0 comments on commit 1859217

Please sign in to comment.