-
Notifications
You must be signed in to change notification settings - Fork 2
INLINABLE insert and delete, bench, small doc additions. #19
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
Open
robinp
wants to merge
7
commits into
larskuhtz:master
Choose a base branch
from
robinp:master
base: master
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
f76284f
Add initial benchmark about public interface ops.
c63f7f6
Mark insert and delete INLINABLE.
e609e45
Cleanup - splitting (and delete bench) not always available.
85b3590
Whitespace removed.
f883803
Small note on using CF as a set without delete.
d7b0163
Fix RARANDOM typo
robinp 2f83d76
Fix RARANDOM typo
robinp 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 |
|---|---|---|
| @@ -0,0 +1,3 @@ | ||
| *.swp | ||
| cabal.project.local* | ||
| dist-newstyle |
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 |
|---|---|---|
| @@ -0,0 +1,83 @@ | ||
| {-# LANGUAGE CPP #-} | ||
| {-# LANGUAGE DataKinds #-} | ||
| {-# LANGUAGE ScopedTypeVariables #-} | ||
| {-# LANGUAGE TypeApplications #-} | ||
|
|
||
| {-# OPTIONS_GHC -fno-warn-orphans #-} | ||
|
|
||
| -- | | ||
| -- Module: Main | ||
| -- Copyright: Copyright © 2019-2021 Lars Kuhtz <lakuhtz@gmail.com> | ||
| -- License: BSD3 | ||
| -- Maintainer: Lars Kuhtz <lakuhtz@gmail.com> | ||
| -- Stability: experimental | ||
| -- | ||
| module Main | ||
| ( main | ||
| ) where | ||
|
|
||
| import Control.Monad (filterM) | ||
| import Control.Monad.Primitive (PrimState) | ||
| import Control.Monad.ST (runST) | ||
| import Criterion | ||
| import Criterion.Main | ||
| import Data.Foldable (traverse_) | ||
|
|
||
| import Data.Cuckoo | ||
|
|
||
| -- -------------------------------------------------------------------------- -- | ||
| -- Main | ||
|
|
||
| main :: IO () | ||
| main = do | ||
| cf10k <- do | ||
| cf <- newCuckooFilter @4 @10 @Int 0 12000 | ||
| traverse_ (insert cf) [1..10000] | ||
| pure $! cf | ||
|
|
||
| defaultMain | ||
| [ bgroup "insert" | ||
| [ insertBench 10000 12000 [1..10000] | ||
| ] | ||
| , bgroup "member" | ||
| [ memberBench "1:1 hit:hiss" 10000 cf10k (interleave [5000..10000] [10001..15000]) | ||
| , memberBench "only hit" 10000 cf10k [1..10000] | ||
| ] | ||
| #ifdef RANDOM_STANDARD | ||
| , bgroup "delete" | ||
| [ deleteBench "all" 10000 cf10k [1..10000] | ||
| ] | ||
| #endif | ||
| ] | ||
|
|
||
| -- -------------------------------------------------------------------------- -- | ||
|
|
||
| instance CuckooFilterHash Int | ||
|
|
||
| insertBench :: Int -> Int -> [Int] -> Benchmark | ||
| insertBench n cap xs0 = bench ("n-" <> show n <> "-cap-" <> show cap) $ whnf f xs0 | ||
| where | ||
| f xs = runST $ do | ||
| cf <- newCuckooFilter @4 @10 @Int 0 (fromIntegral cap) | ||
| failed <- filterM (fmap not . insert cf) xs | ||
| pure $! length failed | ||
|
|
||
| memberBench :: String -> Int -> CuckooFilter (PrimState IO) 4 10 Int -> [Int] -> Benchmark | ||
| memberBench label n cf xs = bench (label <> " - " <> show n) $ whnfIO f | ||
| where | ||
| f = fmap length (filterM (member cf) xs) | ||
|
|
||
| #ifdef RANDOM_STANDARD | ||
| deleteBench :: String -> Int -> CuckooFilter (PrimState IO) 4 10 Int -> [Int] -> Benchmark | ||
| deleteBench label n cf0 xs = bench (label <> " - " <> show n) $ whnfIO f | ||
| where | ||
| f = do | ||
| (cf, _) <- splitCuckooFilter cf0 | ||
| fmap length (filterM (delete cf) xs) | ||
| #endif | ||
|
|
||
| -- | Doesn't cause actual difference in bench. | ||
| interleave :: [a] -> [a] -> [a] | ||
| interleave (a:as) (b:bs) = a:b:interleave as bs | ||
| interleave as bs = as <> bs | ||
|
|
||
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
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
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 |
|---|---|---|
|
|
@@ -109,6 +109,9 @@ module Data.Cuckoo | |
| -- * Debugging Utils | ||
| , showFilter | ||
| , itemHashes | ||
|
|
||
| -- * Testing Utils | ||
| , splitCuckooFilter | ||
| ) where | ||
|
|
||
| import Control.Applicative | ||
|
|
@@ -332,6 +335,24 @@ newCuckooFilter salt n = do | |
| | not (64 <= n) = error "Seriously? Are you kidding me? If you need to represent such a tiny set, you'll have to pick another data structure for that" | ||
| | otherwise = return () | ||
|
|
||
| -- | Creates two new 'CuckooFilter's with identical data as the original, and with the RNG state split. | ||
| -- | ||
| -- This function is not thread-safe. The original CuckooFilter must not be written concurrently during the split operation. | ||
| -- | ||
| -- /IMPORTANT/ For testing purposes only - not all random lib choices support RNG state splitting. | ||
| -- | ||
| splitCuckooFilter | ||
| :: PrimMonad m | ||
| => CuckooFilter (PrimState m) b f a | ||
| -> m (CuckooFilter (PrimState m) b f a, CuckooFilter (PrimState m) b f a) | ||
| splitCuckooFilter (CuckooFilter bc s rng d) = do | ||
| len <- getSizeofMutableByteArray d | ||
| d0 <- cloneMutableByteArray d 0 len | ||
| d1 <- cloneMutableByteArray d 0 len | ||
| (g0, g1) <- genSplit rng | ||
| return $! (CuckooFilter bc s g0 d0, CuckooFilter bc s g1 d1) | ||
|
|
||
|
|
||
| -- -------------------------------------------------------------------------- -- | ||
| -- Insert | ||
|
|
||
|
|
@@ -356,6 +377,11 @@ newCuckooFilter salt n = do | |
| -- function. If this function is used in the presence of asynchronous exceptions | ||
| -- it should be apprioriately masked. | ||
| -- | ||
| -- Note: if the potentially inserted elements have many repetitions, and you | ||
| -- don't intend to delete items, rather just use the filter as a set, then it is | ||
| -- advised to perform a 'member' check before insert. Otherwise repeated items | ||
| -- will take up slots and cause high load or even premature insertion failure. | ||
| -- | ||
|
Comment on lines
+380
to
+384
Owner
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. 👍 |
||
| -- >>> f <- newCuckooFilter @4 @10 @Int 0 1000 | ||
| -- >>> insert f 0 | ||
| -- True | ||
|
|
@@ -401,6 +427,7 @@ insert f a = do | |
| setFingerprint f b i k | ||
| return k' | ||
| {-# INLINE swapFingerprint #-} | ||
| {-# INLINABLE insert #-} | ||
|
|
||
| -- -------------------------------------------------------------------------- -- | ||
| -- Member Test | ||
|
|
@@ -492,6 +519,7 @@ delete f a = do | |
| Nothing -> checkBucket f b2 fp >>= \case | ||
| Just i -> True <$ setFingerprint f b2 i null | ||
| Nothing -> return False | ||
| {-# INLINABLE delete #-} | ||
|
|
||
| -- -------------------------------------------------------------------------- -- | ||
| -- Internal | ||
|
|
||
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.
What is the purpose of splitting the filter here before doing the deletes?
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.
The idea was to pre-create the filter once, and then the benchmark would only time the deletions (not the filter creation). The split was just a way to prevent the original filter from being mutated across the different bench iterations.
But maybe there are other ways to get this, without split - I learned there is
perRunEnvin criterion that lets one create the input separately from the measurement. That would let one get rid of the split functionality.I'm open to whichever way is preferred, or alternatives.