Skip to content

Commit 1c8fa69

Browse files
committed
leverage lazy bytestring for 'Serialise' CBOR-in-CBOR.
The main use case for this type lies within the StateQuery protocol, where it can be particularly useful to obtain a plain CBOR response. This is particularly useful for large query results such as DebugNewEpochState (multiple GB on mainnet). The network library makes a great effort at trying to serialise and deserialise bytes lazily throughout; and these efforts are unfortunately destroyed by this implementation that would here evaluate the entire ByteString when decoding and encoding. So on a machine that would have both a server and client using this library, we would pay twice the cost of fully evaluating in memory the entire response, instead of leverage lazy IO as, I believe, is originally intended. Note that 'decode' and 'encode' here rely on the default implementation for lazy ByteString in Codec.Serialise, which do the right thing: encode lazy bytestrings as indefinite sequences of byte chunks; effectively preserving the laziness in both directions.
1 parent df7e419 commit 1c8fa69

File tree

1 file changed

+2
-5
lines changed
  • ouroboros-network-api/src/Ouroboros/Network

1 file changed

+2
-5
lines changed

ouroboros-network-api/src/Ouroboros/Network/Block.hs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -473,16 +473,13 @@ fromSerialised dec (Serialised payload) =
473473
--
474474
-- TODO: replace with encodeEmbeddedCBOR from cborg-0.2.4 once
475475
-- it is available, since that will be faster.
476-
--
477-
-- TODO: Avoid converting to a strict ByteString, as that requires copying O(n)
478-
-- in case the lazy ByteString consists of more than one chunks.
479476
instance Serialise (Serialised a) where
480477
encode (Serialised bs) = mconcat [
481478
Enc.encodeTag 24
482-
, Enc.encodeBytes (Lazy.toStrict bs)
479+
encode bs
483480
]
484481

485482
decode = do
486483
tag <- Dec.decodeTag
487484
when (tag /= 24) $ fail "expected tag 24 (CBOR-in-CBOR)"
488-
Serialised . Lazy.fromStrict <$> Dec.decodeBytes
485+
Serialised <$> decode

0 commit comments

Comments
 (0)