From 185921752d40abbb42a22d8a9d4ef3b521af2881 Mon Sep 17 00:00:00 2001 From: Alexey Kuleshevich Date: Sun, 22 Dec 2024 16:01:01 -0700 Subject: [PATCH] Make result of floating point computation strict 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. --- src/System/Random/Internal.hs | 8 ++------ 1 file changed, 2 insertions(+), 6 deletions(-) diff --git a/src/System/Random/Internal.hs b/src/System/Random/Internal.hs index 7ac14be4..545e5dd2 100644 --- a/src/System/Random/Internal.hs +++ b/src/System/Random/Internal.hs @@ -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 @@ -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