-
Notifications
You must be signed in to change notification settings - Fork 183
Optimize IntSet.Bin #998
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
Optimize IntSet.Bin #998
Changes from 6 commits
080dde1
b3ae761
40f8503
3d49a54
4a1c12b
fce6246
d18a3c6
bd18faf
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -38,17 +38,15 @@ main = defaultMain $ testGroup "intset-properties" | |
, testProperty "prop_UnionInsert" prop_UnionInsert | ||
, testProperty "prop_UnionAssoc" prop_UnionAssoc | ||
, testProperty "prop_UnionComm" prop_UnionComm | ||
, testProperty "prop_Diff" prop_Diff | ||
, testProperty "prop_Int" prop_Int | ||
, testProperty "prop_union" prop_union | ||
, testProperty "prop_difference" prop_difference | ||
, testProperty "prop_intersection" prop_intersection | ||
, testProperty "prop_Ordered" prop_Ordered | ||
, testProperty "prop_List" prop_List | ||
, testProperty "prop_DescList" prop_DescList | ||
, testProperty "prop_AscDescList" prop_AscDescList | ||
, testProperty "prop_fromList" prop_fromList | ||
, testProperty "prop_fromRange" prop_fromRange | ||
, testProperty "prop_MaskPow2" prop_MaskPow2 | ||
, testProperty "prop_Prefix" prop_Prefix | ||
, testProperty "prop_LeftRight" prop_LeftRight | ||
, testProperty "prop_isProperSubsetOf" prop_isProperSubsetOf | ||
, testProperty "prop_isProperSubsetOf2" prop_isProperSubsetOf2 | ||
, testProperty "prop_isSubsetOf" prop_isSubsetOf | ||
|
@@ -113,9 +111,8 @@ test_split = do | |
Arbitrary, reasonably balanced trees | ||
--------------------------------------------------------------------} | ||
instance Arbitrary IntSet where | ||
arbitrary = do{ xs <- arbitrary | ||
; return (fromList xs) | ||
} | ||
arbitrary = fromList <$> oneof [arbitrary, fmap (fmap getLarge) arbitrary] | ||
shrink = fmap fromList . shrink . toAscList | ||
|
||
{-------------------------------------------------------------------- | ||
Valid IntMaps | ||
|
@@ -232,19 +229,26 @@ prop_UnionComm :: IntSet -> IntSet -> Bool | |
prop_UnionComm t1 t2 | ||
= (union t1 t2 == union t2 t1) | ||
|
||
prop_Diff :: [Int] -> [Int] -> Property | ||
prop_Diff xs ys = | ||
case difference (fromList xs) (fromList ys) of | ||
prop_union :: IntSet -> IntSet -> Property | ||
prop_union xs ys = | ||
case union xs ys of | ||
t -> | ||
valid t .&&. | ||
toAscList t === List.sort ((List.\\) (nub xs) (nub ys)) | ||
toAscList t === List.nub (List.sort (toAscList xs ++ toAscList ys)) | ||
|
||
prop_Int :: [Int] -> [Int] -> Property | ||
prop_Int xs ys = | ||
case intersection (fromList xs) (fromList ys) of | ||
prop_difference :: IntSet -> IntSet -> Property | ||
prop_difference xs ys = | ||
case difference xs ys of | ||
t -> | ||
valid t .&&. | ||
toAscList t === List.sort (nub ((List.intersect) (xs) (ys))) | ||
toAscList t === (toAscList xs List.\\ toAscList ys) | ||
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.
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. Do you mean from data-ordlist? Surely this is not worth a new dependency? 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. Yes, from that package. I imagine we can copy what we like, though I haven't checked its license to be sure. My only real concern about the dependency is that the package doesn't seem to be actively maintained, so it might run into annoying issues with 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. Since the tests are quite fast (as mentioned the other comment), this seems like something that shouldn't cause too much worry. |
||
|
||
prop_intersection :: IntSet -> IntSet -> Property | ||
prop_intersection xs ys = | ||
case intersection xs ys of | ||
t -> | ||
valid t .&&. | ||
toAscList t === (toAscList xs `List.intersect` toAscList ys) | ||
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. Similarly, |
||
|
||
prop_disjoint :: IntSet -> IntSet -> Bool | ||
prop_disjoint a b = a `disjoint` b == null (a `intersection` b) | ||
|
@@ -284,28 +288,6 @@ prop_fromRange = forAll (scale (*100) arbitrary) go | |
go (l,h) = valid t .&&. t === fromAscList [l..h] | ||
where t = fromRange (l,h) | ||
|
||
{-------------------------------------------------------------------- | ||
Bin invariants | ||
--------------------------------------------------------------------} | ||
powersOf2 :: IntSet | ||
powersOf2 = fromList [2^i | i <- [0..63]] | ||
|
||
-- Check the invariant that the mask is a power of 2. | ||
prop_MaskPow2 :: IntSet -> Bool | ||
prop_MaskPow2 (Bin _ msk left right) = member msk powersOf2 && prop_MaskPow2 left && prop_MaskPow2 right | ||
prop_MaskPow2 _ = True | ||
|
||
-- Check that the prefix satisfies its invariant. | ||
prop_Prefix :: IntSet -> Bool | ||
prop_Prefix s@(Bin prefix msk left right) = all (\elem -> match elem prefix msk) (toList s) && prop_Prefix left && prop_Prefix right | ||
prop_Prefix _ = True | ||
|
||
-- Check that the left elements don't have the mask bit set, and the right | ||
-- ones do. | ||
prop_LeftRight :: IntSet -> Bool | ||
prop_LeftRight (Bin _ msk left right) = and [x .&. msk == 0 | x <- toList left] && and [x .&. msk == msk | x <- toList right] | ||
prop_LeftRight _ = True | ||
|
||
{-------------------------------------------------------------------- | ||
IntSet operations are like Set operations | ||
--------------------------------------------------------------------} | ||
|
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 don't see anything checking the prefixes of
Bin
s other than the root. Shouldn't this be calledprefixesOk
, and perform a recursive test?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.
For test performance reasons, I suspect we want to consider validity of the prefix of a
Bin
relative to that of its parent when making the test recursive, treating the (peculiarly simple) case of the root specially.Uh oh!
There was an error while loading. Please reload this page.
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.
Fixed 🤦
Is the trouble really worth it?
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.
For the 0.21s, no. But why does it run for such a short time? Can we make it run more tests? Larger tests? Ideally I want to use more efficient tests and also get that testing time up.
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.
Separately, I suspect that a relative test is likely to help give more useful information in case of failure, though I don't know that for sure. Regardless, I won't block up this PR for that.
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.
This would be the easiest, we can increase the number of quickcheck runs.
Well, if it helps catch errors. Don't know how one would judge that, except maybe coverage. Otherwise it is a waste.
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.
For
Data.Sequence
, that's helpful for dealing with "special casing" in some parts of the code. Maybe it's overkill here.