|
56 | 56 | -- during an unsafe call, even in the bytecode interpreter, and further |
57 | 57 | -- guarantees that unsafe calls will be performed in the calling thread. Making |
58 | 58 | -- 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 | +-- |
60 | 98 | -- == Serialization using Unbox |
61 | 99 | -- |
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. |
68 | 102 | -- |
69 | 103 | -- Writing a data type to an array using the array creation routines in |
70 | 104 | -- "Streamly.Data.Array" or "Streamly.Data.MutArray" (e.g. @writeN@ or |
|
74 | 108 | -- |
75 | 109 | -- == Serialization using Serialize |
76 | 110 | -- |
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'. |
82 | 114 | -- |
83 | 115 | -- See 'Streamly.Internal.Data.Array.pinnedSerialize' and |
84 | 116 | -- 'Streamly.Internal.Data.Array.deserialize' for 'Array' type based |
|
0 commit comments