Skip to content

Commit a534648

Browse files
committed
Use dupIO to avoid unwanted sharing when encoding (fixes #318)
This calls dupIO before evaluating each token in the token stream.
1 parent 7e9f525 commit a534648

File tree

2 files changed

+20
-1
lines changed

2 files changed

+20
-1
lines changed

cborg/cborg.cabal

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,11 @@ flag optimize-gmp
5959
manual: False
6060
description: Use optimized code paths for integer-gmp
6161

62+
flag dupIO
63+
default: False
64+
manual: True
65+
description: Use dupIO to avoid unwanted sharing when encoding
66+
6267
--------------------------------------------------------------------------------
6368
-- Library
6469

@@ -100,6 +105,10 @@ library
100105
primitive >= 0.5 && < 0.10,
101106
text >= 1.1 && < 1.3 || >= 2.0 && <2.2
102107

108+
if flag(dupIO)
109+
cpp-options: -DDUPIO
110+
build-depends: dupIO >= 0.1.0 && < 0.2
111+
103112
if flag(optimize-gmp)
104113
cpp-options: -DOPTIMIZE_GMP
105114
if impl(ghc >= 9.0)

cborg/src/Codec/CBOR/Write.hs

Lines changed: 11 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -75,6 +75,10 @@ import qualified Codec.CBOR.ByteArray.Sliced as BAS
7575
import Codec.CBOR.Encoding
7676
import Codec.CBOR.Magic
7777

78+
#if defined(DUPIO)
79+
import qualified Data.Dup as Dup
80+
#endif
81+
7882
--------------------------------------------------------------------------------
7983

8084
-- | Turn an 'Encoding' into a lazy 'L.ByteString' in CBOR binary
@@ -110,7 +114,13 @@ buildStep vs1 k (BI.BufferRange op0 ope0) =
110114
go vs1 op0
111115
where
112116
go vs !op
113-
| op `plusPtr` bound <= ope0 = case vs of
117+
| op `plusPtr` bound <= ope0 = do
118+
#if defined(DUPIO)
119+
dup_vs <- Dup.dupIO vs
120+
case dup_vs of
121+
#else
122+
case vs of
123+
#endif
114124
TkWord x vs' -> PI.runB wordMP x op >>= go vs'
115125
TkWord64 x vs' -> PI.runB word64MP x op >>= go vs'
116126

0 commit comments

Comments
 (0)