Skip to content

Commit 380e221

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 1dfa61e commit 380e221

File tree

14 files changed

+401
-97
lines changed

14 files changed

+401
-97
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: 15 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ seed = 1337
2525
main :: IO ()
2626
main = do
2727
let !sz = 100000
28+
genLengths :: ([Int], StdGen)
2829
genLengths =
2930
-- create 5000 small lengths that are needed for ShortByteString generation
3031
runStateGen (mkStdGen 2020) $ \g -> replicateM 5000 (uniformRM (16 + 1, 16 + 7) g)
@@ -243,16 +244,20 @@ main = do
243244
sz
244245
]
245246
]
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-
]
247+
]
248+
, bgroup "Bytes"
249+
[ env (pure genLengths) $ \ ~(ns, gen) ->
250+
bench "uniformShortByteStringM" $
251+
nfIO $ runStateGenT gen $ \g -> mapM (`uniformShortByteStringM` g) ns
252+
, env getStdGen $ \gen ->
253+
bench "uniformByteStringM 100MB" $
254+
nf (runStateGen gen . uniformByteStringM) (100 * 1024 * 1024)
255+
, env getStdGen $ \gen ->
256+
bench "uniformByteArray 100MB" $
257+
nf (\n -> uniformByteArray False n gen) (100 * 1024 * 1024)
258+
, env getStdGen $ \gen ->
259+
bench "genByteString 100MB" $
260+
nf (\k -> genByteString k gen) (100 * 1024 * 1024)
256261
]
257262
]
258263
]

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: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -23,11 +23,15 @@ module System.Random
2323
RandomGen(..)
2424
, uniform
2525
, uniformR
26-
, genByteString
2726
, Random(..)
2827
, Uniform
2928
, UniformRange
3029
, Finite
30+
-- * Generators for sequences of pseudo-random bytes
31+
, uniformByteArray
32+
, uniformByteString
33+
, uniformFillMutableByteArray
34+
, genByteString
3135

3236
-- ** Standard pseudo-random number generator
3337
, StdGen
@@ -199,6 +203,9 @@ uniformR r g = runStateGen g (uniformRM r)
199203
-- >>> unpack . fst . genByteString 10 $ pureGen
200204
-- [51,123,251,37,49,167,90,109,1,4]
201205
--
206+
-- /Note/ - This function is equivalet to `uniformByteString` and will be deprecated in
207+
-- the next major release.
208+
--
202209
-- @since 1.2.0
203210
genByteString :: RandomGen g => Int -> g -> (ByteString, g)
204211
genByteString n g = runStateGenST g (uniformByteStringM n)

0 commit comments

Comments
 (0)