Skip to content

Commit

Permalink
WIP: experimentally use dupIO to fix #318
Browse files Browse the repository at this point in the history
This adds a new token type TkDup for use when encoding, that duplicates
the contained closure. This means that encodings can explicitly duplicate
thunks that may otherwise end up in the old generation.  It isn't obvious
to me whether this is enough or whether calling dupIO every time would be
better (and whether that might have an unreasonable performance cost).
  • Loading branch information
adamgundry committed May 18, 2023
1 parent ec399a1 commit c87ccfb
Show file tree
Hide file tree
Showing 5 changed files with 11 additions and 0 deletions.
1 change: 1 addition & 0 deletions cborg/cborg.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -94,6 +94,7 @@ library
bytestring >= 0.10.4 && < 0.12,
containers >= 0.5 && < 0.7,
deepseq >= 1.0 && < 1.5,
dupIO,
ghc-prim >= 0.3.1.0 && < 0.11,
half >= 0.2.2.3 && < 0.4,
primitive >= 0.5 && < 0.9,
Expand Down
2 changes: 2 additions & 0 deletions cborg/src/Codec/CBOR/Encoding.hs
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ data Tokens =
-- Special
| TkEncoded {-# UNPACK #-} !B.ByteString Tokens

| TkDup Tokens

| TkEnd
deriving (Show,Eq)

Expand Down
1 change: 1 addition & 0 deletions cborg/src/Codec/CBOR/FlatTerm.hs
Original file line number Diff line number Diff line change
Expand Up @@ -153,6 +153,7 @@ convFlatTerm (Enc.TkFloat64 f ts) = TkFloat64 f : convFlatTerm ts
convFlatTerm (Enc.TkBreak ts) = TkBreak : convFlatTerm ts
convFlatTerm (Enc.TkEncoded bs ts) = decodePreEncoded bs
++ convFlatTerm ts
convFlatTerm (Enc.TkDup ts) = convFlatTerm ts
convFlatTerm Enc.TkEnd = []

--------------------------------------------------------------------------------
Expand Down
3 changes: 3 additions & 0 deletions cborg/src/Codec/CBOR/Pretty.hs
Original file line number Diff line number Diff line change
Expand Up @@ -215,6 +215,8 @@ pprint = do
TkFloat64 _ _ -> termFailure term
TkEncoded _ TkEnd -> ppTkEncoded
TkEncoded _ _ -> termFailure term
TkDup TkEnd -> str "# Dup"
TkDup _ -> termFailure term
TkEnd -> str "# End of input"
where
termFailure t = fail $ unwords ["pprint: Unexpected token:", show t]
Expand Down Expand Up @@ -337,6 +339,7 @@ ppTkFloat64 f = str "# float64" >> parens (shown f)

unconsToken :: Tokens -> Maybe (Tokens, Tokens)
unconsToken TkEnd = Nothing
unconsToken (TkDup tks) = Just (TkDup TkEnd,tks)
unconsToken (TkWord w tks) = Just (TkWord w TkEnd,tks)
unconsToken (TkWord64 w tks) = Just (TkWord64 w TkEnd,tks)
unconsToken (TkInt i tks) = Just (TkInt i TkEnd,tks)
Expand Down
4 changes: 4 additions & 0 deletions cborg/src/Codec/CBOR/Write.hs
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,8 @@ import qualified Codec.CBOR.ByteArray.Sliced as BAS
import Codec.CBOR.Encoding
import Codec.CBOR.Magic

import qualified Data.Dup as Dup

--------------------------------------------------------------------------------

-- | Turn an 'Encoding' into a lazy 'L.ByteString' in CBOR binary
Expand Down Expand Up @@ -192,6 +194,8 @@ buildStep vs1 k (BI.BufferRange op0 ope0) =
(B.byteString x) (buildStep vs' k)
(BI.BufferRange op ope0)

TkDup vs' -> flip go op =<< Dup.dupIO vs'

TkEnd -> k (BI.BufferRange op ope0)

| otherwise = return $ BI.bufferFull bound op (buildStep vs k)
Expand Down

0 comments on commit c87ccfb

Please sign in to comment.