[GHC] #8638: Optimize by demoting "denormalized" Integers (i.e. J# -> S#)

GHC ghc-devs at haskell.org
Mon Dec 30 15:07:36 UTC 2013


#8638: Optimize by demoting "denormalized" Integers (i.e. J# -> S#)
------------------------------+--------------------------------------------
       Reporter:  hvr         |             Owner:  hvr
           Type:  feature     |            Status:  new
  request                     |         Milestone:  7.8.1
       Priority:  normal      |           Version:  7.7
      Component:  libraries   |  Operating System:  Unknown/Multiple
  (other)                     |   Type of failure:  Runtime performance bug
       Keywords:  integer-    |         Test Case:
  gmp                         |          Blocking:
   Architecture:              |
  Unknown/Multiple            |
     Difficulty:  Unknown     |
     Blocked By:              |
Related Tickets:              |
------------------------------+--------------------------------------------
 In the course of a recent [discussion on
 reddit](http://www.reddit.com/r/haskell/comments/1twtvm/the_problem_with_integer/)
 it was highlighted, that `integer-gmp` doesn't try to demote `J#` to the
 more efficient `S#` if even though they would fit.

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

 {{{
 #!hs
 -- | 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#)
 }}}

 And replaces a couple of `J#`-constructions 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.

--
Ticket URL: <http://ghc.haskell.org/trac/ghc/ticket/8638>
GHC <http://www.haskell.org/ghc/>
The Glasgow Haskell Compiler


More information about the ghc-tickets mailing list