Skip to content

Commit 87f64d7

Browse files
Document the differences between Unbox and Serialize
Add a section to Streamly.Data.MutByteArray on choosing between Unbox and Serialize: the purpose, the supported types, bounds checks, word size, deterministic bytes, schema options and byte order. Remove the claim that Serialize is a superset of Unbox. The Unbox and Serialize class docs point to this section. A blank line ended the module header doc, the sections after the FFI section were not rendered by haddock. Join them to the module header.
1 parent f37eb87 commit 87f64d7

3 files changed

Lines changed: 50 additions & 12 deletions

File tree

‎core/src/Streamly/Data/MutByteArray.hs‎

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -56,15 +56,49 @@
5656
-- during an unsafe call, even in the bytecode interpreter, and further
5757
-- guarantees that unsafe calls will be performed in the calling thread. Making
5858
-- it safe to pass heap-allocated objects to unsafe functions.
59-
59+
--
60+
-- == Choosing between Unbox and Serialize
61+
--
62+
-- 'Unbox' stores Haskell values in memory in unboxed form, for example as the
63+
-- elements of the unboxed arrays in "Streamly.Data.Array" and
64+
-- "Streamly.Data.MutArray". It is designed for speed, for data written and
65+
-- read by the same program on the same machine:
66+
--
67+
-- * Only non-recursive types of a fixed size are supported, the size is
68+
-- determined by the type, not by the value.
69+
-- * 'peekAt' and 'pokeAt' do not check the bounds of the array, the caller
70+
-- checks them.
71+
-- * 'Int' and 'Word' use the native machine word size, e.g. 4 bytes on the
72+
-- JavaScript backend and 8 bytes on 64-bit platforms.
73+
-- * Bytes that carry no information are not written, see the section
74+
-- Comparing serialized values below.
75+
--
76+
-- 'Serialize' converts Haskell values to bytes for storing in files or
77+
-- sending to another program or machine. It may be slower than 'Unbox':
78+
--
79+
-- * Recursive and variable length types are supported, e.g. lists. The
80+
-- length of a variable length value is stored with the value.
81+
-- * 'deserializeAt' checks the bounds of the input buffer and fails on
82+
-- invalid lengths.
83+
-- * 'Int' and 'Word' are serialized as 8 bytes on all platforms.
84+
-- * Every byte is written, the same value is always serialized to the same
85+
-- bytes, except for 'Streamly.Data.Array.Array', see the section Comparing
86+
-- serialized values below.
87+
-- * Constructors and record fields can optionally be encoded by name, so
88+
-- that constructors or record fields can be reordered without changing the
89+
-- meaning of serialized data. This is experimental, see the section Fast
90+
-- serialization with schema above.
91+
--
92+
-- Both use the byte order of the host machine. Data written on a machine with
93+
-- one byte order cannot be read on a machine with the other.
94+
--
95+
-- 'Unbox' can be used for storing or transferring data if both ends use the
96+
-- same byte order and word size, and the unwritten bytes are acceptable.
97+
--
6098
-- == Serialization using Unbox
6199
--
62-
-- The 'Unbox' type class is simple and used to serialize non-recursive fixed
63-
-- size data types. This type class is primarily used to implement unboxed
64-
-- arrays. Unboxed arrays are just a sequence of serialized fixed length
65-
-- Haskell data types. Instances of this type class can be derived using
66-
-- 'Generic' or template haskell based deriving functions provided in this
67-
-- module.
100+
-- Instances of 'Unbox' can be derived using 'Generic' or the Template Haskell
101+
-- function 'deriveUnbox' provided in this module.
68102
--
69103
-- Writing a data type to an array using the array creation routines in
70104
-- "Streamly.Data.Array" or "Streamly.Data.MutArray" (e.g. @writeN@ or
@@ -74,11 +108,9 @@
74108
--
75109
-- == Serialization using Serialize
76110
--
77-
-- The 'Serialize' type class is a superset of the 'Unbox' type class, it can
78-
-- serialize variable length data types as well e.g. Haskell lists. Use
79-
-- 'deriveSerialize' to derive the instances of the type class automatically
80-
-- and then use the type class methods to serialize and deserialize to and from
81-
-- a 'MutByteArray'.
111+
-- Use 'deriveSerialize' to derive the instances of the type class
112+
-- automatically and then use the type class methods to serialize and
113+
-- deserialize to and from a 'MutByteArray'.
82114
--
83115
-- See 'Streamly.Internal.Data.Array.pinnedSerialize' and
84116
-- 'Streamly.Internal.Data.Array.deserialize' for 'Array' type based

‎core/src/Streamly/Internal/Data/Serialize/Type.hs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,9 @@ import Prelude hiding (Foldable(..))
9797
-- deserialization of general Haskell data types to and from their byte stream
9898
-- representation.
9999
--
100+
-- See the module "Streamly.Data.MutByteArray" for the differences between
101+
-- 'Unbox' and 'Serialize' and for choosing one of them.
102+
--
100103
-- Unlike 'Unbox', 'Serialize' uses variable length encoding, therefore, it can
101104
-- serialize recursive and variable length data types like lists, or variable
102105
-- length sum types where the length of the value may vary depending on a

‎core/src/Streamly/Internal/Data/Unbox.hs‎

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -183,6 +183,9 @@ import qualified Streamly.Internal.Data.MutByteArray.Type as MutByteArray
183183
-- and deserialization (boxing) of fixed-length, non-recursive Haskell data
184184
-- types to and from their byte stream representation.
185185
--
186+
-- See the module "Streamly.Data.MutByteArray" for the differences between
187+
-- 'Unbox' and 'Serialize' and for choosing one of them.
188+
--
186189
-- Unbox uses fixed size encoding, therefore, size is independent of the value,
187190
-- it must be determined solely by the type. This restriction makes types with
188191
-- 'Unbox' instances suitable for storing in arrays. Note that sum types may

0 commit comments

Comments
 (0)