Skip to content

Commit c3e6fd6

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 c3e6fd6

File tree

14 files changed

+385
-96
lines changed

14 files changed

+385
-96
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: 12 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,17 @@ 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)
256258
]
257259
]
258260
]

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: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -28,6 +28,10 @@ module System.Random
2828
, Uniform
2929
, UniformRange
3030
, Finite
31+
-- * Generators for sequences of pseudo-random bytes
32+
, uniformByteArray
33+
, uniformByteString
34+
, uniformFillMutableByteArray
3135

3236
-- ** Standard pseudo-random number generator
3337
, StdGen

0 commit comments

Comments
 (0)