Skip to content

Commit b22bac0

Browse files
committed
Loosen requirements for serializer: only keys should be Ord
Previously in a code we required entire value to have an Ord instance. While it's possible to have such one it leads to problems when we work with generic values of the types we do not know. The other problem is that during the computation we should check the equality for the keys only and do not allow two values with the same key even if they are not equal. So the new type reflects that properly.
1 parent 6214336 commit b22bac0

File tree

2 files changed

+16
-18
lines changed
  • scls-format/src/Cardano/SCLS/Internal/Serializer

2 files changed

+16
-18
lines changed

scls-format/src/Cardano/SCLS/Internal/Serializer/External/Impl.hs

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -17,7 +17,7 @@ import Cardano.Types.SlotNo
1717
import Control.Exception (onException, throwIO)
1818
import Control.Monad.ST (runST)
1919
import Data.ByteString qualified as B
20-
import Data.Function (fix, (&))
20+
import Data.Function (fix, on, (&))
2121
import Data.IORef (IORef, modifyIORef', newIORef, readIORef)
2222

2323
import Data.Map.Strict qualified as Map
@@ -44,7 +44,7 @@ import VectorBuilder.Builder qualified as Builder
4444
import VectorBuilder.MVector qualified as Builder
4545

4646
serialize ::
47-
(MemPack a, Ord a, Typeable a, HasKey a, MemPackHeaderOffset a) =>
47+
(MemPack a, Ord (Key a), Typeable a, HasKey a, MemPackHeaderOffset a) =>
4848
-- | path to resulting file
4949
FilePath ->
5050
-- | Network identifier
@@ -88,7 +88,7 @@ so on, until we have placed a file.
8888
the size of the entries, but it can be changed without modifying the interface.
8989
-}
9090
prepareExternalSortNamespaced ::
91-
(Typeable a, Ord a, MemPack a) =>
91+
(Typeable a, Ord (Key a), HasKey a, MemPack a) =>
9292
FilePath ->
9393
S.Stream (S.Of (InputChunk a)) IO () ->
9494
IO ()
@@ -115,7 +115,7 @@ the input may be unordered and we can have a namespaces to appear
115115
multiple times in the stream
116116
-}
117117
mergeChunks ::
118-
(Ord a) =>
118+
(Ord (Key a), HasKey a) =>
119119
S.Stream (S.Of (InputChunk a)) IO () ->
120120
S.Stream (S.Of (Namespace, V.Vector a)) IO ()
121121
mergeChunks = loop Map.empty
@@ -138,20 +138,17 @@ mergeChunks = loop Map.empty
138138
in if Builder.size i' < chunkSize -- we were no able to fill the chunk, so r is empty
139139
then return $ loop (Map.insert ns i' s') (rest)
140140
else do
141-
let v' = runST do
142-
mv <- Builder.build i'
143-
Tim.sort mv
144-
V.unsafeFreeze mv
141+
let v' = finalizeVector i'
145142
return $ S.yield (ns, v') >> loop (Map.delete ns s') (Step ((ns :> r) :> rest))
146143
loop s (Effect e) = Effect (e >>= \s' -> return (loop s s'))
147144
loop s (Return _) = do
148145
S.each (Map.toList s)
149-
& S.map \(ns, builder) ->
150-
let v = runST do
151-
mv <- Builder.build builder
152-
Tim.sort mv
153-
V.unsafeFreeze mv
154-
in (ns, v)
146+
& S.map \(ns, builder) -> (ns, finalizeVector builder)
147+
finalizeVector :: (Ord (Key a), HasKey a) => Builder.Builder a -> V.Vector a
148+
finalizeVector builder = runST do
149+
mv <- Builder.build builder
150+
Tim.sortBy (compare `on` getKey) mv
151+
V.unsafeFreeze mv
155152

156153
merge2 :: FilePath -> FilePath -> IO ()
157154
merge2 f1 f2 = do

scls-format/src/Cardano/SCLS/Internal/Serializer/Reference/Impl.hs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import Cardano.Types.Network
1414
import Cardano.Types.SlotNo
1515
import Control.Monad.IO.Class (MonadIO (liftIO))
1616
import Control.Monad.ST (runST)
17+
import Data.Function (on)
1718
import Data.Map.Strict (Map)
1819
import Data.Map.Strict qualified as Map
1920
import Data.MemPack
@@ -30,7 +31,7 @@ import VectorBuilder.MVector qualified as Builder
3031
At this point it accepts values from one namespace only.
3132
-}
3233
serialize ::
33-
(MemPack a, Ord a, Typeable a, HasKey a, MemPackHeaderOffset a) =>
34+
(MemPack a, Ord (Key a), Typeable a, HasKey a, MemPackHeaderOffset a) =>
3435
-- | path to resulting file
3536
FilePath ->
3637
-- | Network identifier
@@ -50,7 +51,7 @@ serialize resultFilePath network slotNo plan = do
5051
S.each [n S.:> S.each v | (n, v) <- Map.toList orderedStream]
5152
)
5253
where
53-
mkVectors :: (Ord a) => S.Stream (S.Of (InputChunk a)) IO () -> IO (Map Namespace (V.Vector a))
54+
mkVectors :: (Ord (Key a), HasKey a) => S.Stream (S.Of (InputChunk a)) IO () -> IO (Map Namespace (V.Vector a))
5455
mkVectors = do
5556
S.foldM_
5657
do
@@ -61,10 +62,10 @@ serialize resultFilePath network slotNo plan = do
6162
do
6263
traverse \builder -> pure $ runST do
6364
mv <- Builder.build builder
64-
Tim.sort mv
65+
Tim.sortBy (compare `on` getKey) mv
6566
V.unsafeFreeze mv
6667

67-
mkVector :: (Ord a) => S.Stream (S.Of a) IO () -> IO (Builder.Builder a)
68+
mkVector :: S.Stream (S.Of a) IO () -> IO (Builder.Builder a)
6869
mkVector = S.fold_
6970
do \x e -> x <> Builder.singleton e
7071
do Builder.empty

0 commit comments

Comments
 (0)