Skip to content

Commit

Permalink
Merge pull request #153 from haskell/lehins/bytearray
Browse files Browse the repository at this point in the history
Add ByteArray generation
  • Loading branch information
lehins authored Nov 26, 2023
2 parents 9f92421 + 8267ce8 commit a249073
Show file tree
Hide file tree
Showing 14 changed files with 442 additions and 114 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
28 changes: 22 additions & 6 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,11 +1,27 @@
# 1.3.0

* Move `thawGen` from `FreezeGen` into the new `ThawGen` type class. Fixes an issue with
an unlawful instance of `StateGen` for `FreezeGen`.
* Add `modifyGen` and `overwriteGen` to the `FrozenGen` type class
* Add `splitGen` and `splitMutableGen`
* Switch `randomM` and `randomRM` to use `FrozenGen` instead of `RandomGenM`
* Deprecate `RandomGenM` in favor of a more powerful `FrozenGen`
* 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`.
* Add `modifyGen` and `overwriteGen` to the `FrozenGen` type class
* Add `splitGen` and `splitMutableGen`
* Switch `randomM` and `randomRM` to use `FrozenGen` instead of `RandomGenM`
* Deprecate `RandomGenM` in favor of a more powerful `FrozenGen`
* Add `isInRange` to `UniformRange`: [#78](https://github.com/haskell/random/pull/78)
* Add default implementation for `uniformRM` using `Generics`:
[#92](https://github.com/haskell/random/pull/92)
Expand Down
24 changes: 14 additions & 10 deletions bench/Main.hs
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,8 @@ seed = 1337
main :: IO ()
main = do
let !sz = 100000
!sz100MiB = 100 * 1024 * 1024
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 +245,18 @@ 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) sz100MiB
, env getStdGen $ \gen ->
bench "uniformByteArray 100MB" $ nf (\n -> uniformByteArray False n gen) sz100MiB
, env getStdGen $ \gen ->
bench "genByteString 100MB" $ nf (\k -> genByteString k gen) sz100MiB
]
]
]
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
23 changes: 21 additions & 2 deletions src/System/Random.hs
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,28 @@ module System.Random

-- * Pure number generator interface
-- $interfaces
RandomGen(..)
RandomGen
( split
, genWord8
, genWord16
, genWord32
, genWord64
, genWord32R
, genWord64R
, unsafeUniformFillMutableByteArray
)
, uniform
, uniformR
, genByteString
, Random(..)
, Uniform
, UniformRange
, Finite
-- * Generators for sequences of pseudo-random bytes
, uniformByteArray
, uniformByteString
, uniformFillMutableByteArray
, genByteString
, genShortByteString

-- ** Standard pseudo-random number generator
, StdGen
Expand All @@ -45,6 +59,8 @@ module System.Random

-- * Compatibility and reproducibility
-- ** Backwards compatibility and deprecations
, genRange
, next
-- $deprecations

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

0 comments on commit a249073

Please sign in to comment.