Skip to content

Commit

Permalink
Add compatibility with recently added ByteArray to base:
Browse files Browse the repository at this point in the history
* 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`
  • Loading branch information
lehins committed Nov 26, 2023
1 parent 1dfa61e commit c3e6fd6
Show file tree
Hide file tree
Showing 14 changed files with 385 additions and 96 deletions.
6 changes: 4 additions & 2 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ jobs:
stack-yaml: stack-old.yaml
- resolver: lts-18
ghc: '8.10.7'
stack-yaml: stack.yaml
stack-yaml: stack.lts-18.yaml
- resolver: lts-19
ghc: '9.0.2'
stack-yaml: stack-coveralls.yaml
Expand Down Expand Up @@ -271,8 +271,10 @@ jobs:
githubToken: ${{ github.token }}
install: |
apt-get update -y
apt-get install -y ghc libghc-tasty-smallcheck-dev libghc-tasty-hunit-dev libghc-splitmix-dev curl
apt-get install -y git ghc libghc-tasty-smallcheck-dev libghc-tasty-hunit-dev libghc-splitmix-dev curl
run: |
git clone https://github.com/Bodigrim/data-array-byte
cp -r data-array-byte/Data .
ghc --version
ghc --make -isrc:test-legacy -o legacy test-legacy/Legacy.hs
./legacy
Expand Down
15 changes: 15 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@
# 1.3.0

* Add compatibility with recently added `ByteArray` to `base`:
[#153](https://github.com/haskell/random/pull/153)
* 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`
* Improve `FrozenGen` interface: [#149](https://github.com/haskell/random/pull/149)
* Move `thawGen` from `FreezeGen` into the new `ThawGen` type class. Fixes an issue with
an unlawful instance of `StateGen` for `FreezeGen`.
Expand Down
22 changes: 12 additions & 10 deletions bench/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ seed = 1337
main :: IO ()
main = do
let !sz = 100000
genLengths :: ([Int], StdGen)
genLengths =
-- create 5000 small lengths that are needed for ShortByteString generation
runStateGen (mkStdGen 2020) $ \g -> replicateM 5000 (uniformRM (16 + 1, 16 + 7) g)
Expand Down Expand Up @@ -243,16 +244,17 @@ main = do
sz
]
]
, bgroup "ShortByteString"
[ env (pure genLengths) $ \ ~(ns, gen) ->
bench "genShortByteString" $
nfIO $ runStateGenT gen $ \g -> mapM (`uniformShortByteString` g) ns
]
, bgroup "ByteString"
[ env getStdGen $ \gen ->
bench "genByteString 100MB" $
nfIO $ runStateGenT gen $ uniformByteStringM 100000000
]
]
, bgroup "Bytes"
[ env (pure genLengths) $ \ ~(ns, gen) ->
bench "uniformShortByteStringM" $
nfIO $ runStateGenT gen $ \g -> mapM (`uniformShortByteStringM` g) ns
, env getStdGen $ \gen ->
bench "uniformByteStringM 100MB" $
nf (runStateGen gen . uniformByteStringM) (100 * 1024 * 1024)
, env getStdGen $ \gen ->
bench "uniformByteArray 100MB" $
nf (\n -> uniformByteArray False n gen) (100 * 1024 * 1024)
]
]
]
Expand Down
2 changes: 2 additions & 0 deletions random.cabal
Original file line number Diff line number Diff line change
Expand Up @@ -100,6 +100,8 @@ library
deepseq >=1.1 && <2,
mtl >=2.2 && <2.4,
splitmix >=0.1 && <0.2
if impl(ghc < 9.4)
build-depends: data-array-byte

test-suite legacy-test
type: exitcode-stdio-1.0
Expand Down
4 changes: 4 additions & 0 deletions src/System/Random.hs
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ module System.Random
, Uniform
, UniformRange
, Finite
-- * Generators for sequences of pseudo-random bytes
, uniformByteArray
, uniformByteString
, uniformFillMutableByteArray

-- ** Standard pseudo-random number generator
, StdGen
Expand Down
Loading

0 comments on commit c3e6fd6

Please sign in to comment.