Skip to content

Commit

Permalink
Add note motivating Iterator
Browse files Browse the repository at this point in the history
  • Loading branch information
meooow25 committed Aug 7, 2024
1 parent a0ce261 commit b865ca8
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
2 changes: 2 additions & 0 deletions containers/src/Data/Map/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -4092,6 +4092,8 @@ deleteFindMax t = case maxViewWithKey t of
Iterator
--------------------------------------------------------------------}

-- See Note [Iterator] in Data.Set.Internal

newtype Iterator k a = Iterator (Stack k a)

iterDown :: Map k a -> Stack k a -> Stack k a
Expand Down
12 changes: 12 additions & 0 deletions containers/src/Data/Set/Internal.hs
Original file line number Diff line number Diff line change
Expand Up @@ -1267,6 +1267,18 @@ foldl'Stack f = go
Iterator
--------------------------------------------------------------------}

-- Note [Iterator]
-- ~~~~~~~~~~~~~~~
-- This is an efficient way to consume a Set one element at a time.
-- Alternately, this may be done by toAscList. toAscList when consumed via
-- List.foldr will rewrite to Set.foldr (thanks to rewrite rules), which is
-- quite efficient. However, sometimes that is not possible, such as in the
-- second arg of '==' or 'compare', where manifesting the list cons cells
-- is unavoidable and makes things slower.
--
-- Concretely, compare on Set Int using toAscList takes ~21% more time compared
-- to using Iterator, on GHC 9.6.3.

newtype Iterator a = Iterator (Stack a)

iterDown :: Set a -> Stack a -> Stack a
Expand Down

0 comments on commit b865ca8

Please sign in to comment.