@@ -5,152 +5,402 @@
55-- | Writing to an `ArrayBuffer` is an `Effect`ful activity, so most
66-- | functions in this module must be run in a `MonadEffect` context.
77-- |
8−-- | For other operations for working with `ArrayBuffer`, see
8+-- | For operations for working with `ArrayBuffer`, see
99-- | module
1010-- | [`Data.ArrayBuffer.ArrayBuffer`](https://pursuit.purescript.org/packages/purescript-arraybuffer/docs/Data.ArrayBuffer.ArrayBuffer)
11−-- | in package __arraybuffer__.
11+-- | in package __purescript-arraybuffer__.
1212module Data.ArrayBuffer.Builder
13− ( PutM
14− , Put
15− , execPutM
16− , execPut
17− , subBuilder
18− , putArrayBuffer
19− , putDataView
20− , putDataBuff
21− , putUint8
22− , putInt8
23− , putUint16be
24− , putUint16le
25− , putInt16be
26− , putInt16le
27− , putUint32be
28− , putUint32le
29− , putInt32be
30− , putInt32le
31− , putFloat32be
32− , putFloat32le
33− , putFloat64be
34− , putFloat64le
35− , module Data.ArrayBuffer.Builder.Internal
36− ) where
13+( PutM
14+, Put
15+, execPutM
16+, execPut
17+, putArrayBuffer
18+, putUint8
19+, putInt8
20+, putUint16be
21+, putUint16le
22+, putInt16be
23+, putInt16le
24+, putUint32be
25+, putUint32le
26+, putInt32be
27+, putInt32le
28+, putFloat32be
29+, putFloat32le
30+, putFloat64be
31+, putFloat64le
32+, Builder
33+, (<>>)
34+, execBuilder
35+, singleton
36+, snoc
37+, encodeUint8
38+, encodeInt8
39+, encodeUint16be
40+, encodeUint16le
41+, encodeInt16be
42+, encodeInt16le
43+, encodeUint32be
44+, encodeUint32le
45+, encodeInt32be
46+, encodeInt32le
47+, encodeFloat32be
48+, encodeFloat32le
49+, encodeFloat64be
50+, encodeFloat64le
51+)
52+where
3753
3854import Prelude
3955
40−import Control.Monad.Rec.Class (class MonadRec)
41−import Control.Monad.Writer.Trans (WriterT, execWriterT, tell, lift)
42−import Data.ArrayBuffer.Builder.Internal (Builder, DataBuff(..), length, toView, (<>>))
43−import Data.ArrayBuffer.Builder.Internal as Internal
44−import Data.ArrayBuffer.Types (ArrayBuffer, DataView)
56+import Control.Monad.Writer.Trans (WriterT, execWriterT, tell)
57+import Data.ArrayBuffer.ArrayBuffer as AB
58+import Data.ArrayBuffer.DataView as DV
59+import Data.ArrayBuffer.Typed as AT
60+import Data.ArrayBuffer.Types (ArrayBuffer, Uint8Array)
4561import Data.Float32 (Float32)
62+import Data.Maybe (Maybe(..))
4663import Data.UInt (UInt)
4764import Effect (Effect)
48−import Effect.Class (class MonadEffect)
65+import Effect.Class (class MonadEffect, liftEffect)
4966
50−-- | The `PutM` monad is a `WriterT Builder` transformer monad which
51−-- | gives us do-notation for the `Builder` monoid. The base monad must be
52−-- | a `MonadEffect`.
53−-- |
54−-- | To append `Builder`s in this monad call `tell`, or any of the `put*`
55−-- | functions in this module.
67+-- | The `PutM` monad is a Writer transformer monad which
68+-- | [gives us access to do-notation](https://wiki.haskell.org/Do_notation_considered_harmful#Library_design)
69+-- | for building `ArrayBuffer`s.
5670type PutM = WriterT Builder
5771
58−-- | The `PutM` type reified to `Effect`, in other words `WriterT Builder Effect`.
72+-- | `Put` monad is the non-transformer version of `PutM`.
5973type Put = PutM Effect
6074
61−-- | Build an `ArrayBuffer` with do-notation in any `MonadEffect`. *O(n)* Stack-safe.
62−execPutM :: forall m. MonadEffect m => MonadRec m => PutM m Unit -> m ArrayBuffer
63−execPutM = Internal.execBuilder <=< execWriterT
64−
65−-- | Build an `ArrayBuffer` with do-notation in `Effect`. *O(n)* Stack-safe.
66−execPut :: Put Unit -> Effect ArrayBuffer
67−execPut = execPutM
68−
69−-- | Build up a sub-`Builder` without `tell`ing it to the `Writer` yet.
75+-- | Monoidal builder for `ArrayBuffer`s.
7076-- |
71−-- | One case where we might want to call a `subBuilder` is when
72−-- | serializing length-prefixed messages in some protocol. In that case,
73−-- | we must serialize the message first, calculate the message length,
74−-- | append the message length, and then append the message.
77+-- | ### Left-associative `<>>` append operator
7578-- |
76−-- | In a `PutM` monad do-block, we can
79+-- | __TL;DR__ You probably don't want to use the `Builder` monoid directly
80+-- | in your code, it’s better to use the `PutM` monad with do-notation instead.
7781-- |
78−-- | ```
79−-- | do
80−-- | messageBuilder <- subBuilder $ do
81−-- | putField1
82−-- | putField2
82+-- | The `Builder` monoid in this library is efficient when we `snoc` single
83+-- | items onto the end of it, or when we only `cons` single items to the
84+-- | beginning, but it can be less efficient when we are mixing `cons` and
85+-- | `snoc`.
86+-- | This matters when we're appending three or more `Builder`s
87+-- | in one associative expression, because the
88+-- | `Semigroup` append operator `<>` is right-associative.
8389-- |
84−-- | putInt32be $ length messageBuilder
85−-- | tell messageBuilder
90+-- | To solve this, we provide an operator `<>>` for appending `Builders`.
91+-- | `<>>` is exactly the same as `<>`, but left-associative.
92+-- | This only matters when we're chaining together three
93+-- | or more `Builder`s in a single associative expression.
94+-- | Instead of
95+-- | ```
96+-- | builder₁ <> builder₂ <> builder₃
97+-- | ```
98+-- | we should always prefer to
99+-- | write
86100-- | ```
87−subBuilder :: forall m. Monad m => PutM m Unit -> PutM m Builder
88−subBuilder = lift <<< execWriterT
101+-- | builder₁ <>> builder₂ <>> builder₃
102+-- | ```
103+-- | so that we get the efficient
104+-- | `snoc`ing of `Builder`s.
105+-- |
106+-- | If we build our `ArrayBuffer`s with the `PutM` monad instead of appending by
107+-- | using the `Semigroup` instance of `Builder`, then we always get the efficient
108+-- | `snoc` case.
109+-- |
110+data Builder
111+ = Node Builder ArrayBuffer Builder
112+ | Null
89113
90−-- | Append an `ArrayBuffer` to the builder.
91−putArrayBuffer :: forall m. Monad m => ArrayBuffer -> PutM m Unit
92−putArrayBuffer = tell <<< Internal.singleton <<< Internal.Buff
114+instance semigroupBuilder :: Semigroup Builder where
115+ append Null Null = Null
116+ append l Null = l
117+ append Null r = r
118+ append l (Node Null rx rr) = Node l rx rr -- this is the snoc case
119+ append (Node ll lx Null) r = Node ll lx r -- found the rightmost Null
120+ -- of the left tree
121+ append (Node ll lx lr) r = Node ll lx $ append lr r -- search down the right
122+ -- side of the left tree
123+ -- to find the rightmost
124+ -- Null
125+
126+infixl 5 append as <>>
127+
128+instance monoidBuilder :: Monoid Builder where
129+ mempty = Null
130+
131+-- | `Builder` is not an instance of `Foldable` because `Builder` is monomorphic.
132+foldl :: forall b. (b -> ArrayBuffer -> b) -> b -> Builder -> b
133+foldl f a Null = a
134+foldl f a (Node l x r) = foldl f (f (foldl f a l) x) r
93135
94−-- | Append a `DataView` to the builder.
95−putDataView :: forall m. Monad m => DataView -> PutM m Unit
96−putDataView = tell <<< Internal.singleton <<< Internal.View
136+-- | Monomorphic foldM copied from
137+-- | [`Data.Foldable.foldM`](https://pursuit.purescript.org/packages/purescript-foldable-traversable/4.1.1/docs/Data.Foldable#v:foldM)
138+foldM :: forall b m. (Monad m) => (b -> ArrayBuffer -> m b) -> b -> Builder -> m b
139+foldM f a0 = foldl (\ma b -> ma >>= flip f b) (pure a0)
97140
98−-- | Append either an `ArrayBuffer` or a `DataView` to the builder.
99−putDataBuff :: forall m. Monad m => DataBuff -> PutM m Unit
100−putDataBuff = tell <<< Internal.singleton
141+-- | Construct a `Builder` with a single `ArrayBuffer`. *O(1)*
142+singleton :: ArrayBuffer -> Builder
143+singleton buf = Node Null buf Null
144+
145+-- | Add an `ArrayBuffer` to the end of the `Builder`. *O(1)*
146+snoc :: Builder -> ArrayBuffer -> Builder
147+snoc bs x = Node bs x Null
148+
149+-- | Build a single `ArrayBuffer` from a `Builder`. *O(n)*
150+execBuilder :: forall m. (MonadEffect m) => Builder -> m ArrayBuffer
151+execBuilder bldr = do
152+ let buflen = foldl (\b a -> b + AB.byteLength a) 0 bldr
153+ -- Allocate the final ArrayBuffer
154+ buf <- liftEffect $ AB.empty buflen
155+ -- Then copy each ArrayBuffer into the final ArrayBuffer.
156+ -- To memcpy from one ArrayBuffer to another, apparently we have to first view
157+ -- both ArrayBuffers as ArrayView (Typed Array).
158+ newview <- liftEffect (AT.whole buf :: Effect Uint8Array)
159+ _ <- foldM
160+ ( \offset a -> do
161+ aview <- liftEffect (AT.whole a :: Effect Uint8Array)
162+ _ <- liftEffect $ AT.setTyped newview (Just offset) aview
163+ pure $ offset + AB.byteLength a
164+ ) 0 bldr
165+ pure buf
166+
167+-- | Build an `ArrayBuffer` with do-notation in any `MonadEffect`. *O(n)*
168+execPutM :: forall m. (MonadEffect m) => PutM m Unit -> m ArrayBuffer
169+execPutM = execBuilder <=< execWriterT
170+
171+-- | Build an `ArrayBuffer` with do-notation. *O(n)*
172+execPut :: Put Unit -> Effect ArrayBuffer
173+execPut = execPutM
174+
175+-- | Append an `ArrayBuffer` to the builder.
176+putArrayBuffer :: forall m. (MonadEffect m) => ArrayBuffer -> PutM m Unit
177+putArrayBuffer = tell <<< singleton
178+
179+-- | Serialize an 8-bit unsigned integer (byte) into a new `ArrayBuffer`.
180+encodeUint8 :: forall m. (MonadEffect m) => UInt -> m ArrayBuffer
181+encodeUint8 x = do
182+ buf <- liftEffect $ AB.empty 1
183+ _ <- liftEffect $ DV.setUint8 (DV.whole buf) 0 x
184+ pure buf
101185
102186-- | Append an 8-bit unsigned integer (byte) to the builder.
103−putUint8 :: forall m. MonadEffect m => UInt -> PutM m Unit
104−putUint8 = putArrayBuffer <=< Internal.encodeUint8
187+putUint8 :: forall m. (MonadEffect m) => UInt -> PutM m Unit
188+putUint8 = putArrayBuffer <=< encodeUint8
189+
190+-- | Serialize an 8-bit two’s-complement signed integer (char) into a new `ArrayBuffer`.
191+encodeInt8 :: forall m. (MonadEffect m) => Int -> m ArrayBuffer
192+encodeInt8 x = do
193+ buf <- liftEffect $ AB.empty 1
194+ _ <- liftEffect $ DV.setInt8 (DV.whole buf) 0 x
195+ pure buf
105196
106197-- | Append an 8-bit two’s-complement signed integer (char) to the builder.
107−putInt8 :: forall m. MonadEffect m => Int -> PutM m Unit
108−putInt8 = putArrayBuffer <=< Internal.encodeInt8
198+putInt8 :: forall m. (MonadEffect m) => Int -> PutM m Unit
199+putInt8 = putArrayBuffer <=< encodeInt8
200+
201+-- | Serialize a 16-bit big-endian unsigned integer into a new `ArrayBuffer`.
202+encodeUint16be :: forall m. (MonadEffect m) => UInt -> m ArrayBuffer
203+encodeUint16be x = do
204+ buf <- liftEffect $ AB.empty 2
205+ _ <- liftEffect $ DV.setUint16be (DV.whole buf) 0 x
206+ pure buf
109207
110208-- | Append a 16-bit big-endian unsigned integer to the builder.
111−putUint16be :: forall m. MonadEffect m => UInt -> PutM m Unit
112−putUint16be = putArrayBuffer <=< Internal.encodeUint16be
209+putUint16be :: forall m. (MonadEffect m) => UInt -> PutM m Unit
210+putUint16be = putArrayBuffer <=< encodeUint16be
211+
212+-- | Serialize a 16-bit little-endian unsigned integer into a new `ArrayBuffer`.
213+encodeUint16le :: forall m. (MonadEffect m) => UInt -> m ArrayBuffer
214+encodeUint16le x = do
215+ buf <- liftEffect $ AB.empty 2
216+ _ <- liftEffect $ DV.setUint16le (DV.whole buf) 0 x
217+ pure buf
113218
114219-- | Append a 16-bit little-endian unsigned integer to the builder.
115−putUint16le :: forall m. MonadEffect m => UInt -> PutM m Unit
116−putUint16le = putArrayBuffer <=< Internal.encodeUint16le
220+putUint16le :: forall m. (MonadEffect m) => UInt -> PutM m Unit
221+putUint16le = putArrayBuffer <=< encodeUint16le
222+
223+-- | Serialize a 16-bit big-endian two’s-complement signed integer into a new `ArrayBuffer`.
224+encodeInt16be :: forall m. (MonadEffect m) => Int -> m ArrayBuffer
225+encodeInt16be x = do
226+ buf <- liftEffect $ AB.empty 2
227+ _ <- liftEffect $ DV.setInt16be (DV.whole buf) 0 x
228+ pure buf
117229
118230-- | Append a 16-bit big-endian two’s-complement signed integer to the builder.
119−putInt16be :: forall m. MonadEffect m => Int -> PutM m Unit
120−putInt16be = putArrayBuffer <=< Internal.encodeInt16be
231+putInt16be :: forall m. (MonadEffect m) => Int -> PutM m Unit
232+putInt16be = putArrayBuffer <=< encodeInt16be
233+
234+-- | Serialize a 16-bit little-endian two’s-complement signed integer into a new `ArrayBuffer`.
235+encodeInt16le :: forall m. (MonadEffect m) => Int -> m ArrayBuffer
236+encodeInt16le x = do
237+ buf <- liftEffect $ AB.empty 2
238+ _ <- liftEffect $ DV.setInt16le (DV.whole buf) 0 x
239+ pure buf
121240
122241-- | Append a 16-bit little-endian two’s-complement signed integer to the builder.
123−putInt16le :: forall m. MonadEffect m => Int -> PutM m Unit
124−putInt16le = putArrayBuffer <=< Internal.encodeInt16le
242+putInt16le :: forall m. (MonadEffect m) => Int -> PutM m Unit
243+putInt16le = putArrayBuffer <=< encodeInt16le
244+
245+-- | Serialize a 32-bit big-endian unsigned integer into a new `ArrayBuffer`.
246+encodeUint32be :: forall m. (MonadEffect m) => UInt -> m ArrayBuffer
247+encodeUint32be x = do
248+ buf <- liftEffect $ AB.empty 4
249+ _ <- liftEffect $ DV.setUint32be (DV.whole buf) 0 x
250+ pure buf
125251
126252-- | Append a 32-bit big-endian unsigned integer to the builder.
127−putUint32be :: forall m. MonadEffect m => UInt -> PutM m Unit
128−putUint32be = putArrayBuffer <=< Internal.encodeUint32be
253+putUint32be :: forall m. (MonadEffect m) => UInt -> PutM m Unit
254+putUint32be = putArrayBuffer <=< encodeUint32be
255+
256+-- | Serialize a 32-bit little-endian unsigned integer into a new `ArrayBuffer`.
257+encodeUint32le :: forall m. (MonadEffect m) => UInt -> m ArrayBuffer
258+encodeUint32le x = do
259+ buf <- liftEffect $ AB.empty 4
260+ _ <- liftEffect $ DV.setUint32le (DV.whole buf) 0 x
261+ pure buf
129262
130263-- | Append a 32-bit little-endian unsigned integer to the builder.
131−putUint32le :: forall m. MonadEffect m => UInt -> PutM m Unit
132−putUint32le = putArrayBuffer <=< Internal.encodeUint32le
264+putUint32le :: forall m. (MonadEffect m) => UInt -> PutM m Unit
265+putUint32le = putArrayBuffer <=< encodeUint32le
266+
267+-- | Serialize a 32-bit big-endian two’s-complement signed integer into a new `ArrayBuffer`.
268+encodeInt32be :: forall m. (MonadEffect m) => Int -> m ArrayBuffer
269+encodeInt32be x = do
270+ buf <- liftEffect $ AB.empty 4
271+ _ <- liftEffect $ DV.setInt32be (DV.whole buf) 0 x
272+ pure buf
133273
134274-- | Append a 32-bit big-endian two’s-complement signed integer to the builder.
135−putInt32be :: forall m. MonadEffect m => Int -> PutM m Unit
136−putInt32be = putArrayBuffer <=< Internal.encodeInt32be
275+putInt32be :: forall m. (MonadEffect m) => Int -> PutM m Unit
276+putInt32be = putArrayBuffer <=< encodeInt32be
277+
278+-- | Serialize a 32-bit little-endian two’s-complement signed integer into a new `ArrayBuffer`.
279+encodeInt32le :: forall m. (MonadEffect m) => Int -> m ArrayBuffer
280+encodeInt32le x = do
281+ buf <- liftEffect $ AB.empty 4
282+ _ <- liftEffect $ DV.setInt32le (DV.whole buf) 0 x
283+ pure buf
137284
138285-- | Append a 32-bit little-endian two’s-complement signed integer to the builder.
139−putInt32le :: forall m. MonadEffect m => Int -> PutM m Unit
140−putInt32le = putArrayBuffer <=< Internal.encodeInt32le
286+putInt32le :: forall m. (MonadEffect m) => Int -> PutM m Unit
287+putInt32le = putArrayBuffer <=< encodeInt32le
288+
289+-- | Serialize a 32-bit big-endian IEEE single-precision float into a new `ArrayBuffer`.
290+encodeFloat32be :: forall m. (MonadEffect m) => Float32 -> m ArrayBuffer
291+encodeFloat32be x = do
292+ buf <- liftEffect $ AB.empty 4
293+ _ <- liftEffect $ DV.setFloat32be (DV.whole buf) 0 x
294+ pure buf
141295
142296-- | Append a 32-bit big-endian IEEE single-precision float to the builder.
143−putFloat32be :: forall m. MonadEffect m => Float32 -> PutM m Unit
144−putFloat32be = putArrayBuffer <=< Internal.encodeFloat32be
297+putFloat32be :: forall m. (MonadEffect m) => Float32 -> PutM m Unit
298+putFloat32be = putArrayBuffer <=< encodeFloat32be
299+
300+-- | Serialize a 32-bit little-endian IEEE single-precision float into a new `ArrayBuffer`.
301+encodeFloat32le :: forall m. (MonadEffect m) => Float32 -> m ArrayBuffer
302+encodeFloat32le x = do
303+ buf <- liftEffect $ AB.empty 4
304+ _ <- liftEffect $ DV.setFloat32le (DV.whole buf) 0 x
305+ pure buf
145306
146307-- | Append a 32-bit little-endian IEEE single-precision float to the builder.
147−putFloat32le :: forall m. MonadEffect m => Float32 -> PutM m Unit
148−putFloat32le = putArrayBuffer <=< Internal.encodeFloat32le
308+putFloat32le :: forall m. (MonadEffect m) => Float32 -> PutM m Unit
309+putFloat32le = putArrayBuffer <=< encodeFloat32le
310+
311+-- | Serialize a 64-bit big-endian IEEE double-precision float into a new `ArrayBuffer`.
312+encodeFloat64be :: forall m. (MonadEffect m) => Number -> m ArrayBuffer
313+encodeFloat64be x = do
314+ buf <- liftEffect $ AB.empty 8
315+ _ <- liftEffect $ DV.setFloat64be (DV.whole buf) 0 x
316+ pure buf
149317
150318-- | Append a 64-bit big-endian IEEE double-precision float to the builder.
151−putFloat64be :: forall m. MonadEffect m => Number -> PutM m Unit
152−putFloat64be = putArrayBuffer <=< Internal.encodeFloat64be
319+putFloat64be :: forall m. (MonadEffect m) => Number -> PutM m Unit
320+putFloat64be = putArrayBuffer <=< encodeFloat64be
321+
322+-- | Serialize a 64-bit little-endian IEEE double-precision float into a new `ArrayBuffer`.
323+encodeFloat64le :: forall m. (MonadEffect m) => Number -> m ArrayBuffer
324+encodeFloat64le x = do
325+ buf <- liftEffect $ AB.empty 8
326+ _ <- liftEffect $ DV.setFloat64le (DV.whole buf) 0 x
327+ pure buf
153328
154329-- | Append a 64-bit little-endian IEEE double-precision float to the builder.
155−putFloat64le :: forall m. MonadEffect m => Number -> PutM m Unit
156−putFloat64le = putArrayBuffer <=< Internal.encodeFloat64le
330+putFloat64le :: forall m. (MonadEffect m) => Number -> PutM m Unit
331+putFloat64le = putArrayBuffer <=< encodeFloat64le
332+
333+
334+-- ## Implementation Details
335+--
336+-- We want our `Builder` to be a data structure with
337+-- * *O(1)* monoid append
338+-- * *O(n)* fold
339+--
340+-- Our `Builder` implementation is an unbalanced binary tree.
341+--
342+-- For monoid `append`, what we actually get is *O(1)* when either the
343+-- left or right tree is a singleton. If that's not true, then in the
344+-- unlikely worst case `append` might be *O(n)*.
345+--
346+-- `Builder` is optimized what we consider to be normal usage, that is,
347+-- `snoc`ing singleton elements to the end of the `Builder`.
348+--
349+-- If a Builder is built entirely by `snoc`ing, it will look like a
350+-- left-only binary tree, a.k.a. a linked list.
351+--
352+-- ```
353+-- ④
354+-- ╱
355+-- ③
356+-- ╱
357+-- ②
358+-- ╱
359+-- ①
360+-- ```
361+--
362+-- If two of these `snoc`-built trees are `append`ed, then the new tree
363+-- will look like
364+--
365+-- ```
366+-- ④
367+-- ╱ ╲
368+-- ③ ⑧
369+-- ╱ ╱
370+-- ② ⑦
371+-- ╱ ╱
372+-- ① ⑥
373+-- ╱
374+-- ⑤
375+-- ```
376+--
377+-- This is all similar to
378+-- [__bytestring-tree-builder__](https://hackage.haskell.org/package/bytestring-tree-builder)
379+-- , except that the
380+-- [`Tree`](https://hackage.haskell.org/package/bytestring-tree-builder-0.2.7.3/docs/src/ByteString.TreeBuilder.Tree.html#Tree)
381+-- structure in __bytestring-tree-builder__ only carries values in its
382+-- leaves, which is how it achieves *O(1)* appending, at the cost of
383+-- a higher coefficient time factor on the fold.
384+--
385+-- We hope that this implementation is fairly fast, but we
386+-- haven't chosen this implementation because it's fast, we've chosen
387+-- this implementation because it's simple.
388+-- If someone wants to create a fast Purescript ArrayBuffer serialization
389+-- library, then they can benchmark against this one to prove that the new
390+-- one is fast.
391+--
392+-- One relatively cheap and simple performance improvement for this library would be to
393+-- remove the Null constructor of `Builder` and instead use Javascript nulls.
394+--
395+-- In the longer term, it might make sense to try to change the Builder so
396+-- that it works like the
397+-- https://hackage.haskell.org/package/bytestring/docs/Data-ByteString-Builder.html
398+--
399+-- Here are some benchmarks of different Haskell ByteString builders
400+-- https://github.com/haskell-perf/strict-bytestring-builders
401+--
402+-- We've tried to design the API for this library with minimal assumptions,
403+-- so that if we want to change the Builder implementation later then we can.
404+--
405+-- Another way this could go:
406+-- https://kodimensional.dev/posts/2019-03-25-comonadic-builders