Skip to content

Commit 2f995b2

Browse files
authored
Merge pull request #658 from IntersectMBO/jeltsch/efficient-unsliced-comparison
Make comparison of `Unsliced` values more efficient
2 parents eb8d04c + 47573dd commit 2f995b2

File tree

4 files changed

+63
-2
lines changed

4 files changed

+63
-2
lines changed

lsm-tree.cabal

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -393,6 +393,7 @@ test-suite lsm-tree-test
393393
Test.Database.LSMTree.Internal.Snapshot.Codec
394394
Test.Database.LSMTree.Internal.Snapshot.Codec.Golden
395395
Test.Database.LSMTree.Internal.Snapshot.FS
396+
Test.Database.LSMTree.Internal.Unsliced
396397
Test.Database.LSMTree.Internal.Vector
397398
Test.Database.LSMTree.Internal.Vector.Growing
398399
Test.Database.LSMTree.Internal.WriteBufferBlobs.FS

src/Database/LSMTree/Internal/Unsliced.hs

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ module Database.LSMTree.Internal.Unsliced (
1616

1717
import Control.DeepSeq (NFData)
1818
import Control.Exception (assert)
19+
import Data.ByteString.Short (ShortByteString (SBS))
1920
import Data.Primitive.ByteArray
2021
import qualified Data.Vector.Primitive as VP
2122
import Database.LSMTree.Internal.RawBytes (RawBytes (..))
@@ -69,7 +70,13 @@ instance Show (Unsliced SerialisedKey) where
6970
show x = show (fromUnslicedKey x)
7071

7172
instance Eq (Unsliced SerialisedKey) where
72-
x == y = fromUnslicedKey x == fromUnslicedKey y
73+
Unsliced ba1 == Unsliced ba2 = SBS ba1' == SBS ba2'
74+
where
75+
!(ByteArray ba1') = ba1
76+
!(ByteArray ba2') = ba2
7377

7478
instance Ord (Unsliced SerialisedKey) where
75-
x <= y = fromUnslicedKey x <= fromUnslicedKey y
79+
compare (Unsliced ba1) (Unsliced ba2) = compare (SBS ba1') (SBS ba2')
80+
where
81+
!(ByteArray ba1') = ba1
82+
!(ByteArray ba2') = ba2

test/Main.hs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -35,6 +35,7 @@ import qualified Test.Database.LSMTree.Internal.Serialise.Class
3535
import qualified Test.Database.LSMTree.Internal.Snapshot.Codec
3636
import qualified Test.Database.LSMTree.Internal.Snapshot.Codec.Golden
3737
import qualified Test.Database.LSMTree.Internal.Snapshot.FS
38+
import qualified Test.Database.LSMTree.Internal.Unsliced
3839
import qualified Test.Database.LSMTree.Internal.Vector
3940
import qualified Test.Database.LSMTree.Internal.Vector.Growing
4041
import qualified Test.Database.LSMTree.Internal.WriteBufferBlobs.FS
@@ -82,6 +83,7 @@ main = do
8283
, Test.Database.LSMTree.Internal.Snapshot.Codec.tests
8384
, Test.Database.LSMTree.Internal.Snapshot.Codec.Golden.tests
8485
, Test.Database.LSMTree.Internal.Snapshot.FS.tests
86+
, Test.Database.LSMTree.Internal.Unsliced.tests
8587
, Test.Database.LSMTree.Internal.Vector.tests
8688
, Test.Database.LSMTree.Internal.Vector.Growing.tests
8789
, Test.Database.LSMTree.Internal.WriteBufferBlobs.FS.tests
Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,51 @@
1+
module Test.Database.LSMTree.Internal.Unsliced (tests) where
2+
3+
import Database.LSMTree.Extras.Generators ()
4+
import Database.LSMTree.Internal.Serialise
5+
import Database.LSMTree.Internal.Unsliced
6+
import Test.Tasty
7+
import Test.Tasty.QuickCheck
8+
9+
tests :: TestTree
10+
tests = testGroup "Test.Database.LSMTree.Internal.Unsliced" [
11+
testProperty "prop_makeUnslicedKeyPreservesEq" prop_makeUnslicedKeyPreservesEq
12+
, testProperty "prop_fromUnslicedKeyPreservesEq" prop_fromUnslicedKeyPreservesEq
13+
, testProperty "prop_makeUnslicedKeyPreservesOrd" prop_makeUnslicedKeyPreservesOrd
14+
, testProperty "prop_fromUnslicedKeyPreservesOrd" prop_fromUnslicedKeyPreservesOrd
15+
]
16+
17+
-- 'Eq' on serialised keys is preserved when converting to /unsliced/ serialised
18+
-- keys.
19+
prop_makeUnslicedKeyPreservesEq :: SerialisedKey -> SerialisedKey -> Property
20+
prop_makeUnslicedKeyPreservesEq k1 k2 = checkCoverage $
21+
cover 1 lhs "k1 == k2" $ lhs === rhs
22+
where
23+
lhs = k1 == k2
24+
rhs = makeUnslicedKey k1 == makeUnslicedKey k2
25+
26+
-- 'Eq' on /unsliced/ serialised keys is preserved when converting to serialised
27+
-- keys.
28+
prop_fromUnslicedKeyPreservesEq :: Unsliced SerialisedKey -> Unsliced SerialisedKey -> Property
29+
prop_fromUnslicedKeyPreservesEq k1 k2 = checkCoverage $
30+
cover 1 lhs "k1 == k2" $ lhs === rhs
31+
where
32+
lhs = k1 == k2
33+
rhs = fromUnslicedKey k1 == fromUnslicedKey k2
34+
35+
-- 'Ord' on serialised keys is preserved when converting to /unsliced/
36+
-- serialised keys.
37+
prop_makeUnslicedKeyPreservesOrd :: SerialisedKey -> SerialisedKey -> Property
38+
prop_makeUnslicedKeyPreservesOrd k1 k2 = checkCoverage $
39+
cover 50 lhs "k1 <= k2" $ lhs === rhs
40+
where
41+
lhs = k1 <= k2
42+
rhs = makeUnslicedKey k1 <= makeUnslicedKey k2
43+
44+
-- 'Ord' on /unsliced/ serialised keys is preserved when converting to serialised
45+
-- keys.
46+
prop_fromUnslicedKeyPreservesOrd :: Unsliced SerialisedKey -> Unsliced SerialisedKey -> Property
47+
prop_fromUnslicedKeyPreservesOrd k1 k2 = checkCoverage $
48+
cover 50 lhs "k1 <= k2" $ lhs === rhs
49+
where
50+
lhs = k1 <= k2
51+
rhs = fromUnslicedKey k1 <= fromUnslicedKey k2

0 commit comments

Comments
 (0)