-- Hoogle documentation, generated by Haddock -- See Hoogle, http://www.haskell.org/hoogle/ -- | Library for efficiently building up buffers, one piece at a time -- -- Data.BufferBuilder is an efficient library for incrementally -- building up ByteStrings, one chunk at a time. Early benchmarks -- show it is over twice as fast as ByteString Builder, primarily because -- BufferBuilder is built upon an ST-style restricted monad and -- mutable state instead of ByteString Builder's monoidal AST. -- -- Internally, BufferBuilder is backed by a few C functions. Examination -- of GHC's output shows nearly optimal code generation with no -- intermediate thunks -- and thus, continuation passing and its -- associated indirect jumps and stack traffic only occur when -- BufferBuilder is asked to append a non-strict ByteString. -- -- I benchmarked four approaches with a URL encoding benchmark: -- --
-- import qualified Data.BufferBuilder as BB -- -- let byteString = BB.runBufferBuilder $ do -- BB.appendBS "http" -- BB.appendChar8 '/' -- BB.appendBS "//" ---- -- This package also provides Data.BufferBuilder.Utf8 for -- generating UTF-8 buffers and Data.BufferBuilder.Json for -- encoding data structures into JSON. @package buffer-builder @version 0.2.4.9 -- | A library for efficiently building up a buffer of data. When given -- data known to be strict, use of BufferBuilder compiles directly into a -- series of efficient C function calls. module Data.BufferBuilder -- | BufferBuilder is the type of a monadic action that appends to an -- implicit, growable buffer. Use runBufferBuilder to extract the -- resulting buffer as a ByteString. data BufferBuilder a -- | Run a sequence of BufferBuilder actions and extract the -- resulting buffer as a ByteString. runBufferBuilder :: BufferBuilder a -> ByteString -- | Run a sequence of BufferBuilder actions and extract the -- resulting buffer as a ByteString. Also returns the -- BufferBuilder's result. runBufferBuilder' :: BufferBuilder a -> (a, ByteString) data Options Options :: !Int -> !Bool -> Options [initialCapacity] :: Options -> !Int [trimFinalBuffer] :: Options -> !Bool runBufferBuilderWithOptions :: Options -> BufferBuilder a -> ByteString runBufferBuilderWithOptions' :: Options -> BufferBuilder a -> (a, ByteString) -- | Given a BufferBuilder, calculate its length. This runs every -- BufferBuilder action in a mode that simply accumulates the number of -- bytes without copying any data into an output buffer. calculateLength :: BufferBuilder a -> Int -- | Reads current length of BufferBuilder. If memory allocation has failed -- at any point, this returns zero. In the future, currentLength may -- throw an exception upon memory allocation failure. currentLength :: BufferBuilder Int -- | Append a single byte to the output buffer. To append multiple bytes in -- sequence and avoid redundant bounds checks, consider using -- appendBS, appendLiteral, or unsafeAppendLiteralN. appendByte :: Word8 -> BufferBuilder () -- | Appends a character to the buffer, truncating it to the bottom 8 bits. appendChar8 :: Char -> BufferBuilder () -- | Appends a ByteString to the buffer. When appending constant, -- hardcoded strings, to avoid a CAF and the costs of its associated tag -- check and indirect jump, use appendLiteral or -- unsafeAppendLiteralN instead. appendBS :: ByteString -> BufferBuilder () -- | Appends a lazy ByteString to the buffer. This function operates -- by traversing the lazy ByteString chunks, appending each in -- turn. appendLBS :: ByteString -> BufferBuilder () -- | Appends a zero-terminated MagicHash string literal. Use this function -- instead of appendBS for string constants. For example: -- --
-- appendLiteral "true"# ---- -- If the length of the string literal is known, calling -- unsafeAppendLiteralN is faster, as unsafeAppendLiteralN -- avoids a strlen operation which has nontrivial cost in some -- benchmarks. appendLiteral :: Addr# -> BufferBuilder () -- | Appends a MagicHash string literal with a known length. Use this when -- the string literal's length is known. For example: -- --
-- unsafeAppendLiteralN 4 "true"# ---- -- Per byte, this is the fastest append function. It amounts to a C -- function call with two constant arguments. The C function checks to -- see if it needs to grow the buffer and then it simply calls memcpy. -- -- WARNING: passing an incorrect length value is likely to cause -- an access violation or worse. unsafeAppendLiteralN :: Int -> Addr# -> BufferBuilder () appendByte7 :: Word8 -> BufferBuilder () appendChar7 :: Char -> BufferBuilder () appendBS7 :: ByteString -> BufferBuilder () appendLiteral7 :: Addr# -> BufferBuilder () unsafeAppendLiteralN7 :: Int -> Addr# -> BufferBuilder () -- | Appends a UTF-8-encoded Char to the buffer. appendCharUtf8 :: Char -> BufferBuilder () -- | Appends a UTF-8-encoded String to the buffer. The best way to -- improve performance here is to use ByteString or Text -- instead of String. appendStringUtf8 :: String -> BufferBuilder () -- | Appends a decimal integer, just like calling printf("%d", ...) appendDecimalSignedInt :: Int -> BufferBuilder () -- | Appends a decimal double, just like calling printf("%f", ...) appendDecimalDouble :: Double -> BufferBuilder () appendEscapedJson :: ByteString -> BufferBuilder () appendEscapedJsonLiteral :: Addr# -> BufferBuilder () appendEscapedJsonText :: Text -> BufferBuilder () -- | Append a percent-encoded ByteString. All characters except for -- alphanumerics, -, ., _, and ~ will be encoded. The string produced by -- URL encoding is guaranteed ASCII-7 and thus valid UTF-8. Moreover, it -- is not required to be JSON-escaped. appendUrlEncoded :: ByteString -> BufferBuilder () instance GHC.Show.Show Data.BufferBuilder.BufferOutOfMemoryError instance GHC.Exception.Type.Exception Data.BufferBuilder.BufferOutOfMemoryError instance GHC.Base.Functor Data.BufferBuilder.BufferBuilder instance GHC.Base.Applicative Data.BufferBuilder.BufferBuilder instance GHC.Base.Monad Data.BufferBuilder.BufferBuilder -- | A library for efficiently building up a buffer of UTF-8-encoded text. -- If only safe functions are used, the resulting ByteString is -- guaranteed to be valid UTF-8. -- -- To run a sequence of Utf8Builder actions and retrieve the resulting -- buffer, use runUtf8Builder. -- -- In special situations, for maximum performance, unsafe functions are -- also provided. The unsafe functions do not guarantee the buffer is -- correct UTF-8. -- -- This module is built on top of Data.BufferBuilder. module Data.BufferBuilder.Utf8 data Utf8Builder a -- | Run a sequence of Utf8Builder actions and extracting the -- resulting buffer as a ByteString. runUtf8Builder :: Utf8Builder () -> ByteString -- | Encodes the given Text in UTF-8, appending it to the buffer. appendText :: Text -> Utf8Builder () -- | Encodes the given String in UTF-8, appending it to the buffer. appendString :: String -> Utf8Builder () -- | Encodes a single Char in UTF-8, appending it to the buffer. appendChar :: Char -> Utf8Builder () -- | Appends the bottom 7 bits of a byte to the buffer. appendByte7 :: Word8 -> Utf8Builder () -- | Appends the bottom 7 bits of a Char to the buffer. appendChar7 :: Char -> Utf8Builder () -- | Appends the given ByteString to the buffer, taking the bottom 7 bits -- of each byte. appendBS7 :: ByteString -> Utf8Builder () -- | Appends the zero-terminated byte string at the given address to the -- buffer, taking the bottom 7 bits of each byte. appendLiteral7 :: Addr# -> Utf8Builder () -- | Directly calls appendUrlEncoded. The output from URL -- percent-encoding is guaranteed to be valid UTF-8. appendUrlEncoded :: ByteString -> Utf8Builder () appendDecimalSignedInt :: Int -> Utf8Builder () appendDecimalDouble :: Double -> Utf8Builder () appendEscapedJson :: ByteString -> Utf8Builder () appendEscapedJsonLiteral :: Addr# -> Utf8Builder () appendEscapedJsonText :: Text -> Utf8Builder () -- | Directly append a BufferBuilder into the UTF-8 code stream. Incorrect -- use of this function can result in invalid UTF-8. unsafeAppendBufferBuilder :: BufferBuilder () -> Utf8Builder () -- | Directly append a byte into the UTF-8 code stream. Incorrect use of -- this function can result in invalid UTF-8. unsafeAppendByte :: Word8 -> Utf8Builder () -- | Directly append the bottom 8 bits of the given character to the UTF-8 -- code stream. Incorrect use of this function can result in invalid -- UTF-8. unsafeAppendChar8 :: Char -> Utf8Builder () -- | Directly append the zero-terminated byte sequence pointed to by the -- given address. Be careful that the referenced byte sequence contains -- valid UTF-8. unsafeAppendLiteral :: Addr# -> Utf8Builder () -- | Directly append the given byte sequence pointed to by the given -- address. Be careful that the referenced byte sequence contains valid -- UTF-8. -- -- WARNING: passing an incorrect length value is likely to cause -- an access violation or worse. unsafeAppendLiteralN :: Int -> Addr# -> Utf8Builder () -- | Directly append the given ByteString to the output buffer. Be -- careful that the referenced ByteString contains valid UTF-8. unsafeAppendBS :: ByteString -> Utf8Builder () instance GHC.Base.Monad Data.BufferBuilder.Utf8.Utf8Builder instance GHC.Base.Applicative Data.BufferBuilder.Utf8.Utf8Builder instance GHC.Base.Functor Data.BufferBuilder.Utf8.Utf8Builder -- | A library for efficiently building up a valid JSON document. -- -- The difference between Data.BufferBuilder.Json and the -- excellent Data.Aeson is that Aeson represents the JSON document -- as an in-memory tree structure before encoding it into bytes. This -- module, on the other hand, represents each value as an action that -- writes its representation directly into the output buffer. At the cost -- of reduced flexibility, this results in significantly improved -- encoding performance. At the time of this writing, encoding a custom -- record type into JSON using this module was almost 5x faster than -- using Aeson. -- -- This module is built on top of Data.Utf8Builder. module Data.BufferBuilder.Json -- | Represents a JSON value. -- -- Values are built up from either ToJson instances or from -- primitives like emptyObject, array, and null. -- -- In special cases, or when performance is of utmost importance, the -- unsafe functions unsafeAppendUtf8Builder are available. -- -- Internally, Value encodes an action or sequence of actions that append -- JSON-encoded text to the underlying Utf8Builder. data Value -- | The class of types that can be converted to JSON values. See -- ObjectBuilder for an example of writing a ToJson -- instance for a custom data type. -- -- ToJson instances are provided for many common types. For -- example, to create a JSON array, call toJson on a list or -- Vector. To create a JSON object, call toJson on a -- HashMap. class ToJson a toJson :: ToJson a => a -> Value -- | Encode a value into a ByteString containing valid UTF-8-encoded -- JSON. The argument value must have a corresponding ToJson -- instance. -- -- WARNING: There are three cases where the resulting -- ByteString may not contain legal JSON: -- --
-- data Friend = Friend
-- { fId :: !Int
-- , fName :: !Text
-- } deriving (Eq, Show)
--
-- instance ToJson Friend where
-- toJson friend = toJson $
-- "id" .= fId friend
-- <> "name" .= fName friend
--
--
-- WARNING: ObjectBuilder does not check uniqueness of
-- object keys. If two keys with the same value are inserted, then the
-- resulting JSON document will be illegal.
data ObjectBuilder
-- | A Value that produces the empty object.
emptyObject :: Value
-- | Create an ObjectBuilder from a key and a value.
(.=) :: ToJson a => Text -> a -> ObjectBuilder
infixr 8 .=
-- | Create an ObjectBuilder from a key and a value. The key is an
-- ASCII-7, unescaped, zero-terminated Addr#.
--
-- WARNING: This function is unsafe. If the key is NOT
-- zero-terminated, then an access violation might result. If the key is
-- not a sequence of unescaped ASCII characters, the resulting JSON
-- document will be illegal.
--
-- This function is provided for maximum performance in the common case
-- that object keys are ASCII-7. It achieves performance by avoiding the
-- CAF for a Text literal and avoiding the need to transcode UTF-16 to
-- UTF-8 and escape.
--
-- To use this function, the calling source file must have the MagicHash
-- extension enabled.
--
--
-- data Friend = Friend
-- { fId :: !Int
-- , fName :: !Text
-- } deriving (Eq, Show)
--
-- instance ToJson Friend where
-- toJson friend = toJson $
-- "id"# .=# fId friend
-- <> "name"# .=# fName friend
--
(.=#) :: ToJson a => Addr# -> a -> ObjectBuilder
infixr 8 .=#
-- | Create an ObjectBuilder from an arbitrary key and value. The key can
-- be any type with a ToJsonString instance.
row :: (ToJsonString k, ToJson v) => k -> v -> ObjectBuilder
infixr 8 `row`
-- | Serialize any Foldable as a JSON array. This is generally
-- slower than directly calling toJson on a list or Vector,
-- but it will convert any Foldable type into an array.
array :: (Foldable t, ToJson a) => t a -> Value
-- | Represents a JSON "null".
nullValue :: Value
-- | Unsafely convert a Utf8Builder into a JSON value. This function
-- does not escape, quote, or decorate the string in any way. This
-- function is unsafe because you can trivially use it to generate
-- illegal JSON.
unsafeValueUtf8Builder :: Utf8Builder () -> Value
-- | Unsafely convert a Utf8Builder into a JSON string. This
-- function does not escape, quote, or decorate the string in any way.
-- This function is unsafe because you can trivially use it to
-- generate illegal JSON.
unsafeStringUtf8Builder :: Utf8Builder () -> JsonString
-- | Deprecated: Use unsafeValueUtf8Builder or unsafeStringUtf8Builder
-- instead
unsafeAppendUtf8Builder :: Utf8Builder () -> Value
-- | Deprecated: Use unsafeValueUtf8Builder or unsafeStringUtf8Builder
-- instead
unsafeAppendBS :: ByteString -> Value
instance GHC.Base.Semigroup Data.BufferBuilder.Json.ObjectBuilder
instance GHC.Base.Monoid Data.BufferBuilder.Json.ObjectBuilder
instance Data.BufferBuilder.Json.ToJson Data.BufferBuilder.Json.ObjectBuilder
instance (Data.BufferBuilder.Json.ToJsonString k, Data.BufferBuilder.Json.ToJson v) => Data.BufferBuilder.Json.ToJson (Data.HashMap.Internal.HashMap k v)
instance Data.BufferBuilder.Json.ToJsonString Data.BufferBuilder.Json.JsonString
instance Data.BufferBuilder.Json.ToJsonString Data.Text.Internal.Text
instance Data.BufferBuilder.Json.ToJson Data.BufferBuilder.Json.JsonString
instance Data.BufferBuilder.Json.ToJson a => Data.BufferBuilder.Json.ToJson [a]
instance Data.BufferBuilder.Json.ToJson a => Data.BufferBuilder.Json.ToJson (Data.Vector.Vector a)
instance (Foreign.Storable.Storable a, Data.BufferBuilder.Json.ToJson a) => Data.BufferBuilder.Json.ToJson (Data.Vector.Storable.Vector a)
instance (Data.Primitive.Types.Prim a, Data.BufferBuilder.Json.ToJson a) => Data.BufferBuilder.Json.ToJson (Data.Vector.Primitive.Vector a)
instance (Data.Vector.Generic.Base.Vector Data.Vector.Unboxed.Base.Vector a, Data.BufferBuilder.Json.ToJson a) => Data.BufferBuilder.Json.ToJson (Data.Vector.Unboxed.Base.Vector a)
instance Data.BufferBuilder.Json.ToJson Data.BufferBuilder.Json.Value
instance Data.BufferBuilder.Json.ToJson GHC.Types.Bool
instance Data.BufferBuilder.Json.ToJson a => Data.BufferBuilder.Json.ToJson (GHC.Maybe.Maybe a)
instance Data.BufferBuilder.Json.ToJson Data.Text.Internal.Text
instance Data.BufferBuilder.Json.ToJson GHC.Types.Double
instance Data.BufferBuilder.Json.ToJson GHC.Types.Int