blaze-builder 0.3.1.1 → 0.3.2.0
raw patch · 6 files changed
+34/−3 lines, 6 filesdep ~base
Dependency ranges changed: base
Files
- Blaze/ByteString/Builder.hs +1/−0
- Blaze/ByteString/Builder/Internal.hs +13/−0
- CHANGES +4/−0
- README.markdown +1/−1
- blaze-builder.cabal +2/−2
- tests/Tests.hs +13/−0
Blaze/ByteString/Builder.hs view
@@ -79,6 +79,7 @@ , fromWrite , fromWriteSingleton , fromWriteList+ , writeToByteString -- ** Writing 'Storable's , writeStorable
Blaze/ByteString/Builder/Internal.hs view
@@ -39,6 +39,7 @@ -- * Writes , module Blaze.ByteString.Builder.Internal.Write+ , writeToByteString -- * Execution , toLazyByteString@@ -62,6 +63,7 @@ #endif import Control.Monad (unless)+import System.IO.Unsafe (unsafeDupablePerformIO) import qualified Data.ByteString as S import qualified Data.ByteString.Internal as S@@ -357,6 +359,17 @@ toByteStringIO = toByteStringIOWith defaultBufferSize {-# INLINE toByteStringIO #-} +-- | Run a 'Write' to produce a strict 'S.ByteString'.+-- This is equivalent to @('toByteString' . 'fromWrite')@, but is more+-- efficient because it uses just one appropriately-sized buffer.+writeToByteString :: Write -> S.ByteString+writeToByteString !w = unsafeDupablePerformIO $ do+ fptr <- S.mallocByteString (getBound w)+ len <- withForeignPtr fptr $ \ptr -> do+ end <- runWrite w ptr+ return $! end `minusPtr` ptr+ return $! S.fromForeignPtr fptr 0 len+{-# INLINE writeToByteString #-} ------------------------------------------------------------------------------ -- Draft of new builder/put execution code
CHANGES view
@@ -1,3 +1,7 @@+* 0.3.2.0+ - added 'writeToByteString' to construct a strict bytestring in a single+ step. We can actually view 'Write's as strict-bytestring builders.+ * 0.3.1.1 - Changed imports of Foreign.Unsafe to make it GHC 7.8 compatible - -Wall clean on GHC 7.0 - 7.6
README.markdown view
@@ -4,7 +4,7 @@ This library allows to efficiently serialize Haskell values to lazy bytestrings with a large average chunk size. The large average chunk size allows to make good use of cache prefetching in later processing steps (e.g. compression) and-reduces the sytem call overhead when writing the resulting lazy bytestring to a+reduces the system call overhead when writing the resulting lazy bytestring to a file or sending it over the network. This library was inspired by the module Data.Binary.Builder provided by the
blaze-builder.cabal view
@@ -1,5 +1,5 @@ Name: blaze-builder-Version: 0.3.1.1+Version: 0.3.2.0 Synopsis: Efficient buffered output. Description: @@ -9,7 +9,7 @@ Haskell values to lazy bytestrings with a large average chunk size. The large average chunk size allows to make good use of cache prefetching in later processing steps- (e.g. compression) and reduces the sytem call overhead+ (e.g. compression) and reduces the system call overhead when writing the resulting lazy bytestring to a file or sending it over the network.
tests/Tests.hs view
@@ -32,6 +32,7 @@ , testProperty "string and text" stringAndText , testProperty "lazy bytestring identity" identityLazyByteString , testProperty "flushing identity" identityFlushing+ , testProperty "writeToByteString" writeToByteStringProp , testCase "escaping case 1" escaping1 , testCase "escaping case 2" escaping2 , testCase "escaping case 3" escaping3@@ -66,6 +67,9 @@ b2 = fromString s2 in b1 `mappend` b2 == b1 `mappend` flush `mappend` b2 +writeToByteStringProp :: Write -> Bool+writeToByteStringProp w = toByteString (fromWrite w) == writeToByteString w+ escaping1 :: Assertion escaping1 = fromString "<hello>" @?= fromHtmlEscapedString "<hello>" @@ -78,6 +82,9 @@ instance Show Builder where show = show . toLazyByteString +instance Show Write where+ show = show . fromWrite+ instance Eq Builder where b1 == b2 = -- different and small buffer sizses for testing wrapping behaviour@@ -91,6 +98,12 @@ instance Arbitrary Builder where arbitrary = (mconcat . replicate numRepetitions . fromString) <$> arbitrary++instance Arbitrary Write where+ arbitrary = mconcat . map singleWrite <$> arbitrary+ where+ singleWrite (Left bs) = writeByteString (LB.toStrict bs)+ singleWrite (Right w) = writeWord8 w instance Arbitrary LB.ByteString where arbitrary = (LB.concat . replicate numRepetitions . LB.pack) <$> arbitrary