Skip to content

IntMap.delete allocates even for absent keys #1220

Description

@sjakobi

delete :: Key -> IntMap a -> IntMap a
delete !k t@(Bin p l r)
| nomatch k p = t
| left k p = binCheckL p (delete k l) r
| otherwise = binCheckR p l (delete k r)
delete k t@(Tip ky _)
| k == ky = Nil
| otherwise = t
delete _k Nil = Nil

I noticed some code in GHC that performs a bunch of deletions (on the very similar Word64Map) with keys that are most likely absent from the map. So now I'm wondering whether to introduce pointer-checks in Word64Map.delete or to split off a delete_possiblyAbsent version or…

But since Word64Map is derived from IntMap, my first question is: why doesn't IntMap.delete perform these pointer-checks already, even though Map.delete does?

delete :: Ord k => k -> Map k a -> Map k a
delete = go
where
go :: Ord k => k -> Map k a -> Map k a
go !_ Tip = Tip
go k t@(Bin _ kx x l r) =
case compare k kx of
LT | l' `ptrEq` l -> t
| otherwise -> balanceR kx x l' r
where !l' = go k l
GT | r' `ptrEq` r -> t
| otherwise -> balanceL kx x l r'
where !r' = go k r
EQ -> glue l r
{-# INLINABLE delete #-}

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