Skip to content

Commit

Permalink
killing inlines because simplifier ticks are exhausting on 7.8
Browse files Browse the repository at this point in the history
  • Loading branch information
ekmett committed Mar 29, 2014
1 parent 40d31de commit 7b44e6d
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 81 deletions.
15 changes: 0 additions & 15 deletions src/Succinct/Dictionary/Builder.hs
Original file line number Diff line number Diff line change
Expand Up @@ -35,53 +35,41 @@ newtype Builder a b = Builder (forall s. Building (ST s) a b)

instance Profunctor Builder where
dimap f g (Builder k) = Builder (dimap f g k)
{-# INLINE dimap #-}

instance Choice Builder where
left' (Builder k) = Builder (left' k)
{-# INLINE left' #-}
right' (Builder k) = Builder (right' k)
{-# INLINE right' #-}

instance Functor (Builder a) where
fmap f (Builder k) = Builder (fmap f k)
{-# INLINE fmap #-}

instance Applicative (Builder a) where
pure a = Builder (pure a)
{-# INLINE pure #-}
Builder mf <*> Builder ma = Builder (mf <*> ma)
{-# INLINE (<*>) #-}

build :: (Foldable f, Buildable a b) => f a -> b
build = buildWith builder
{-# INLINE build #-}

buildWith :: Foldable f => Builder a b -> f a -> b
buildWith (Builder m) as = runST $ case m of
Building k h z -> do
b <- z
k =<< F.foldlM h b as
{-# INLINE buildWith #-}

class Buildable a b | b -> a where
builder :: Builder a b

instance Buildable a [a] where
builder = Builder $ Building (\k -> return $ k []) (\f a -> return $ f . (a:)) (return id)
{-# INLINE builder #-}

instance U.Unbox a => Buildable a (U.Vector a) where
builder = vector
{-# INLINE builder #-}

instance P.Prim a => Buildable a (P.Vector a) where
builder = vector
{-# INLINE builder #-}

instance Buildable a (V.Vector a) where
builder = vector
{-# INLINE builder #-}

data V a = V {-# UNPACK #-} !Int !a

Expand All @@ -96,7 +84,6 @@ vector = Builder building where
= G.unsafeFreeze
$ INTERNAL_CHECK(checkSlice) "Builder.vector" 0 n (GM.length v)
$ GM.unsafeSlice 0 n v
{-# INLINE vector #-}

unsafeAppend1 :: GM.MVector v a => v s a -> Int -> a -> ST s (v s a)
unsafeAppend1 v i x
Expand All @@ -108,9 +95,7 @@ unsafeAppend1 v i x
INTERNAL_CHECK(checkIndex) "unsafeAppend1" i (GM.length v')
$ GM.unsafeWrite v' i x
return v'
{-# INLINE unsafeAppend1 #-}

-- | Grow a vector logarithmically
enlarge :: GM.MVector v a => v s a -> ST s (v s a)
enlarge v = GM.unsafeGrow v (max (GM.length v) 1)
{-# INLINE enlarge #-}
20 changes: 1 addition & 19 deletions src/Succinct/Dictionary/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -39,37 +39,29 @@ class Access a t | t -> a where

instance Access a [a] where
size = Prelude.length
{-# INLINE size #-}

(!) = (!!)
{-# INLINE (!) #-}

instance Access Bool Word64 where
size _ = 64
{-# INLINE size #-}

(!) = testBit
{-# INLINE (!) #-}

instance Access Bool (U.Vector Bit) where
size (V_Bit n _) = n
{-# INLINE size #-}

(!) (V_Bit n bs) i
= BOUNDS_CHECK(checkIndex) "RangeMin.!" i n
$ testBit (P.unsafeIndex bs $ wd i) (bt i)
{-# INLINE (!) #-}

class Bitwise t where
bitwise :: t -> U.Vector Bit

instance a ~ Bit => Bitwise (U.Vector a) where
bitwise = id
{-# INLINE bitwise #-}

instance Bitwise Word64 where
bitwise a = V_Bit 64 (P.singleton a)
{-# INLINE bitwise #-}

instance a ~ Bool => Bitwise [a] where
bitwise xs = U.fromList (fmap Bit xs)
Expand Down Expand Up @@ -100,7 +92,6 @@ class Access a t => Dictionary a t | t -> a where
-- as long as @0 < i <= 'rank' a t ('size' t)@
select :: a -> t -> Int -> Int
select a t i = search (\j -> rank a t j >= i) i (size t)
{-# INLINE select #-}

-- For testing
instance Eq a => Dictionary a [a] where
Expand All @@ -110,15 +101,13 @@ instance Eq a => Dictionary a [a] where
| a == b = go (acc + 1) (n-1) bs
| otherwise = go acc (n-1) bs
go _ _ [] = Prelude.error "rank []"
{-# INLINE rank #-}

select a xs0 n0 = go 0 n0 xs0 where
go !acc 0 _ = acc
go acc n (b:bs)
| a == b = go (acc + 1) (n-1) bs
| otherwise = go (acc + 1) n bs
go _ _ [] = Prelude.error "select []"
{-# INLINE select #-}

-- | /O(1)/ 'rank' and 'select'
instance Dictionary Bool Word64 where
Expand All @@ -128,11 +117,9 @@ instance Dictionary Bool Word64 where
rank False xs i
| i >= 64 = 64 - popCount xs
| otherwise = i - popCount (xs .&. (bit i - 1))
{-# INLINE rank #-}

select True xs i = selectWord64 xs i
select False xs i = selectWord64 (complement xs) i
{-# INLINE select #-}

-- | Many structures that do not support arbitrary 'rank' can support a
-- limited notion of 'select'.
Expand All @@ -157,11 +144,9 @@ instance a ~ Bool => Select1 [a]

instance Select0 Word64 where
select0 xs i = selectWord64 (complement xs) i
{-# INLINE select0 #-}

instance Select1 Word64 where
select1 xs i = selectWord64 xs i
{-# INLINE select1 #-}

-- | a classic bit-vector-based succinct indexed dictionary
--
Expand Down Expand Up @@ -194,7 +179,6 @@ class (Select0 t, Select1 t, Dictionary Bool t) => Ranked t where
-- @
rank0 :: Ranked t => t -> Int -> Int
rank0 t i = i - rank0 t i
{-# INLINE rank0 #-}

-- |
-- @
Expand All @@ -203,7 +187,6 @@ class (Select0 t, Select1 t, Dictionary Bool t) => Ranked t where
-- @
rank1 :: Ranked t => t -> Int -> Int
rank1 t i = i - rank1 t i
{-# INLINE rank1 #-}

-- | @'rank_' t i@ return the number of bits to the left of position @i@
--
Expand All @@ -220,7 +203,6 @@ class (Select0 t, Select1 t, Dictionary Bool t) => Ranked t where

excess :: Ranked t => t -> Int -> Int
excess t i = rank1 t i - rank0 t i
{-# INLINE excess #-}

-- | Offset binary search
--
Expand All @@ -233,5 +215,5 @@ search p = go where
| otherwise = go (m+1) h
where hml = h - l
m = l + unsafeShiftR hml 1 + unsafeShiftR hml 6
{-# INLINE search #-}
-- {-# INLINE search #-}

4 changes: 0 additions & 4 deletions src/Succinct/Dictionary/RangeMin.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,19 +31,15 @@ data RangeMin = RangeMin
rangeMin :: Bitwise t => t -> RangeMin
rangeMin t = case bitwise t of
V_Bit n bs -> RangeMin n bs $ V.fromList $ levels bs
{-# RULES "rangeMin" rangeMin = id #-}
{-# INLINE [0] rangeMin #-}

instance Access Bool RangeMin where
size (RangeMin n _ _) = n
(!) (RangeMin n bs _) i
= BOUNDS_CHECK(checkIndex) "RangeMin.!" i n
$ testBit (P.unsafeIndex bs $ wd i) (bt i)
{-# INLINE (!) #-}

instance Bitwise RangeMin where
bitwise (RangeMin n bs _) = V_Bit n bs
{-# INLINE bitwise #-}

instance Dictionary Bool RangeMin
instance Select1 RangeMin
Expand Down
34 changes: 17 additions & 17 deletions src/Succinct/Internal/Bit.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,17 +75,17 @@ instance UM.Unbox Bit
data instance UM.MVector s Bit = MV_Bit {-# UNPACK #-} !Int !(PM.MVector s Word64)

instance GM.MVector U.MVector Bit where
{-# INLINE basicLength #-}
{-# INLINE basicUnsafeSlice #-}
{-# INLINE basicOverlaps #-}
{-# INLINE basicUnsafeNew #-}
{-# INLINE basicUnsafeReplicate #-}
{-# INLINE basicUnsafeRead #-}
{-# INLINE basicUnsafeWrite #-}
{-# INLINE basicClear #-}
{-# INLINE basicSet #-}
{-# INLINE basicUnsafeCopy #-}
{-# INLINE basicUnsafeGrow #-}
-- {-# INLINE basicLength #-}
-- {-# INLINE basicUnsafeSlice #-}
-- {-# INLINE basicOverlaps #-}
-- {-# INLINE basicUnsafeNew #-}
-- {-# INLINE basicUnsafeReplicate #-}
-- {-# INLINE basicUnsafeRead #-}
-- {-# INLINE basicUnsafeWrite #-}
-- {-# INLINE basicClear #-}
-- {-# INLINE basicSet #-}
-- {-# INLINE basicUnsafeCopy #-}
-- {-# INLINE basicUnsafeGrow #-}
basicLength (MV_Bit n _) = n
basicUnsafeSlice i n (MV_Bit _ u) = MV_Bit n $ GM.basicUnsafeSlice i (wds n) u
basicOverlaps (MV_Bit _ v1) (MV_Bit _ v2) = GM.basicOverlaps v1 v2
Expand All @@ -110,12 +110,12 @@ instance GM.MVector U.MVector Bit where

data instance U.Vector Bit = V_Bit {-# UNPACK #-} !Int !(P.Vector Word64)
instance G.Vector U.Vector Bit where
{-# INLINE basicLength #-}
{-# INLINE basicUnsafeFreeze #-}
{-# INLINE basicUnsafeThaw #-}
{-# INLINE basicUnsafeSlice #-}
{-# INLINE basicUnsafeIndexM #-}
{-# INLINE elemseq #-}
-- {-# INLINE basicLength #-}
-- {-# INLINE basicUnsafeFreeze #-}
-- {-# INLINE basicUnsafeThaw #-}
-- {-# INLINE basicUnsafeSlice #-}
-- {-# INLINE basicUnsafeIndexM #-}
-- {-# INLINE elemseq #-}
basicLength (V_Bit n _) = n
basicUnsafeFreeze (MV_Bit n u) = liftM (V_Bit n) (G.basicUnsafeFreeze u)
basicUnsafeThaw (V_Bit n u) = liftM (MV_Bit n) (G.basicUnsafeThaw u)
Expand Down
8 changes: 0 additions & 8 deletions src/Succinct/Internal/Building.hs
Original file line number Diff line number Diff line change
Expand Up @@ -16,11 +16,8 @@ data Building m a b where

instance Functor m => Profunctor (Building m) where
dimap f g (Building k h z) = Building (fmap g . k) (\x a -> h x (f a)) z
{-# INLINE dimap #-}
rmap g (Building k h z) = Building (fmap g . k) h z
{-# INLINE rmap #-}
lmap f (Building k h z) = Building k (\x a -> h x (f a)) z
{-# INLINE lmap #-}

instance Applicative m => Choice (Building m) where
left' (Building k h z) = Building (_Left k) step (Left <$> z) where
Expand All @@ -30,7 +27,6 @@ instance Applicative m => Choice (Building m) where

_Left f (Left a) = Left <$> f a
_Left _ (Right b) = pure $ Right b
{-# INLINE left' #-}

right' (Building k h z) = Building (_Right k) step (Right <$> z) where
step (Right x) (Right y) = Right <$> h x y
Expand All @@ -39,19 +35,15 @@ instance Applicative m => Choice (Building m) where

_Right _ (Left b) = pure $ Left b
_Right f (Right a) = Right <$> f a
{-# INLINE right' #-}

instance Functor m => Functor (Building m a) where
fmap f (Building k h z) = Building (fmap f . k) h z
{-# INLINE fmap #-}

instance Applicative m => Applicative (Building m a) where
pure b = Building (\() -> pure b) (\() _ -> pure ()) (pure ())
{-# INLINE pure #-}
Building kf hf zf <*> Building ka ha za = Building
(\(Pair xf xa) -> kf xf <*> ka xa)
(\(Pair xf xa) a -> Pair <$> hf xf a <*> ha xa a)
(Pair <$> zf <*> za)
{-# INLINE (<*>) #-}

data Pair a b = Pair !a !b
13 changes: 2 additions & 11 deletions src/Succinct/Internal/Delta.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,21 +33,16 @@ instance Semigroup Delta where
LT -> Delta (e + e') d' n'
EQ -> Delta (e + e') (d + e) (n + n')
GT -> Delta (e + e') (d + e) n
{-# INLINE (<>) #-}

minima :: Delta -> Int
minima (Delta e d _) = e - d

bool :: Bool -> Delta
bool True = Delta 1 1 1
bool False = Delta (-1) 0 1
{-# INLINE bool #-}

bits :: Bits a => a -> Delta
bits w = Prelude.foldr1 (<>) $ fmap (bool . testBit w) [0..bitSize w - 1]
{-# SPECIALIZE bits :: Word64 -> Delta #-}
{-# SPECIALIZE bits :: Word16 -> Delta #-}
{-# SPECIALIZE bits :: Word8 -> Delta #-}
bits :: FiniteBits a => a -> Delta
bits w = Prelude.foldr1 (<>) $ fmap (bool . testBit w) [0..finiteBitSize w - 1]

e8s, d8s, n8s :: P.Vector Int8
(e8s, d8s, n8s) = case U.fromListN 256 $ fmap go [0..255 :: Word8] of
Expand All @@ -59,16 +54,12 @@ e8s, d8s, n8s :: P.Vector Int8
-- | Look up the 'Delta' for a Word8 via LUTs
byte :: Word8 -> Delta
byte w = Delta (e8 w) (d8 w) (n8 w)
{-# INLINE byte #-}

e8 :: Word8 -> Int
e8 w = fromIntegral $ P.unsafeIndex e8s (fromIntegral w)
{-# INLINE e8 #-}

d8 :: Word8 -> Int
d8 w = fromIntegral $ P.unsafeIndex d8s (fromIntegral w)
{-# INLINE d8 #-}

n8 :: Word8 -> Int
n8 w = fromIntegral $ P.unsafeIndex n8s (fromIntegral w)
{-# INLINE n8 #-}
6 changes: 0 additions & 6 deletions src/Succinct/Internal/Level.hs
Original file line number Diff line number Diff line change
Expand Up @@ -33,31 +33,25 @@ instance Access Delta Level where
size (L8 _ es _ _) = P.length es
size (L16 _ es _ _) = P.length es
size (L64 _ es _ _) = P.length es
{-# INLINE size #-}

(!) (L8 _ es ms ns) i = Delta (fromIntegral (es P.! i)) (fromIntegral (ms P.! i)) (fromIntegral (ns P.! i))
(!) (L16 _ es ms ns) i = Delta (fromIntegral (es P.! i)) (fromIntegral (ms P.! i)) (fromIntegral (ns P.! i))
(!) (L64 _ es ms ns) i = Delta (fromIntegral (es P.! i)) (fromIntegral (ms P.! i)) (fromIntegral (ns P.! i))
{-# INLINE (!) #-}

levelN :: Int -> Int -> [Delta] -> Level
levelN s n xs
| s <= 64 = case U.fromListN n (fmap s8 xs) of UB.V_3 _ (V_Int8 es) (V_Int8 ms) (V_Int8 ns) -> L8 s es ms ns
| s <= 16384 = case U.fromListN n (fmap s16 xs) of UB.V_3 _ (V_Int16 es) (V_Int16 ms) (V_Int16 ns) -> L16 s es ms ns
| otherwise = case U.fromListN n (fmap s64 xs) of UB.V_3 _ (V_Int64 es) (V_Int64 ms) (V_Int64 ns) -> L64 s es ms ns
{-# INLINE levelN #-}

s8 :: Delta -> (Int8, Int8, Int8)
s8 (Delta e m n) = (fromIntegral e, fromIntegral m, fromIntegral n)
{-# INLINE s8 #-}

s16 :: Delta -> (Int16,Int16,Int16)
s16 (Delta e m n) = (fromIntegral e, fromIntegral m, fromIntegral n)
{-# INLINE s16 #-}

s64 :: Delta -> (Int64,Int64,Int64)
s64 (Delta e m n) = (fromIntegral e, fromIntegral m, fromIntegral n)
{-# INLINE s64 #-}

levels :: P.Vector Word64 -> [Level]
levels v = Prelude.reverse $ go 16 (P.length v * 4)
Expand Down
2 changes: 1 addition & 1 deletion src/Succinct/Internal/Spill.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ redundancy s = logBase 2 $ fromIntegral (size s) / fromIntegral (universe s)
-- The resulting scheme has bounded redundancy:
--
-- @redundancy n r <= 2/fromIntegral r@
integral :: (Bits a, Integral a) => a -> a -> Spill a a
integral :: (FiniteBits a, Integral a) => a -> a -> Spill a a
integral n r = Spill
{ bits = m
, spill = fromIntegral $ unsafeShiftR (n + bit m - 1) m
Expand Down
4 changes: 4 additions & 0 deletions src/Succinct/Internal/Word4.hs
Original file line number Diff line number Diff line change
Expand Up @@ -55,9 +55,13 @@ instance Bits Word4 where
complementBit (Word4 n) i = Word4 (complementBit n i .&. 15)
testBit (Word4 n) i = testBit n i
bitSize _ = 4
bitSizeMaybe _ = Just 4
isSigned _ = False
popCount (Word4 n) = popCount n

instance FiniteBits Word4 where
finiteBitSize _ = 4

instance Real Word4 where
toRational (Word4 n) = toRational n

Expand Down

0 comments on commit 7b44e6d

Please sign in to comment.