Skip to content

Commit

Permalink
Avoid using pinned memory for Hash deserialization
Browse files Browse the repository at this point in the history
During deserialization we create a temporary buffer, whcih is later
converted to `PackedBytes` representation. There is no need fort hat
intermediarey buffer to be backed by pinned memory (i.e. `ByteString`)
This commit switches to using unpinned `ShortByteString` in order to
reduce fragmentation of pinned memory due to deserialization of
milllions of hashes on a running node
  • Loading branch information
lehins committed Nov 20, 2024
1 parent c942c10 commit 1588e3a
Showing 1 changed file with 5 additions and 5 deletions.
10 changes: 5 additions & 5 deletions cardano-crypto-class/src/Cardano/Crypto/Hash/Class.hs
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,7 @@ import qualified Data.ByteString as BS
import qualified Data.ByteString.Base16 as Base16
import qualified Data.ByteString.Char8 as BSC
import Data.ByteString.Short (ShortByteString)
import qualified Data.ByteString.Short as SBS
import Data.MemPack (StateT(StateT), FailT(FailT), MemPack, Unpack(Unpack))
import Data.Word (Word8)
import Numeric.Natural (Natural)
Expand All @@ -86,8 +87,7 @@ import Control.DeepSeq (NFData)

import NoThunks.Class (NoThunks)

import Cardano.Binary (Encoding, FromCBOR(..), Size, ToCBOR(..), decodeBytes,
serialize')
import Cardano.Binary (Encoding, FromCBOR(..), Size, ToCBOR(..), serialize')
import Cardano.Crypto.PackedBytes
import Cardano.Crypto.Util (decodeHexString)
import Cardano.HeapWords (HeapWords (..))
Expand Down Expand Up @@ -357,14 +357,14 @@ instance (HashAlgorithm h, Typeable a) => ToCBOR (Hash h a) where

instance (HashAlgorithm h, Typeable a) => FromCBOR (Hash h a) where
fromCBOR = do
bs <- decodeBytes
case hashFromBytes bs of
sbs <- fromCBOR
case hashFromBytesShort sbs of
Just x -> return x
Nothing -> fail $ "hash bytes wrong size, expected " ++ show expected
++ " but got " ++ show actual
where
expected = sizeHash (Proxy :: Proxy h)
actual = BS.length bs
actual = SBS.length sbs

--
-- Deprecated
Expand Down

0 comments on commit 1588e3a

Please sign in to comment.