Skip to content

Commit cc7a818

Browse files
committed
Add compatibility with recently added ByteArray to base:
* Switch to using `ByteArray` for type class implementation instead of `ShortByteString` * Add `unsafeUniformFillMutableByteArray` to `RandomGen` and a helper function `defaultUnsafeUniformFillMutableByteArray` that makes implementation for most instances easier. * Add `uniformByteArray`, `uniformByteString` and `uniformFillMutableByteArray` * Add `uniformByteArrayM` to `StatefulGen` * Add `uniformByteStringM` and `uniformShortByteStringM` * Deprecate `uniformShortByteString` in favor of `uniformShortByteStringM` for consistent naming and a future plan of removing it from `StatefulGen` type class * Expose a helper function `genByteArrayST`, that can be used for defining implementation for `uniformByteArrayM`
1 parent 411c7e1 commit cc7a818

File tree

14 files changed

+426
-105
lines changed

14 files changed

+426
-105
lines changed

.github/workflows/ci.yaml

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -108,7 +108,7 @@ jobs:
108108
stack-yaml: stack-old.yaml
109109
- resolver: lts-18
110110
ghc: '8.10.7'
111-
stack-yaml: stack.yaml
111+
stack-yaml: stack.lts-18.yaml
112112
- resolver: lts-19
113113
ghc: '9.0.2'
114114
stack-yaml: stack-coveralls.yaml
@@ -271,8 +271,10 @@ jobs:
271271
githubToken: ${{ github.token }}
272272
install: |
273273
apt-get update -y
274-
apt-get install -y ghc libghc-tasty-smallcheck-dev libghc-tasty-hunit-dev libghc-splitmix-dev curl
274+
apt-get install -y git ghc libghc-tasty-smallcheck-dev libghc-tasty-hunit-dev libghc-splitmix-dev curl
275275
run: |
276+
git clone https://github.com/Bodigrim/data-array-byte
277+
cp -r data-array-byte/Data .
276278
ghc --version
277279
ghc --make -isrc:test-legacy -o legacy test-legacy/Legacy.hs
278280
./legacy

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,20 @@
11
# 1.3.0
22

3+
* Add compatibility with recently added `ByteArray` to `base`:
4+
[#153](https://github.com/haskell/random/pull/153)
5+
* Switch to using `ByteArray` for type class implementation instead of
6+
`ShortByteString`
7+
* Add `unsafeUniformFillMutableByteArray` to `RandomGen` and a helper function
8+
`defaultUnsafeUniformFillMutableByteArray` that makes implementation
9+
for most instances easier.
10+
* Add `uniformByteArray`, `uniformByteString` and `uniformFillMutableByteArray`
11+
* Add `uniformByteArrayM` to `StatefulGen`
12+
* Add `uniformByteStringM` and `uniformShortByteStringM`
13+
* Deprecate `uniformShortByteString` in favor of `uniformShortByteStringM` for
14+
consistent naming and a future plan of removing it from `StatefulGen`
15+
type class
16+
* Expose a helper function `genByteArrayST`, that can be used for
17+
defining implementation for `uniformByteArrayM`
318
* Improve `FrozenGen` interface: [#149](https://github.com/haskell/random/pull/149)
419
* Move `thawGen` from `FreezeGen` into the new `ThawGen` type class. Fixes an issue with
520
an unlawful instance of `StateGen` for `FreezeGen`.

bench/Main.hs

Lines changed: 14 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,8 @@ seed = 1337
2525
main :: IO ()
2626
main = do
2727
let !sz = 100000
28+
!sz100MiB = 100 * 1024 * 1024
29+
genLengths :: ([Int], StdGen)
2830
genLengths =
2931
-- create 5000 small lengths that are needed for ShortByteString generation
3032
runStateGen (mkStdGen 2020) $ \g -> replicateM 5000 (uniformRM (16 + 1, 16 + 7) g)
@@ -243,16 +245,18 @@ main = do
243245
sz
244246
]
245247
]
246-
, bgroup "ShortByteString"
247-
[ env (pure genLengths) $ \ ~(ns, gen) ->
248-
bench "genShortByteString" $
249-
nfIO $ runStateGenT gen $ \g -> mapM (`uniformShortByteString` g) ns
250-
]
251-
, bgroup "ByteString"
252-
[ env getStdGen $ \gen ->
253-
bench "genByteString 100MB" $
254-
nfIO $ runStateGenT gen $ uniformByteStringM 100000000
255-
]
248+
]
249+
, bgroup "Bytes"
250+
[ env (pure genLengths) $ \ ~(ns, gen) ->
251+
bench "uniformShortByteStringM" $
252+
nfIO $ runStateGenT gen $ \g -> mapM (`uniformShortByteStringM` g) ns
253+
, env getStdGen $ \gen ->
254+
bench "uniformByteStringM 100MB" $
255+
nf (runStateGen gen . uniformByteStringM) sz100MiB
256+
, env getStdGen $ \gen ->
257+
bench "uniformByteArray 100MB" $ nf (\n -> uniformByteArray False n gen) sz100MiB
258+
, env getStdGen $ \gen ->
259+
bench "genByteString 100MB" $ nf (\k -> genByteString k gen) sz100MiB
256260
]
257261
]
258262
]

random.cabal

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,8 @@ library
100100
deepseq >=1.1 && <2,
101101
mtl >=2.2 && <2.4,
102102
splitmix >=0.1 && <0.2
103+
if impl(ghc < 9.4)
104+
build-depends: data-array-byte
103105

104106
test-suite legacy-test
105107
type: exitcode-stdio-1.0

src/System/Random.hs

Lines changed: 21 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,28 @@ module System.Random
2020

2121
-- * Pure number generator interface
2222
-- $interfaces
23-
RandomGen(..)
23+
RandomGen
24+
( split
25+
, genWord8
26+
, genWord16
27+
, genWord32
28+
, genWord64
29+
, genWord32R
30+
, genWord64R
31+
, unsafeUniformFillMutableByteArray
32+
)
2433
, uniform
2534
, uniformR
26-
, genByteString
2735
, Random(..)
2836
, Uniform
2937
, UniformRange
3038
, Finite
39+
-- * Generators for sequences of pseudo-random bytes
40+
, uniformByteArray
41+
, uniformByteString
42+
, uniformFillMutableByteArray
43+
, genByteString
44+
, genShortByteString
3145

3246
-- ** Standard pseudo-random number generator
3347
, StdGen
@@ -45,6 +59,8 @@ module System.Random
4559

4660
-- * Compatibility and reproducibility
4761
-- ** Backwards compatibility and deprecations
62+
, genRange
63+
, next
4864
-- $deprecations
4965

5066
-- ** Reproducibility
@@ -199,6 +215,9 @@ uniformR r g = runStateGen g (uniformRM r)
199215
-- >>> unpack . fst . genByteString 10 $ pureGen
200216
-- [51,123,251,37,49,167,90,109,1,4]
201217
--
218+
-- /Note/ - This function is equivalet to `uniformByteString` and will be deprecated in
219+
-- the next major release.
220+
--
202221
-- @since 1.2.0
203222
genByteString :: RandomGen g => Int -> g -> (ByteString, g)
204223
genByteString n g = runStateGenST g (uniformByteStringM n)

0 commit comments

Comments
 (0)