Skip to content

Commit

Permalink
Remove the internals of unordered-containers
Browse files Browse the repository at this point in the history
  • Loading branch information
adithyaov committed Oct 1, 2023
1 parent b17e51e commit 80e42b9
Show file tree
Hide file tree
Showing 4 changed files with 3 additions and 59 deletions.
3 changes: 2 additions & 1 deletion cabal.project.set-1
Original file line number Diff line number Diff line change
Expand Up @@ -11,4 +11,5 @@ constraints:
, bytestring == 0.10.10.0
, vector == 0.12.2.0
, aeson == 1.5.6.0
, containers == 0.6.0.1
, containers == 0.6.0.1
, unordered-containers == 0.2.13.0
50 changes: 0 additions & 50 deletions src/Streamly/Data/Serialize/Instances.hs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,6 @@ module Streamly.Data.Serialize.Instances () where
-- Imports
--------------------------------------------------------------------------------

import Control.Monad.ST (stToIO)
import Data.Fixed (Fixed)
import Data.Hashable (Hashable)
import Data.Int (Int64)
Expand All @@ -37,10 +36,6 @@ import qualified Data.Vector.Mutable as MVector
import qualified Streamly.Internal.Data.Serialize.TH as Serialize
import qualified Streamly.Internal.Data.Unbox as Unbox

-- Internal imports
-- import qualified Data.HashMap.Internal as HashMap (Leaf)
import qualified Data.HashMap.Internal.Array as HMArr

--------------------------------------------------------------------------------
-- Time
--------------------------------------------------------------------------------
Expand Down Expand Up @@ -89,51 +84,6 @@ instance (Ord k, Serialize k, Serialize v) => Serialize (Map k v) where
-- HashMap
--------------------------------------------------------------------------------

instance Serialize a => Serialize (HMArr.Array a) where

{-# INLINE size #-}
size :: Int -> HMArr.Array a -> Int
size acc = HMArr.foldl' size (acc + Unbox.sizeOf (Proxy :: Proxy Int64))

{-# INLINE serialize #-}
serialize :: Int -> MutableByteArray -> HMArr.Array a -> IO Int
serialize off arr val = do
let len = HMArr.length val
mval <- stToIO $ HMArr.unsafeThaw val
finalOffset <-
unfillArray len 0 (off + Unbox.sizeOf (Proxy :: Proxy Int64)) mval
Unbox.pokeByteIndex off arr ((fromIntegral :: Int -> Int64) len)
pure finalOffset

where

unfillArray len acc off1 hmArr
| acc >= len = pure off1
| otherwise = do
v <- stToIO $ HMArr.read hmArr acc
off2 <- serialize off1 arr v
unfillArray len (acc + 1) off2 hmArr

{-# INLINE deserialize #-}
deserialize :: Int -> MutableByteArray -> Int -> IO (Int, HMArr.Array a)
deserialize off arr s = do

(off1, len64) <- deserialize off arr s
let len = (fromIntegral :: Int64 -> Int) len64
val <- stToIO $ HMArr.new_ len
(off2, val1) <- fillArray len 0 off1 val
val2 <- stToIO $ HMArr.unsafeFreeze val1
pure (off2, val2)

where

fillArray len acc off1 val
| acc >= len = pure (off1, val)
| otherwise = do
(off2, v) <- deserialize off1 arr s
stToIO $ HMArr.write val acc v
fillArray len (acc + 1) off2 val

-- Comparing 2 stategies for encoding a HashMap
--
-- 1. Direct binary serialization
Expand Down
2 changes: 1 addition & 1 deletion streamly-serialize-instances.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,7 @@ library
, bytestring >= 0.10.0 && < 0.13.0
, vector >= 0.12 && < 0.14
, aeson >= 1.5 && < 2.3
, unordered-containers
, unordered-containers >= 0.2 && < 0.3
, time
, scientific
, containers >= 0.6.0.1 && < 1.0.0
Expand Down
7 changes: 0 additions & 7 deletions test/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,6 @@ import qualified Data.ByteString as StrictByteString
import qualified Data.ByteString.Lazy as LazyByteString

import qualified Data.HashMap.Strict as SHashMap
import qualified Data.HashMap.Internal.Array as HArr

#if (!(MIN_VERSION_aeson(2,0,3)))
import Test.QuickCheck
Expand All @@ -43,9 +42,6 @@ import Test.Hspec as H
-- Arbitrary instances
--------------------------------------------------------------------------------

instance Eq a => Eq (HArr.Array a) where
(==) a b = HArr.sameArray1 (==) a b

#if (!(MIN_VERSION_aeson(2,0,3)))

instance Arbitrary Aeson.Value where
Expand Down Expand Up @@ -137,9 +133,6 @@ testCases = do
prop "Vector"
$ \(x :: Vector.Vector String) -> roundtrip x

prop "HashMap (Array Int)"
$ \(x :: [Int]) -> roundtrip (HArr.fromList (length x) x)

prop "Strict HashMap"
$ \(x :: SHashMap.HashMap TextS.Text TextS.Text) -> roundtrip x

Expand Down

0 comments on commit 80e42b9

Please sign in to comment.