Skip to content

Optimize by demoting "denormalized" Integers (i.e. J# -> S#)

In the course of a recent recent reddit discussion it was highlighted, that integer-gmp doesn't try to demote J# result-values to the more efficient S# even though they would fit into a machine word.

The attached proof-of-concept patch introduces a "smart" J# constructor which constructs a S# value instead (if possible):

-- | Demote 'J#' to 'S#' if possible. See also 'smartJ#'.
toSmall :: Integer -> Integer
toSmall i@(S# _)  = i
toSmall (J# 0# _) = S# 0#
toSmall (J# 1# mb#)  | isTrue# (v ># 0#) = S# v
    where
      v = indexIntArray# mb# 0#
toSmall (J# -1# mb#) | isTrue# (v <# 0#) = S# v
    where
      v = negateInt# (indexIntArray# mb# 0#)
toSmall i         = i

-- | Smart 'J#' constructor which tries to construct 'S#' if possible
smartJ# :: Int# -> ByteArray# -> Integer
smartJ# s# mb# = toSmall (J# s# mb#)

The patch replaces a couple of J#-invocations which are likely to produce a S#-fitting Integer. A nofib comparison for vanilla GHC HEAD vs. patched GHC HEAD is attached for further discussion.

Edited by Herbert Valerio Riedel
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information