-
Notifications
You must be signed in to change notification settings - Fork 102
Refactor delete
#571
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Refactor delete
#571
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -1158,56 +1158,56 @@ unsafeInsertWithKey f k0 v0 m0 = runST (go h0 k0 v0 0 m0) | |
| -- | \(O(\log n)\) Remove the mapping for the specified key from this map | ||
| -- if present. | ||
| delete :: Hashable k => k -> HashMap k v -> HashMap k v | ||
| delete k m = delete' (hash k) k m | ||
| {-# INLINABLE delete #-} | ||
| delete k = delete' (hash k) k | ||
| {-# INLINE delete #-} | ||
|
|
||
| delete' :: Eq k => Hash -> k -> HashMap k v -> HashMap k v | ||
| delete' h0 k0 m0 = deleteFromSubtree h0 k0 0 m0 | ||
| {-# INLINABLE delete' #-} | ||
| delete' = deleteFromSubtree 0 | ||
| {-# INLINE delete' #-} | ||
|
|
||
| -- | This version of 'delete' can be used on subtrees when a the | ||
| -- | This version of 'delete' can be used on a subtree when the | ||
| -- corresponding 'Shift' argument is supplied. | ||
| deleteFromSubtree :: Eq k => Hash -> k -> Shift -> HashMap k v -> HashMap k v | ||
| deleteFromSubtree !_ !_ !_ Empty = Empty | ||
| deleteFromSubtree h k _ t@(Leaf hy (L ky _)) | ||
| | hy == h && ky == k = Empty | ||
| | otherwise = t | ||
| deleteFromSubtree h k s t@(BitmapIndexed b ary) | ||
| | b .&. m == 0 = t | ||
| | otherwise | ||
| = case A.index# ary i of | ||
| deleteFromSubtree :: Eq k => Shift -> Hash -> k -> HashMap k v -> HashMap k v | ||
| deleteFromSubtree !s !h !k = \case | ||
| Empty -> Empty | ||
| t@(Leaf hy (L ky _)) | ||
| | hy == h && ky == k -> Empty | ||
| | otherwise -> t | ||
| t@(BitmapIndexed b ary) | ||
| | b .&. m == 0 -> t | ||
| | otherwise -> case A.index# ary i of | ||
| (# !st #) -> | ||
| case deleteFromSubtree h k (nextShift s) st of | ||
| case deleteFromSubtree (nextShift s) h k st of | ||
| Empty | A.length ary == 2 | ||
| , (# l #) <- A.index# ary (otherOfOneOrZero i) | ||
| , isLeafOrCollision l | ||
| -> l | ||
| | otherwise | ||
| -> BitmapIndexed (b .&. complement m) (A.delete ary i) | ||
| st' | isLeafOrCollision st' && A.length ary == 1 -> st' | ||
| | st' `ptrEq` st -> t | ||
| st' | st' `ptrEq` st -> t | ||
| | isLeafOrCollision st' && A.length ary == 1 -> st' | ||
|
Comment on lines
+1187
to
+1188
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. This change slightly reduces the code size by moving the pointer-eq check to an earlier point. Only afterwards do we differentiate the alternatives for |
||
| | otherwise -> BitmapIndexed b (A.update ary i st') | ||
| where m = mask h s | ||
| i = sparseIndex b m | ||
| deleteFromSubtree h k s t@(Full ary) = | ||
| where m = mask h s | ||
| i = sparseIndex b m | ||
| t@(Full ary) -> | ||
| case A.index# ary i of | ||
| (# !st #) -> | ||
| case deleteFromSubtree h k (nextShift s) st of | ||
| case deleteFromSubtree (nextShift s) h k st of | ||
| Empty -> | ||
| let ary' = A.delete ary i | ||
| bm = fullBitmap .&. complement (1 `unsafeShiftL` i) | ||
| in BitmapIndexed bm ary' | ||
| st' | st' `ptrEq` st -> t | ||
| | otherwise -> Full (updateFullArray ary i st') | ||
| where i = index h s | ||
| deleteFromSubtree h k _ t@(Collision hy v) | ||
| where i = index h s | ||
| t@(Collision hy ary) | ||
| | h == hy | ||
| , Just i <- indexOf k v | ||
| = if A.length v == 2 | ||
| then case A.index# v (otherOfOneOrZero i) of | ||
| (# l #) -> Leaf h l | ||
| else Collision h (A.delete v i) | ||
| | otherwise = t | ||
| , Just i <- indexOf k ary | ||
| -> if A.length ary == 2 | ||
| then case A.index# ary (otherOfOneOrZero i) of | ||
| (# l #) -> Leaf h l | ||
| else Collision h (A.delete ary i) | ||
| | otherwise -> t | ||
| {-# INLINABLE deleteFromSubtree #-} | ||
|
|
||
| -- | Delete optimized for the case when we know the key is in the map. | ||
|
|
@@ -1845,7 +1845,7 @@ difference = go_difference 0 | |
| go_difference s t1@(Leaf h1 (L k1 _)) t2 | ||
| = lookupCont (\_ -> t1) (\_ _ -> Empty) h1 k1 s t2 | ||
| go_difference _ t1 Empty = t1 | ||
| go_difference s t1 (Leaf h2 (L k2 _)) = deleteFromSubtree h2 k2 s t1 | ||
| go_difference s t1 (Leaf h2 (L k2 _)) = deleteFromSubtree s h2 k2 t1 | ||
|
|
||
| go_difference s t1@(BitmapIndexed b1 ary1) (BitmapIndexed b2 ary2) | ||
| = differenceArrays s b1 ary1 t1 b2 ary2 | ||
|
|
||
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I'm a bit unhappy about the forcing of keys. The upside is that key types likes
IntandShortByteStringare unboxed. The downside is that key types that can't be unboxed to an unlifted representation are unnecessarily evaluated again and again.Maybe we can use
Strict#for this, once it's released?!There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Where/why are keys getting re-forced?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are forced once for each subtree we check. So probably ~1–5 times for a typical HashMap.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
You're only forcing the key to get it to unbox, right? So you need the function to be strict in the key when the key is an unboxable type. The function is "naturally" strict in the key for (non-trivial, non-pathological) unboxable types whenever its hash is present in the map (because you'll test key equality). So I believe you should only need to add explicit strictness in the key where you discover that its hash is not in the map.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Interesting! I agree with your reasoning but I doubt that GHC's strictness analyser could be convinced to believe it too! 😄
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I bet it can, but I need to go back to sleep.