Skip to content

IntSet's minView and maxView creates unwanted thunks #1251

Description

@meooow25

maxView :: IntSet -> Maybe (Key, IntSet)
maxView t =
case t of Nil -> Nothing
Bin p l r | signBranch p -> case go l of (result, l') -> Just (result, binCheckL p l' r)
_ -> Just (go t)
where
go (Bin p l r) = case go r of (result, r') -> (result, binCheckR p l r')
go (Tip kx bm) = case highestBitSet bm of bi -> (kx + bi, tip kx (bm .&. complement (bitmapOfSuffix bi)))
go Nil = error "maxView Nil"

Core:

Rec {
-- RHS size: {terms: 47, types: 28, coercions: 0, joins: 0/1}
$wgo1 [InlPrag=[2], Occ=LoopBreaker] :: IntSet -> (# Int, IntSet #)
[GblId[StrictWorker([!])], Arity=1, Str=<1L>, Unf=OtherCon []]
$wgo1
  = \ (ds_sdOH :: IntSet) ->
      case ds_sdOH of {
        Bin bx_dcyn l_a73T r_a73U ->
          case $wgo1 r_a73U of { (# ww_se6X, ww1_se6Y #) ->
          (# ww_se6X,
             case ww1_se6Y of wild2_X4 {
               __DEFAULT -> Bin bx_dcyn l_a73T wild2_X4;
               Nil -> l_a73T
             } #)
          };
        Tip bx_dcyp bx1_dcyq ->
          let {
            bi_sd64 :: Int#
            [LclId]
            bi_sd64 = -# 63# (word2Int# (clz# bx1_dcyq)) } in
          (# I# (+# bx_dcyp bi_sd64),
             case and# bx1_dcyq (not# (uncheckedShiftL# 1## bi_sd64))
             of ds1_X3 {
               __DEFAULT -> Tip bx_dcyp ds1_X3;
               0## -> Nil
             } #);
        Nil -> case lvl42_reKj of {}
      }

Just using a strict pair instead of lazy should fix it. IntMap's definition seems pretty good and how I would define it for IntSet too, splitting the empty map case out and inlining it to potentially get rid of the Maybe.

maxViewWithKey :: IntMap a -> Maybe ((Key, a), IntMap a)
maxViewWithKey t = case t of
Nil -> Nothing
_ -> Just $ case maxViewWithKeySure t of
View k v t' -> ((k, v), t')
{-# INLINE maxViewWithKey #-}
maxViewWithKeySure :: IntMap a -> View a
maxViewWithKeySure t =
case t of
Nil -> error "maxViewWithKeySure Nil"
Bin p l r | signBranch p ->
case go l of View k a l' -> View k a (binCheckL p l' r)
_ -> go t
where
go (Bin p l r) =
case go r of View k a r' -> View k a (binCheckR p l r')
go (Tip k y) = View k y Nil
go Nil = error "maxViewWithKey_go Nil"
-- See note on NOINLINE at minViewWithKeySure
{-# NOINLINE maxViewWithKeySure #-}

Activity

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions