packages feed

crypto-rng 0.2.0.0 → 0.2.0.1

raw patch · 3 files changed

+15/−6 lines, 3 filesdep −hashablePVP ok

version bump matches the API change (PVP)

Dependencies removed: hashable

API changes (from Hackage documentation)

Files

ChangeLog.md view
@@ -1,5 +1,9 @@ # Revision history for crypto-rng +## 0.2.0.1  -- 2022-02-16++* Better selection strategy for picking generators from the pool.+ ## 0.2.0.0  -- 2022-02-16  * Drop support for GHC < 8.8
crypto-rng.cabal view
@@ -1,5 +1,5 @@ name:                crypto-rng-version:             0.2.0.0+version:             0.2.0.1 synopsis:            Cryptographic random number generator.  description:         Convenient wrapper for the cryptographic random generator@@ -33,7 +33,6 @@                        DRBG              >= 0.5.5  && < 0.6,                        bytestring        >= 0.10.8 && < 0.12,                        crypto-api        >= 0.13.2 && < 0.14,-                       hashable          >= 1.1,                        mtl               >= 2.2.1  && < 2.3,                        exceptions        >= 0.8.3  && < 0.11,                        monad-control     >= 1.0.1  && < 1.1,
src/Crypto/RNG.hs view
@@ -51,7 +51,6 @@ import Data.Bits import Data.ByteString (ByteString) import Data.Either-import Data.Hashable import Data.Primitive.SmallArray import qualified Data.ByteString as BS import qualified System.Random as R@@ -78,9 +77,13 @@  -- | Work with one of the RNGs from the pool. withRNG :: CryptoRNGState -> (RNG -> (a, RNG)) -> IO a-withRNG (CryptoRNGState pool) f = liftIO $ do-  tid <- hash <$> myThreadId-  let mrng = pool `indexSmallArray` (tid `rem` sizeofSmallArray pool)+withRNG (CryptoRNGState pool) f = do+  -- Selection strategy is based on the id of a capability instead of an id of a+  -- thread as that offers much better performance in a typical scenario when+  -- the size of the pool is equal to the number of capabilities and there are+  -- more threads than capabilities.+  (cid, _) <- threadCapability =<< myThreadId+  let mrng = pool `indexSmallArray` (cid `rem` sizeofSmallArray pool)   modifyMVar mrng $ \rng -> do     (a, newRng) <- pure $ f rng     newRng `seq` pure (newRng, a)@@ -93,6 +96,9 @@  -- | Create a new 'CryptoRNGState', based on system entropy with the pool of a -- specific size.+--+-- /Note:/ making the pool bigger than the number of capabilities will not+-- affect anything. newCryptoRNGStateSized   :: MonadIO m   => Int -- ^ Pool size.