[commit: ghc] wip/names3: Unique: Simplify encoding of sum uniques (bdb021c)

git at git.haskell.org git at git.haskell.org
Thu Sep 8 18:52:11 UTC 2016


Repository : ssh://git@git.haskell.org/ghc

On branch  : wip/names3
Link       : http://ghc.haskell.org/trac/ghc/changeset/bdb021c3c0401f84a2f429592d230ad1b408168d/ghc

>---------------------------------------------------------------

commit bdb021c3c0401f84a2f429592d230ad1b408168d
Author: Ben Gamari <ben at smart-cactus.org>
Date:   Sat Aug 20 12:50:49 2016 -0400

    Unique: Simplify encoding of sum uniques
    
    The previous encoding was entropically a bit better, but harder to
    encode and decode. Now we just split up the integer part of the unique
    into a bitfield.


>---------------------------------------------------------------

bdb021c3c0401f84a2f429592d230ad1b408168d
 compiler/basicTypes/Unique.hs | 27 +++++++++++----------------
 1 file changed, 11 insertions(+), 16 deletions(-)

diff --git a/compiler/basicTypes/Unique.hs b/compiler/basicTypes/Unique.hs
index 6d1c0e9..eeffa9b 100644
--- a/compiler/basicTypes/Unique.hs
+++ b/compiler/basicTypes/Unique.hs
@@ -378,31 +378,26 @@ mkTupleDataConUnique Unboxed        a = mkUnique '8' (3*a)
 mkCTupleDataConUnique               a = mkUnique 'm' (3*a)    -- CTuples aren't exactly wired-in, but close
 
 --------------------------------------------------
--- Sum arities start from 2. A sum of arity N has N data constructors, so it
--- occupies N+1 slots: 1 TyCon + N DataCons.
+-- Sum arities start from 2. The encoding is a bit funny: we break up the
+-- integral part into bitfields for the arity and alternative index (which is
+-- taken to be 0xff in the case of the TyCon)
 --
--- So arity 2 sum takes uniques 0 (tycon), 1, 2  (2 data cons)
---    arity 3 sum takes uniques 3 (tycon), 4, 5, 6 (3 data cons)
--- etc.
+-- TyCon for sum of arity k:
+--   00000000 kkkkkkkk 11111111
+-- DataCon for sum of arity k and alternative n:
+--   00000000 kkkkkkkk nnnnnnnn
 
 mkSumTyConUnique :: Arity -> Unique
-mkSumTyConUnique arity = mkUnique 'z' (sumUniqsOccupied arity)
+mkSumTyConUnique arity =
+    ASSERT(arity < 0xff)
+    mkUnique 'z' (arity `shiftL` 8 .|. 0xff)
 
 mkSumDataConUnique :: ConTagZ -> Arity -> Unique
 mkSumDataConUnique alt arity
   | alt >= arity
   = panic ("mkSumDataConUnique: " ++ show alt ++ " >= " ++ show arity)
   | otherwise
-  = mkUnique 'z' (sumUniqsOccupied arity + alt + 1 {- skip the tycon -})
-
--- How many unique slots occupied by sum types (including constructors) up to
--- the given arity?
-sumUniqsOccupied :: Arity -> Int
-sumUniqsOccupied arity
-  = ASSERT(arity >= 2)
-    -- 3 + 4 + ... + arity
-    ((arity * (arity + 1)) `div` 2) - 3
-{-# INLINE sumUniqsOccupied #-}
+  = mkUnique 'z' (arity `shiftL` 8 + alt) {- skip the tycon -}
 
 --------------------------------------------------
 dataConRepNameUnique, dataConWorkerUnique :: Unique -> Unique



More information about the ghc-commits mailing list