git.iko.soy/rowtype-yoga/purescript-arraybuffer-builder
on master help sign in
second draft
commitd7f705d1f33787b35f5a077b77e135bc7b27f11b authorJames Brock <jamesbrock@gmail.com> dateSat 25 Jul 2020, 22:33 (6 years ago) parent682eff0
1 file changed+269−157
src/Data/ArrayBuffer/Builder.purs+269−157
src/Data/ArrayBuffer/Builder.purs+269−157
@@ -1,28 +1,33 @@
1−-- | `ArrayBuffer` serialization primitives in the “builder monad” style.
2−-- | An API on top of
3−-- | [`Data.ArrayBuffer.ArrayBuffer`](https://pursuit.purescript.org/packages/purescript-arraybuffer/docs/Data.ArrayBuffer.ArrayBuffer)
4−-- | which provides a `Builder` monoid and a `PutM` monad which are roughly
5−-- | equivalent to the Haskell
1+-- | `ArrayBuffer` serialization in the “builder monoid” style.
2+-- |
3+-- | This module provides a `Builder` monoid and a `PutM` monad which are roughly
4+-- | equivalent to types of the same name in the Haskell
65-- | [`Data.Binary.Put`](https://hackage.haskell.org/package/binary/docs/Data-Binary-Put.html)
76-- | module.
7+-- |
88-- | This library defines no typeclasses.
99-- |
10−-- | See purescript-parsing-dataview for deserialization.
10+-- | ## Deserialization
11+-- |
12+-- | See
13+-- | [__purescript-parsing-dataview__](https://pursuit.purescript.org/packages/purescript-parsing-dataview/)
14+-- | for a way to deserialize from `ArrayBuffer` back to Purescript data.
1115-- |
12−-- | # Usage Examples
16+-- | ## Usage Examples
1317-- |
1418-- | All `ArrayBuffer` building must occur in `Effect`.
1519-- |
16−-- | ## Serialize an integer
20+-- | ### Serialize an integer
1721-- |
18−-- | Create a two-byte `buf :: ArrayBuffer` which contains the number *-2* encoded as big-endian 16-bit two’s-complement.
22+-- | Create a two-byte `arraybuffer :: ArrayBuffer` which contains the number *-2* encoded as big-endian 16-bit two’s-complement.
1923-- | ```purescript
20−-- | buf <- execPut $ putInt32be (-2)
24+-- | do
25+-- | arraybuffer <- execPut $ putInt16be (-2)
2126-- | ```
2227-- |
23−-- | ## Serialize a `String` as UTF8
28+-- | ### Serialize a `String` as UTF8
2429-- |
25−-- | Create a `buf :: ArrayBuffer` which contains a `String` encoded as UTF8 with a length prefix in a
30+-- | A function which encodes a `String` as UTF8 with a length prefix in a
2631-- | way that's compatible with the
2732-- | [`Binary.Put.putStringUtf8`](https://hackage.haskell.org/package/binary/docs/Data-Binary-Put.html#v:putStringUtf8)
2833-- | function from the Haskell
@@ -30,35 +35,37 @@
3035-- | library.
3136-- | Uses [`Data.TextEncoding.encodeUtf8`](https://pursuit.purescript.org/packages/purescript-text-encoding/docs/Data.TextEncoding#v:encodeUtf8).
3237-- | ```purescript
33−-- | buf <- execPut $ do
34−-- | let stringbuf = Data.ArrayBuffer.Typed.buffer $ Data.TextEncoding.encodeUtf8 "Black Lives Matter"
35−-- | -- Put a 64-bit big-endian length prefix for the length of the utf8 string, in bytes.
36−-- | putUint32be 0
37−-- | putUint32be $ Data.Uint.fromInt $ Data.ArrayBuffer.byteLength stringbuf
38−-- | putArrayBuffer stringbuf
38+-- | putStringUtf8 :: forall m. (MonadEffect m) => String -> PutM m Unit
39+-- | putStringUtf8 s = do
40+-- | let stringbuf = Data.ArrayBuffer.Typed.buffer $
41+-- | Data.TextEncoding.encodeUtf8 s
42+-- | -- Put a 64-bit big-endian length prefix for the length of the utf8
43+-- | -- string, in bytes.
44+-- | putUint32be 0
45+-- | putUint32be $ Data.Uint.fromInt $ Data.ArrayBuffer.byteLength stringbuf
46+-- | putArrayBuffer stringbuf
3947-- | ```
4048-- |
41−-- | ## Serialize an `Array Int`
49+-- | ### Serialize an `Array Int`
4250-- |
43−-- | Create a `buf :: ArrayBuffer` which encodes an `Array Int` with a length prefix in a
51+-- | A function which encodes an `Array Int` with a length prefix in a
4452-- | way that's compatible with the
4553-- | [`Binary` instance for `[Int32]`](https://hackage.haskell.org/package/binary/docs/Data-Binary.html#t:Binary)
4654-- | from the Haskell
4755-- | [__binary__](https://hackage.haskell.org/package/binary)
4856-- | library.
4957-- | ```purescript
50−-- | let x = [1,2,3] :: Array Int
51−-- | buf <- execPut $
52−-- | -- Put a 64-bit big-endian length prefix for the length of the array, in bytes.
53−-- | putUint32be 0
54−-- | putUint32be $ Data.Uint.fromInt $ Data.Array.length x
55−-- | traverse_ putInt32be x
58+-- | putArrayInt32 :: forall m. (MonadEffect m) => Array Int -> PutM m Unit
59+-- | putArrayInt32 xs = do
60+-- | -- Put a 64-bit big-endian length prefix for the length of the
61+-- | -- array, in bytes.
62+-- | putUint32be 0
63+-- | putUint32be $ Data.Uint.fromInt $ Data.Array.length xs
64+-- | traverse_ putInt32be xs
5665-- | ```
57−module Build
58−( Builder
59−, PutM
66+module Data.ArrayBuffer.Builder
67+( PutM
6068, Put
61−, execBuilder
6269, execPutM
6370, execPut
6471, putArrayBuffer
@@ -76,106 +83,84 @@ module Build
7683, putFloat32le
7784, putFloat64be
7885, putFloat64le
86+, Builder
87+, (<>>)
88+, execBuilder
89+, singleton
90+, snoc
91+, encodeUint8
92+, encodeInt8
93+, encodeUint16be
94+, encodeUint16le
95+, encodeInt16be
96+, encodeInt16le
97+, encodeUint32be
98+, encodeUint32le
99+, encodeInt32be
100+, encodeInt32le
101+, encodeFloat32be
102+, encodeFloat32le
103+, encodeFloat64be
104+, encodeFloat64le
79105)
80106where
81107
82108import Prelude
83−import Effect (Effect)
84−import Effect.Class (class MonadEffect, liftEffect)
85−import Data.UInt (UInt)
86−import Data.Float32 (Float32)
87−import Data.Maybe (Maybe(..))
88−import Control.Monad.Writer.Trans (WriterT, execWriterT, tell)
89109
90−import Data.ArrayBuffer.Types (ArrayBuffer, Uint8Array)
110+import Control.Monad.Writer.Trans (WriterT, execWriterT, tell)
91111import Data.ArrayBuffer.ArrayBuffer as AB
92112import Data.ArrayBuffer.DataView as DV
93113import Data.ArrayBuffer.Typed as AT
114+import Data.ArrayBuffer.Types (ArrayBuffer, Uint8Array)
115+import Data.Float32 (Float32)
116+import Data.Maybe (Maybe(..))
117+import Data.UInt (UInt)
118+import Effect (Effect)
119+import Effect.Class (class MonadEffect, liftEffect)
94120
95−-- | Writer monad transformer for `Builder`.
121+-- | The `PutM` monad is a Writer transformer monad which
122+-- | [gives us access to do-notation](https://wiki.haskell.org/Do_notation_considered_harmful#Library_design)
123+-- | for building `ArrayBuffer`s.
96124type PutM = WriterT Builder
97125
98126-- | `Put` monad is the non-transformer version of `PutM`.
99127type Put = PutM Effect
100128
101− -- Note: use liftEffect to call the ArrayBuffer Effect functions
102−
103−-- | Builder for `ArrayBuffer`s.
104−-- |
105−-- | It is common to build an `ArrayBuffer` by issuing a sequence of `put`
106−-- | commands in a `Put` monad, and this is the case which the Builder monoid
107−-- | is optimized for.
129+-- | Monoidal builder for `ArrayBuffer`s.
108130-- |
109−-- | # Implementation Details
131+-- | ### Left-associative `<>>` append operator
110132-- |
111−-- | Implements an unbalanced binary tree to get
112−-- | * *O(1)* `snoc` (add element to end)
113−-- | * *O(1)* monoid `append` (concatenation)
114−-- | * *O(n)* `fold`
133+-- | __TL;DR;__ Don't use the `Builder` monoid directly in your code, use the
134+-- | `PutM` monad with do-notation instead.
115135-- |
116−-- | For monoid `append`, what we actually get is *O(k)* where *k* is
117−-- | proportional to the number of `append`s which have already been performed.
118−-- |
119−-- | `append`ing a singleton `Builder` to the right is treated as a `snoc`.
120−-- | Specifically, the special case of `append l r` where `r` has a `Null`
121−-- | left subtree doesn't increase *k*. So as long as we're right-appending
122−-- | singleton `Builder`s, then we still get *O(1)* `append`.
123−-- |
124−-- | `append` is O(n), in the worst case, if the `Builder` has been built
125−-- | entirely by `append`ing. If the Builder has been built mostly by
126−-- | `snoc`ing, then `append` approaches *O(1)*.
127−-- |
128−-- |
129−-- | If a Builder is built entirely by `snoc`ing, it will look like a
130−-- | left-only binary tree, a.k.a. a linked list.
136+-- | The `Builder` monoid in this library is efficient when we `snoc` single
137+-- | items onto the end of it, or when we only `cons` single items to the
138+-- | beginning, but it can be less efficient when we are mixing `cons` and
139+-- | `snoc`.
140+-- | This matters when we're appending three or more `Builder`s
141+-- | in one associative expression, because the
142+-- | `Semigroup` append operator `<>` is right-associative.
131143-- |
144+-- | To solve this, we provide an operator `<>>` for appending `Builders`.
145+-- | `<>>` is exactly the same as `<>`, but left-associative.
146+-- | This only matters when we're chaining together three
147+-- | or more `Builder`s in a single associative expression.
148+-- | Instead of
132149-- | ```
133−-- | ⬤
134−-- | ╱
135−-- | ⬤
136−-- | ╱
137−-- | ⬤
138−-- | ╱
139−-- | ⬤
150+-- | builder₁ <> builder₂ <> builder₃
140151-- | ```
141−-- |
142−-- | If two of these `snoc`-built trees are `append`ed, then the new tree
143−-- | will look like
144−-- |
152+-- | we should always prefer to
153+-- | write
145154-- | ```
146−-- | ⬤
147−-- | ╱ ╲
148−-- | ⬤ ⬤
149−-- | ╱ ╱
150−-- | ⬤ ⬤
151−-- | ╱ ╱
152−-- | ⬤ ⬤
153−-- | ╱
154−-- | ⬤
155+-- | builder₁ <>> builder₂ <>> builder₃
155156-- | ```
157+-- | so that we get the efficient
158+-- | `snoc`ing of `Builder`s.
156159-- |
157−-- | This is all similar to
158−-- | https://hackage.haskell.org/package/bytestring-tree-builder
159−-- | , except that the `Tree` https://hackage.haskell.org/package/bytestring-tree-builder-0.2.7.3/docs/src/ByteString.TreeBuilder.Tree.html#Tree
160−-- | structure in __bytestring-tree-builder__ only carries values in its
161−-- | leaves, and therefore I think has an *O(n log(n))* fold. This `Builder`
162−-- | has an *O(n)* fold.
160+-- | If we build our `ArrayBuffer`s with the `PutM` monad instead of appending by
161+-- | using the `Semigroup` instance of `Builder`, then we always get the efficient
162+-- | `snoc` case.
163163-- |
164−-- | We hope that this implementation is fairly fast, but we
165−-- | haven't chosen this implementation because it's fast, we've chosen
166−-- | this implementation because it's simple. This library provides basic
167−-- | ArrayBuffer serialization with the familar “put monad” style.
168−-- | The only currently existing Purescript libary which does ArrayBuffer
169−-- | serialization is
170−-- | https://pursuit.purescript.org/packages/purescript-arraybuffer-class
171−-- | , and that library is heavily typeclass-based and designed
172−-- | for compatibility with the Haskell cereal library.
173−-- | If someone wants to create a fast Purescript ArrayBuffer serialization
174−-- | library, then they can benchmark against this one to prove that the new
175−-- | one is fast.
176−-- |
177−-- | One relatively cheap and simple performance improvement for this library would be to
178−-- | remove the Null constructor of Builder and instead use Javascript nulls.
179164data Builder
180165 = Node Builder ArrayBuffer Builder
181166 | Null
@@ -192,6 +177,8 @@ instance semigroupBuilder :: Semigroup Builder where
192177 -- to find the rightmost
193178 -- Null
194179
180+infixl 5 append as <>>
181+
195182instance monoidBuilder :: Monoid Builder where
196183 mempty = Null
197184
@@ -201,18 +188,19 @@ foldl f a Null = a
201188foldl f a (Node l x r) = foldl f (f (foldl f a l) x) r
202189
203190-- | Monomorphic foldM copied from
204−-- | https://pursuit.purescript.org/packages/purescript-foldable-traversable/4.1.1/docs/Data.Foldable#v:foldM
191+-- | [`Data.Foldable.foldM`](https://pursuit.purescript.org/packages/purescript-foldable-traversable/4.1.1/docs/Data.Foldable#v:foldM)
205192foldM :: forall b m. (Monad m) => (b -> ArrayBuffer -> m b) -> b -> Builder -> m b
206193foldM f a0 = foldl (\ma b -> ma >>= flip f b) (pure a0)
207194
195+-- | Construct a `Builder` with a single `ArrayBuffer`. *O(1)*
208196singleton :: ArrayBuffer -> Builder
209197singleton buf = Node Null buf Null
210198
211−-- | Add an ArrayBuffer to the end of the Builder.
199+-- | Add an `ArrayBuffer` to the end of the `Builder`. *O(1)*
212200snoc :: Builder -> ArrayBuffer -> Builder
213201snoc bs x = Node bs x Null
214202
215−-- | Build a single `ArrayBuffer` from a `Builder`.
203+-- | Build a single `ArrayBuffer` from a `Builder`. *O(n)*
216204execBuilder :: forall m. (MonadEffect m) => Builder -> m ArrayBuffer
217205execBuilder bldr = do
218206 let buflen = foldl (\b a -> b + AB.byteLength a) 0 bldr
@@ -231,11 +219,11 @@ execBuilder bldr = do
231219 ) 0 bldr
232220 pure buf
233221
234−-- | Monad computation to build an `ArrayBuffer` in any `MonadEffect`.
222+-- | Build an `ArrayBuffer` with do-notation in any `MonadEffect`. *O(n)*
235223execPutM :: forall m. (MonadEffect m) => PutM m Unit -> m ArrayBuffer
236224execPutM = execBuilder <=< execWriterT
237225
238−-- | Monad computation to build an `ArrayBuffer` in the `Effect` monad.
226+-- | Build an `ArrayBuffer` with do-notation. *O(n)*
239227execPut :: Put Unit -> Effect ArrayBuffer
240228execPut = execPutM
241229
@@ -243,100 +231,224 @@ execPut = execPutM
243231putArrayBuffer :: forall m. (MonadEffect m) => ArrayBuffer -> PutM m Unit
244232putArrayBuffer = tell <<< singleton
245233
234+-- | Serialize an 8-bit unsigned integer (byte) into a new `ArrayBuffer`.
235+encodeUint8 :: forall m. (MonadEffect m) => UInt -> m ArrayBuffer
236+encodeUint8 x = do
237+ buf <- liftEffect $ AB.empty 1
238+ _ <- liftEffect $ DV.setUint8 (DV.whole buf) 0 x
239+ pure buf
240+
246241-- | Append an 8-bit unsigned integer (byte) to the builder.
247242putUint8 :: forall m. (MonadEffect m) => UInt -> PutM m Unit
248−putUint8 x = do
243+putUint8 = putArrayBuffer <=< encodeUint8
244+
245+-- | Serialize an 8-bit two’s-complement signed integer (char) into a new `ArrayBuffer`.
246+encodeInt8 :: forall m. (MonadEffect m) => Int -> m ArrayBuffer
247+encodeInt8 x = do
249248 buf <- liftEffect $ AB.empty 1
250− _ <- liftEffect $ DV.setUint8 (DV.whole buf) 0 x
251− tell $ singleton buf
249+ _ <- liftEffect $ DV.setInt8 (DV.whole buf) 0 x
250+ pure buf
252251
253252-- | Append an 8-bit two’s-complement signed integer (char) to the builder.
254253putInt8 :: forall m. (MonadEffect m) => Int -> PutM m Unit
255−putInt8 x = do
256− buf <- liftEffect $ AB.empty 1
257− _ <- liftEffect $ DV.setInt8 (DV.whole buf) 0 x
258− tell $ singleton buf
254+putInt8 = putArrayBuffer <=< encodeInt8
255+
256+-- | Serialize a 16-bit big-endian unsigned integer into a new `ArrayBuffer`.
257+encodeUint16be :: forall m. (MonadEffect m) => UInt -> m ArrayBuffer
258+encodeUint16be x = do
259+ buf <- liftEffect $ AB.empty 2
260+ _ <- liftEffect $ DV.setUint16be (DV.whole buf) 0 x
261+ pure buf
259262
260263-- | Append a 16-bit big-endian unsigned integer to the builder.
261264putUint16be :: forall m. (MonadEffect m) => UInt -> PutM m Unit
262−putUint16be x = do
265+putUint16be = putArrayBuffer <=< encodeUint16be
266+
267+-- | Serialize a 16-bit little-endian unsigned integer into a new `ArrayBuffer`.
268+encodeUint16le :: forall m. (MonadEffect m) => UInt -> m ArrayBuffer
269+encodeUint16le x = do
263270 buf <- liftEffect $ AB.empty 2
264− _ <- liftEffect $ DV.setUint16be (DV.whole buf) 0 x
265− tell $ singleton buf
271+ _ <- liftEffect $ DV.setUint16le (DV.whole buf) 0 x
272+ pure buf
266273
267274-- | Append a 16-bit little-endian unsigned integer to the builder.
268275putUint16le :: forall m. (MonadEffect m) => UInt -> PutM m Unit
269−putUint16le x = do
276+putUint16le = putArrayBuffer <=< encodeUint16le
277+
278+-- | Serialize a 16-bit big-endian two’s-complement signed integer into a new `ArrayBuffer`.
279+encodeInt16be :: forall m. (MonadEffect m) => Int -> m ArrayBuffer
280+encodeInt16be x = do
270281 buf <- liftEffect $ AB.empty 2
271− _ <- liftEffect $ DV.setUint16le (DV.whole buf) 0 x
272− tell $ singleton buf
282+ _ <- liftEffect $ DV.setInt16be (DV.whole buf) 0 x
283+ pure buf
273284
274285-- | Append a 16-bit big-endian two’s-complement signed integer to the builder.
275286putInt16be :: forall m. (MonadEffect m) => Int -> PutM m Unit
276−putInt16be x = do
287+putInt16be = putArrayBuffer <=< encodeInt16be
288+
289+-- | Serialize a 16-bit little-endian two’s-complement signed integer into a new `ArrayBuffer`.
290+encodeInt16le :: forall m. (MonadEffect m) => Int -> m ArrayBuffer
291+encodeInt16le x = do
277292 buf <- liftEffect $ AB.empty 2
278− _ <- liftEffect $ DV.setInt16be (DV.whole buf) 0 x
279− tell $ singleton buf
293+ _ <- liftEffect $ DV.setInt16le (DV.whole buf) 0 x
294+ pure buf
280295
281296-- | Append a 16-bit little-endian two’s-complement signed integer to the builder.
282297putInt16le :: forall m. (MonadEffect m) => Int -> PutM m Unit
283−putInt16le x = do
284− buf <- liftEffect $ AB.empty 2
285− _ <- liftEffect $ DV.setInt16le (DV.whole buf) 0 x
286− tell $ singleton buf
298+putInt16le = putArrayBuffer <=< encodeInt16le
299+
300+-- | Serialize a 32-bit big-endian unsigned integer into a new `ArrayBuffer`.
301+encodeUint32be :: forall m. (MonadEffect m) => UInt -> m ArrayBuffer
302+encodeUint32be x = do
303+ buf <- liftEffect $ AB.empty 4
304+ _ <- liftEffect $ DV.setUint32be (DV.whole buf) 0 x
305+ pure buf
287306
288307-- | Append a 32-bit big-endian unsigned integer to the builder.
289308putUint32be :: forall m. (MonadEffect m) => UInt -> PutM m Unit
290−putUint32be x = do
309+putUint32be = putArrayBuffer <=< encodeUint32be
310+
311+-- | Serialize a 32-bit little-endian unsigned integer into a new `ArrayBuffer`.
312+encodeUint32le :: forall m. (MonadEffect m) => UInt -> m ArrayBuffer
313+encodeUint32le x = do
291314 buf <- liftEffect $ AB.empty 4
292− _ <- liftEffect $ DV.setUint32be (DV.whole buf) 0 x
293− tell $ singleton buf
315+ _ <- liftEffect $ DV.setUint32le (DV.whole buf) 0 x
316+ pure buf
294317
295318-- | Append a 32-bit little-endian unsigned integer to the builder.
296319putUint32le :: forall m. (MonadEffect m) => UInt -> PutM m Unit
297−putUint32le x = do
320+putUint32le = putArrayBuffer <=< encodeUint32le
321+
322+-- | Serialize a 32-bit big-endian two’s-complement signed integer into a new `ArrayBuffer`.
323+encodeInt32be :: forall m. (MonadEffect m) => Int -> m ArrayBuffer
324+encodeInt32be x = do
298325 buf <- liftEffect $ AB.empty 4
299− _ <- liftEffect $ DV.setUint32le (DV.whole buf) 0 x
300− tell $ singleton buf
326+ _ <- liftEffect $ DV.setInt32be (DV.whole buf) 0 x
327+ pure buf
301328
302329-- | Append a 32-bit big-endian two’s-complement signed integer to the builder.
303330putInt32be :: forall m. (MonadEffect m) => Int -> PutM m Unit
304−putInt32be x = do
331+putInt32be = putArrayBuffer <=< encodeInt32be
332+
333+-- | Serialize a 32-bit little-endian two’s-complement signed integer into a new `ArrayBuffer`.
334+encodeInt32le :: forall m. (MonadEffect m) => Int -> m ArrayBuffer
335+encodeInt32le x = do
305336 buf <- liftEffect $ AB.empty 4
306− _ <- liftEffect $ DV.setInt32be (DV.whole buf) 0 x
307− tell $ singleton buf
337+ _ <- liftEffect $ DV.setInt32le (DV.whole buf) 0 x
338+ pure buf
308339
309340-- | Append a 32-bit little-endian two’s-complement signed integer to the builder.
310341putInt32le :: forall m. (MonadEffect m) => Int -> PutM m Unit
311−putInt32le x = do
342+putInt32le = putArrayBuffer <=< encodeInt32le
343+
344+-- | Serialize a 32-bit big-endian IEEE single-precision float into a new `ArrayBuffer`.
345+encodeFloat32be :: forall m. (MonadEffect m) => Float32 -> m ArrayBuffer
346+encodeFloat32be x = do
312347 buf <- liftEffect $ AB.empty 4
313− _ <- liftEffect $ DV.setInt32le (DV.whole buf) 0 x
314− tell $ singleton buf
348+ _ <- liftEffect $ DV.setFloat32be (DV.whole buf) 0 x
349+ pure buf
315350
316351-- | Append a 32-bit big-endian IEEE single-precision float to the builder.
317352putFloat32be :: forall m. (MonadEffect m) => Float32 -> PutM m Unit
318−putFloat32be x = do
353+putFloat32be = putArrayBuffer <=< encodeFloat32be
354+
355+-- | Serialize a 32-bit little-endian IEEE single-precision float into a new `ArrayBuffer`.
356+encodeFloat32le :: forall m. (MonadEffect m) => Float32 -> m ArrayBuffer
357+encodeFloat32le x = do
319358 buf <- liftEffect $ AB.empty 4
320− _ <- liftEffect $ DV.setFloat32be (DV.whole buf) 0 x
321− tell $ singleton buf
359+ _ <- liftEffect $ DV.setFloat32le (DV.whole buf) 0 x
360+ pure buf
322361
323362-- | Append a 32-bit little-endian IEEE single-precision float to the builder.
324363putFloat32le :: forall m. (MonadEffect m) => Float32 -> PutM m Unit
325−putFloat32le x = do
326− buf <- liftEffect $ AB.empty 4
327− _ <- liftEffect $ DV.setFloat32le (DV.whole buf) 0 x
328− tell $ singleton buf
364+putFloat32le = putArrayBuffer <=< encodeFloat32le
365+
366+-- | Serialize a 64-bit big-endian IEEE double-precision float into a new `ArrayBuffer`.
367+encodeFloat64be :: forall m. (MonadEffect m) => Number -> m ArrayBuffer
368+encodeFloat64be x = do
369+ buf <- liftEffect $ AB.empty 8
370+ _ <- liftEffect $ DV.setFloat64be (DV.whole buf) 0 x
371+ pure buf
329372
330373-- | Append a 64-bit big-endian IEEE double-precision float to the builder.
331374putFloat64be :: forall m. (MonadEffect m) => Number -> PutM m Unit
332−putFloat64be x = do
375+putFloat64be = putArrayBuffer <=< encodeFloat64be
376+
377+-- | Serialize a 64-bit little-endian IEEE double-precision float into a new `ArrayBuffer`.
378+encodeFloat64le :: forall m. (MonadEffect m) => Number -> m ArrayBuffer
379+encodeFloat64le x = do
333380 buf <- liftEffect $ AB.empty 8
334− _ <- liftEffect $ DV.setFloat64be (DV.whole buf) 0 x
335− tell $ singleton buf
381+ _ <- liftEffect $ DV.setFloat64le (DV.whole buf) 0 x
382+ pure buf
336383
337384-- | Append a 64-bit little-endian IEEE double-precision float to the builder.
338385putFloat64le :: forall m. (MonadEffect m) => Number -> PutM m Unit
339−putFloat64le x = do
340− buf <- liftEffect $ AB.empty 8
341− _ <- liftEffect $ DV.setFloat64le (DV.whole buf) 0 x
342− tell $ singleton buf
386+putFloat64le = putArrayBuffer <=< encodeFloat64le
387+
388+
389+-- | ## Implementation Details
390+-- |
391+-- | We want our `Builder` to be a data structure with
392+-- | * *O(1)* monoid append
393+-- | * *O(n)* fold
394+-- |
395+-- | Our `Builder` monoid implementation is an unbalanced binary tree.
396+-- |
397+-- | For monoid `append`, what we actually get is *O(1)* when either the
398+-- | left or right tree is a singleton. If that's not true, then in the
399+-- | unlikely worst case `append` might be *O(n)*.
400+-- |
401+-- | `Builder` is optimized what we consider to be normal usage, that is,
402+-- | `snoc`ing singleton elements to the end of the `Builder`.
403+-- |
404+-- | If a Builder is built entirely by `snoc`ing, it will look like a
405+-- | left-only binary tree, a.k.a. a linked list.
406+-- |
407+-- | ```
408+-- | ④
409+-- | ╱
410+-- | ③
411+-- | ╱
412+-- | ②
413+-- | ╱
414+-- | ①
415+-- | ```
416+-- |
417+-- | If two of these `snoc`-built trees are `append`ed, then the new tree
418+-- | will look like
419+-- |
420+-- | ```
421+-- | ④
422+-- | ╱ ╲
423+-- | ③ ⑧
424+-- | ╱ ╱
425+-- | ② ⑦
426+-- | ╱ ╱
427+-- | ① ⑥
428+-- | ╱
429+-- | ⑤
430+-- | ```
431+-- |
432+-- | This is all similar to
433+-- | [__bytestring-tree-builder__](https://hackage.haskell.org/package/bytestring-tree-builder)
434+-- | , except that the
435+-- | [`Tree`](https://hackage.haskell.org/package/bytestring-tree-builder-0.2.7.3/docs/src/ByteString.TreeBuilder.Tree.html#Tree)
436+-- | structure in __bytestring-tree-builder__ only carries values in its
437+-- | leaves, which is how it achieves *O(1)* appending, at the cost of
438+-- | a higher coefficient time factor on the fold.
439+-- |
440+-- | We hope that this implementation is fairly fast, but we
441+-- | haven't chosen this implementation because it's fast, we've chosen
442+-- | this implementation because it's simple. This library provides basic
443+-- | ArrayBuffer serialization with the familar “put monad” style.
444+-- | The only currently existing Purescript libary which does ArrayBuffer
445+-- | serialization is
446+-- | https://pursuit.purescript.org/packages/purescript-arraybuffer-class
447+-- | , and that library is heavily typeclass-based and designed
448+-- | for compatibility with the Haskell cereal library.
449+-- | If someone wants to create a fast Purescript ArrayBuffer serialization
450+-- | library, then they can benchmark against this one to prove that the new
451+-- | one is fast.
452+-- |
453+-- | One relatively cheap and simple performance improvement for this library would be to
454+-- | remove the Null constructor of `Builder` and instead use Javascript nulls.