@@ -5,47 +5,67 @@
5-- | Writing to an `ArrayBuffer` is an `Effect`ful activity, so most
5-- | Writing to an `ArrayBuffer` is an `Effect`ful activity, so most
6-- | functions in this module must be run in a `MonadEffect` context.
6-- | functions in this module must be run in a `MonadEffect` context.
8−-- | For other operations for working with `ArrayBuffer`, see
8+-- | For operations for working with `ArrayBuffer`, see
10-- | [`Data.ArrayBuffer.ArrayBuffer`](https://pursuit.purescript.org/packages/purescript-arraybuffer/docs/Data.ArrayBuffer.ArrayBuffer)
10-- | [`Data.ArrayBuffer.ArrayBuffer`](https://pursuit.purescript.org/packages/purescript-arraybuffer/docs/Data.ArrayBuffer.ArrayBuffer)
11−-- | in package __arraybuffer__.
11+-- | in package __purescript-arraybuffer__.
12module Data.ArrayBuffer.Builder
12module Data.ArrayBuffer.Builder
15− , execPutM
15+, execPutM
16− , execPut
16+, execPut
17− , subBuilder
17+, subBuilder
18− , putArrayBuffer
18+, putArrayBuffer
21− , putUint8
19+, putUint8
22− , putInt8
20+, putInt8
23− , putUint16be
21+, putUint16be
24− , putUint16le
22+, putUint16le
25− , putInt16be
23+, putInt16be
26− , putInt16le
24+, putInt16le
27− , putUint32be
25+, putUint32be
28− , putUint32le
26+, putUint32le
29− , putInt32be
27+, putInt32be
30− , putInt32le
28+, putInt32le
31− , putFloat32be
29+, putFloat32be
32− , putFloat32le
30+, putFloat32le
33− , putFloat64be
31+, putFloat64be
34− , putFloat64le
32+, putFloat64le
35− , module Data.ArrayBuffer.Builder.Internal
33+, Builder
38import Prelude
57import Prelude
40−import Control.Monad.Rec.Class (class MonadRec)
41import Control.Monad.Writer.Trans (WriterT, execWriterT, tell, lift)
59import Control.Monad.Writer.Trans (WriterT, execWriterT, tell, lift)
42−import Data.ArrayBuffer.Builder.Internal (Builder, DataBuff(..), length, toView, (<>>))
60+import Data.ArrayBuffer.ArrayBuffer as AB
43−import Data.ArrayBuffer.Builder.Internal as Internal
44−import Data.ArrayBuffer.Types (ArrayBuffer, DataView)
61+import Data.ArrayBuffer.DataView as DV
62+import Data.ArrayBuffer.Typed as AT
63+import Data.ArrayBuffer.Types (ArrayBuffer, Uint8Array)
45import Data.Float32 (Float32)
64import Data.Float32 (Float32)
65+import Data.Maybe (Maybe(..))
46import Data.UInt (UInt)
66import Data.UInt (UInt)
47import Effect (Effect)
67import Effect (Effect)
48−import Effect.Class (class MonadEffect)
68+import Effect.Class (class MonadEffect, liftEffect)
50-- | The `PutM` monad is a `WriterT Builder` transformer monad which
70-- | The `PutM` monad is a `WriterT Builder` transformer monad which
51-- | gives us do-notation for the `Builder` monoid. The base monad must be
71-- | gives us do-notation for the `Builder` monoid. The base monad must be
@@ -58,11 +78,111 @@ type PutM = WriterT Builder
58-- | The `PutM` type reified to `Effect`, in other words `WriterT Builder Effect`.
78-- | The `PutM` type reified to `Effect`, in other words `WriterT Builder Effect`.
59type Put = PutM Effect
79type Put = PutM Effect
61−-- | Build an `ArrayBuffer` with do-notation in any `MonadEffect`. *O(n)* Stack-safe.
81+-- | Monoidal builder for `ArrayBuffer`s.
62−execPutM :: forall m. MonadEffect m => MonadRec m => PutM m Unit -> m ArrayBuffer
82+-- |
63−execPutM = Internal.execBuilder <=< execWriterT
83+-- | ### Left-associative `<>>` append operator
85+-- | __TL;DR__ You probably don't want to use the `Builder` monoid directly
86+-- | in your code, it’s better to use the `PutM` monad with do-notation instead.
88+-- | The `Builder` monoid in this library is efficient when we `snoc` single
89+-- | items onto the end of it, or when we only `cons` single items to the
90+-- | beginning, but it can be less efficient when we are mixing `cons` and
92+-- | This matters when we're appending three or more `Builder`s
93+-- | in one associative expression, because the
94+-- | `Semigroup` append operator `<>` is right-associative.
96+-- | To solve this, we provide an operator `<>>` for appending `Builders`.
97+-- | `<>>` is exactly the same as `<>`, but left-associative.
98+-- | This only matters when we're chaining together three
99+-- | or more `Builder`s in a single associative expression.
102+-- | builder₁ <> builder₂ <> builder₃
104+-- | we should always prefer to
107+-- | builder₁ <>> builder₂ <>> builder₃
109+-- | so that we get the efficient
110+-- | `snoc`ing of `Builder`s.
112+-- | If we build our `ArrayBuffer`s with the `PutM` monad instead of appending by
113+-- | using the `Semigroup` instance of `Builder`, then we always get the efficient
117+ = Node Builder ArrayBuffer Builder
120+instance semigroupBuilder :: Semigroup Builder where
121+ append Null Null = Null
124+ append l (Node Null rx rr) = Node l rx rr -- this is the snoc case
125+ append (Node ll lx Null) r = Node ll lx r -- found the rightmost Null
127+ append (Node ll lx lr) r = Node ll lx $ append lr r -- search down the right
128+ -- side of the left tree
129+ -- to find the rightmost
132+infixl 5 append as <>>
134+instance monoidBuilder :: Monoid Builder where
137+-- | `Builder` is not an instance of `Foldable` because `Builder` is monomorphic.
138+foldl :: forall b. (b -> ArrayBuffer -> b) -> b -> Builder -> b
140+foldl f a (Node l x r) = foldl f (f (foldl f a l) x) r
142+-- | Monomorphic foldM copied from
143+-- | [`Data.Foldable.foldM`](https://pursuit.purescript.org/packages/purescript-foldable-traversable/4.1.1/docs/Data.Foldable#v:foldM)
144+foldM :: forall b m. (Monad m) => (b -> ArrayBuffer -> m b) -> b -> Builder -> m b
145+foldM f a0 = foldl (\ma b -> ma >>= flip f b) (pure a0)
147+-- | Construct a `Builder` with a single `ArrayBuffer`. *O(1)*
148+singleton :: ArrayBuffer -> Builder
149+singleton buf = Node Null buf Null
151+-- | Prepend an `ArrayBuffer` to the beginning of the `Builder`. *O(1)*
152+cons :: ArrayBuffer -> Builder -> Builder
153+cons x bs = Node Null x bs
155+-- | Append an `ArrayBuffer` to the end of the `Builder`. *O(1)*
156+snoc :: Builder -> ArrayBuffer -> Builder
157+snoc bs x = Node bs x Null
159+-- | Calculate the total byte length of the `Builder`. *O(n)*
160+length :: Builder -> Int
161+length bldr = foldl (\b a -> b + AB.byteLength a) 0 bldr
163+-- | Build a single `ArrayBuffer` from a `Builder`. *O(n)*
164+execBuilder :: forall m. (MonadEffect m) => Builder -> m ArrayBuffer
165+execBuilder bldr = do
166+ let buflen = foldl (\b a -> b + AB.byteLength a) 0 bldr
167+ -- Allocate the final ArrayBuffer
168+ buf <- liftEffect $ AB.empty buflen
169+ -- Then copy each ArrayBuffer into the final ArrayBuffer.
170+ -- To memcpy from one ArrayBuffer to another, apparently we have to first view
171+ -- both ArrayBuffers as ArrayView (Typed Array).
172+ newview <- liftEffect (AT.whole buf :: Effect Uint8Array)
175+ aview <- liftEffect (AT.whole a :: Effect Uint8Array)
176+ _ <- liftEffect $ AT.setTyped newview (Just offset) aview
177+ pure $ offset + AB.byteLength a
65−-- | Build an `ArrayBuffer` with do-notation in `Effect`. *O(n)* Stack-safe.
181+-- | Build an `ArrayBuffer` with do-notation in any `MonadEffect`. *O(n)*
182+execPutM :: forall m. (MonadEffect m) => PutM m Unit -> m ArrayBuffer
183+execPutM = execBuilder <=< execWriterT
185+-- | Build an `ArrayBuffer` with do-notation. *O(n)*
66execPut :: Put Unit -> Effect ArrayBuffer
186execPut :: Put Unit -> Effect ArrayBuffer
67execPut = execPutM
187execPut = execPutM
@@ -75,82 +195,249 @@ execPut = execPutM
76-- | In a `PutM` monad do-block, we can
196-- | In a `PutM` monad do-block, we can
80-- | messageBuilder <- subBuilder $ do
201-- | messageBuilder <- subBuilder $ do
81-- | putField1
202-- | putField1
82-- | putField2
203-- | putField2
84−-- | putInt32be $ length messageBuilder
205+-- | putInt32 $ length messageBuilder
85-- | tell messageBuilder
206-- | tell messageBuilder
87−subBuilder :: forall m. Monad m => PutM m Unit -> PutM m Builder
208+subBuilder :: forall m. (MonadEffect m) => PutM m Unit -> PutM m Builder
88subBuilder = lift <<< execWriterT
209subBuilder = lift <<< execWriterT
90-- | Append an `ArrayBuffer` to the builder.
211-- | Append an `ArrayBuffer` to the builder.
91−putArrayBuffer :: forall m. Monad m => ArrayBuffer -> PutM m Unit
212+putArrayBuffer :: forall m. (MonadEffect m) => ArrayBuffer -> PutM m Unit
92−putArrayBuffer = tell <<< Internal.singleton <<< Internal.Buff
213+putArrayBuffer = tell <<< singleton
94−-- | Append a `DataView` to the builder.
95−putDataView :: forall m. Monad m => DataView -> PutM m Unit
96−putDataView = tell <<< Internal.singleton <<< Internal.View
98−-- | Append either an `ArrayBuffer` or a `DataView` to the builder.
215+-- | Serialize an 8-bit unsigned integer (byte) into a new `ArrayBuffer`.
99−putDataBuff :: forall m. Monad m => DataBuff -> PutM m Unit
216+encodeUint8 :: forall m. (MonadEffect m) => UInt -> m ArrayBuffer
100−putDataBuff = tell <<< Internal.singleton
217+encodeUint8 x = do
218+ buf <- liftEffect $ AB.empty 1
219+ _ <- liftEffect $ DV.setUint8 (DV.whole buf) 0 x
102-- | Append an 8-bit unsigned integer (byte) to the builder.
222-- | Append an 8-bit unsigned integer (byte) to the builder.
103−putUint8 :: forall m. MonadEffect m => UInt -> PutM m Unit
223+putUint8 :: forall m. (MonadEffect m) => UInt -> PutM m Unit
104−putUint8 = putArrayBuffer <=< Internal.encodeUint8
224+putUint8 = putArrayBuffer <=< encodeUint8
226+-- | Serialize an 8-bit two’s-complement signed integer (char) into a new `ArrayBuffer`.
227+encodeInt8 :: forall m. (MonadEffect m) => Int -> m ArrayBuffer
229+ buf <- liftEffect $ AB.empty 1
230+ _ <- liftEffect $ DV.setInt8 (DV.whole buf) 0 x
106-- | Append an 8-bit two’s-complement signed integer (char) to the builder.
233-- | Append an 8-bit two’s-complement signed integer (char) to the builder.
107−putInt8 :: forall m. MonadEffect m => Int -> PutM m Unit
234+putInt8 :: forall m. (MonadEffect m) => Int -> PutM m Unit
108−putInt8 = putArrayBuffer <=< Internal.encodeInt8
235+putInt8 = putArrayBuffer <=< encodeInt8
237+-- | Serialize a 16-bit big-endian unsigned integer into a new `ArrayBuffer`.
238+encodeUint16be :: forall m. (MonadEffect m) => UInt -> m ArrayBuffer
239+encodeUint16be x = do
240+ buf <- liftEffect $ AB.empty 2
241+ _ <- liftEffect $ DV.setUint16be (DV.whole buf) 0 x
110-- | Append a 16-bit big-endian unsigned integer to the builder.
244-- | Append a 16-bit big-endian unsigned integer to the builder.
111−putUint16be :: forall m. MonadEffect m => UInt -> PutM m Unit
245+putUint16be :: forall m. (MonadEffect m) => UInt -> PutM m Unit
112−putUint16be = putArrayBuffer <=< Internal.encodeUint16be
246+putUint16be = putArrayBuffer <=< encodeUint16be
248+-- | Serialize a 16-bit little-endian unsigned integer into a new `ArrayBuffer`.
249+encodeUint16le :: forall m. (MonadEffect m) => UInt -> m ArrayBuffer
250+encodeUint16le x = do
251+ buf <- liftEffect $ AB.empty 2
252+ _ <- liftEffect $ DV.setUint16le (DV.whole buf) 0 x
114-- | Append a 16-bit little-endian unsigned integer to the builder.
255-- | Append a 16-bit little-endian unsigned integer to the builder.
115−putUint16le :: forall m. MonadEffect m => UInt -> PutM m Unit
256+putUint16le :: forall m. (MonadEffect m) => UInt -> PutM m Unit
116−putUint16le = putArrayBuffer <=< Internal.encodeUint16le
257+putUint16le = putArrayBuffer <=< encodeUint16le
259+-- | Serialize a 16-bit big-endian two’s-complement signed integer into a new `ArrayBuffer`.
260+encodeInt16be :: forall m. (MonadEffect m) => Int -> m ArrayBuffer
262+ buf <- liftEffect $ AB.empty 2
263+ _ <- liftEffect $ DV.setInt16be (DV.whole buf) 0 x
118-- | Append a 16-bit big-endian two’s-complement signed integer to the builder.
266-- | Append a 16-bit big-endian two’s-complement signed integer to the builder.
119−putInt16be :: forall m. MonadEffect m => Int -> PutM m Unit
267+putInt16be :: forall m. (MonadEffect m) => Int -> PutM m Unit
120−putInt16be = putArrayBuffer <=< Internal.encodeInt16be
268+putInt16be = putArrayBuffer <=< encodeInt16be
270+-- | Serialize a 16-bit little-endian two’s-complement signed integer into a new `ArrayBuffer`.
271+encodeInt16le :: forall m. (MonadEffect m) => Int -> m ArrayBuffer
273+ buf <- liftEffect $ AB.empty 2
274+ _ <- liftEffect $ DV.setInt16le (DV.whole buf) 0 x
122-- | Append a 16-bit little-endian two’s-complement signed integer to the builder.
277-- | Append a 16-bit little-endian two’s-complement signed integer to the builder.
123−putInt16le :: forall m. MonadEffect m => Int -> PutM m Unit
278+putInt16le :: forall m. (MonadEffect m) => Int -> PutM m Unit
124−putInt16le = putArrayBuffer <=< Internal.encodeInt16le
279+putInt16le = putArrayBuffer <=< encodeInt16le
281+-- | Serialize a 32-bit big-endian unsigned integer into a new `ArrayBuffer`.
282+encodeUint32be :: forall m. (MonadEffect m) => UInt -> m ArrayBuffer
283+encodeUint32be x = do
284+ buf <- liftEffect $ AB.empty 4
285+ _ <- liftEffect $ DV.setUint32be (DV.whole buf) 0 x
126-- | Append a 32-bit big-endian unsigned integer to the builder.
288-- | Append a 32-bit big-endian unsigned integer to the builder.
127−putUint32be :: forall m. MonadEffect m => UInt -> PutM m Unit
289+putUint32be :: forall m. (MonadEffect m) => UInt -> PutM m Unit
128−putUint32be = putArrayBuffer <=< Internal.encodeUint32be
290+putUint32be = putArrayBuffer <=< encodeUint32be
292+-- | Serialize a 32-bit little-endian unsigned integer into a new `ArrayBuffer`.
293+encodeUint32le :: forall m. (MonadEffect m) => UInt -> m ArrayBuffer
294+encodeUint32le x = do
295+ buf <- liftEffect $ AB.empty 4
296+ _ <- liftEffect $ DV.setUint32le (DV.whole buf) 0 x
130-- | Append a 32-bit little-endian unsigned integer to the builder.
299-- | Append a 32-bit little-endian unsigned integer to the builder.
131−putUint32le :: forall m. MonadEffect m => UInt -> PutM m Unit
300+putUint32le :: forall m. (MonadEffect m) => UInt -> PutM m Unit
132−putUint32le = putArrayBuffer <=< Internal.encodeUint32le
301+putUint32le = putArrayBuffer <=< encodeUint32le
303+-- | Serialize a 32-bit big-endian two’s-complement signed integer into a new `ArrayBuffer`.
304+encodeInt32be :: forall m. (MonadEffect m) => Int -> m ArrayBuffer
306+ buf <- liftEffect $ AB.empty 4
307+ _ <- liftEffect $ DV.setInt32be (DV.whole buf) 0 x
134-- | Append a 32-bit big-endian two’s-complement signed integer to the builder.
310-- | Append a 32-bit big-endian two’s-complement signed integer to the builder.
135−putInt32be :: forall m. MonadEffect m => Int -> PutM m Unit
311+putInt32be :: forall m. (MonadEffect m) => Int -> PutM m Unit
136−putInt32be = putArrayBuffer <=< Internal.encodeInt32be
312+putInt32be = putArrayBuffer <=< encodeInt32be
314+-- | Serialize a 32-bit little-endian two’s-complement signed integer into a new `ArrayBuffer`.
315+encodeInt32le :: forall m. (MonadEffect m) => Int -> m ArrayBuffer
317+ buf <- liftEffect $ AB.empty 4
318+ _ <- liftEffect $ DV.setInt32le (DV.whole buf) 0 x
138-- | Append a 32-bit little-endian two’s-complement signed integer to the builder.
321-- | Append a 32-bit little-endian two’s-complement signed integer to the builder.
139−putInt32le :: forall m. MonadEffect m => Int -> PutM m Unit
322+putInt32le :: forall m. (MonadEffect m) => Int -> PutM m Unit
140−putInt32le = putArrayBuffer <=< Internal.encodeInt32le
323+putInt32le = putArrayBuffer <=< encodeInt32le
325+-- | Serialize a 32-bit big-endian IEEE single-precision float into a new `ArrayBuffer`.
326+encodeFloat32be :: forall m. (MonadEffect m) => Float32 -> m ArrayBuffer
327+encodeFloat32be x = do
328+ buf <- liftEffect $ AB.empty 4
329+ _ <- liftEffect $ DV.setFloat32be (DV.whole buf) 0 x
142-- | Append a 32-bit big-endian IEEE single-precision float to the builder.
332-- | Append a 32-bit big-endian IEEE single-precision float to the builder.
143−putFloat32be :: forall m. MonadEffect m => Float32 -> PutM m Unit
333+putFloat32be :: forall m. (MonadEffect m) => Float32 -> PutM m Unit
144−putFloat32be = putArrayBuffer <=< Internal.encodeFloat32be
334+putFloat32be = putArrayBuffer <=< encodeFloat32be
336+-- | Serialize a 32-bit little-endian IEEE single-precision float into a new `ArrayBuffer`.
337+encodeFloat32le :: forall m. (MonadEffect m) => Float32 -> m ArrayBuffer
338+encodeFloat32le x = do
339+ buf <- liftEffect $ AB.empty 4
340+ _ <- liftEffect $ DV.setFloat32le (DV.whole buf) 0 x
146-- | Append a 32-bit little-endian IEEE single-precision float to the builder.
343-- | Append a 32-bit little-endian IEEE single-precision float to the builder.
147−putFloat32le :: forall m. MonadEffect m => Float32 -> PutM m Unit
344+putFloat32le :: forall m. (MonadEffect m) => Float32 -> PutM m Unit
148−putFloat32le = putArrayBuffer <=< Internal.encodeFloat32le
345+putFloat32le = putArrayBuffer <=< encodeFloat32le
347+-- | Serialize a 64-bit big-endian IEEE double-precision float into a new `ArrayBuffer`.
348+encodeFloat64be :: forall m. (MonadEffect m) => Number -> m ArrayBuffer
349+encodeFloat64be x = do
350+ buf <- liftEffect $ AB.empty 8
351+ _ <- liftEffect $ DV.setFloat64be (DV.whole buf) 0 x
150-- | Append a 64-bit big-endian IEEE double-precision float to the builder.
354-- | Append a 64-bit big-endian IEEE double-precision float to the builder.
151−putFloat64be :: forall m. MonadEffect m => Number -> PutM m Unit
355+putFloat64be :: forall m. (MonadEffect m) => Number -> PutM m Unit
152−putFloat64be = putArrayBuffer <=< Internal.encodeFloat64be
356+putFloat64be = putArrayBuffer <=< encodeFloat64be
358+-- | Serialize a 64-bit little-endian IEEE double-precision float into a new `ArrayBuffer`.
359+encodeFloat64le :: forall m. (MonadEffect m) => Number -> m ArrayBuffer
360+encodeFloat64le x = do
361+ buf <- liftEffect $ AB.empty 8
362+ _ <- liftEffect $ DV.setFloat64le (DV.whole buf) 0 x
154-- | Append a 64-bit little-endian IEEE double-precision float to the builder.
365-- | Append a 64-bit little-endian IEEE double-precision float to the builder.
155−putFloat64le :: forall m. MonadEffect m => Number -> PutM m Unit
366+putFloat64le :: forall m. (MonadEffect m) => Number -> PutM m Unit
156−putFloat64le = putArrayBuffer <=< Internal.encodeFloat64le
367+putFloat64le = putArrayBuffer <=< encodeFloat64le
370+-- ## Implementation Details
372+-- We want our `Builder` to be a data structure with
373+-- * *O(1)* monoid append
376+-- Our `Builder` implementation is an unbalanced binary tree.
378+-- For monoid `append`, what we actually get is *O(1)* when either the
379+-- left or right tree is a singleton. If that's not true, then in the
380+-- unlikely worst case `append` might be *O(n)*.
382+-- `Builder` is optimized what we consider to be normal usage, that is,
383+-- `snoc`ing singleton elements to the end of the `Builder`.
385+-- If a Builder is built entirely by `snoc`ing, it will look like a
386+-- left-only binary tree, a.k.a. a linked list.
398+-- If two of these `snoc`-built trees are `append`ed, then the new tree
413+-- This is all similar to
414+-- [__bytestring-tree-builder__](https://hackage.haskell.org/package/bytestring-tree-builder)
416+-- [`Tree`](https://hackage.haskell.org/package/bytestring-tree-builder-0.2.7.3/docs/src/ByteString.TreeBuilder.Tree.html#Tree)
417+-- structure in __bytestring-tree-builder__ only carries values in its
418+-- leaves, which is how it achieves *O(1)* appending, at the cost of
419+-- a higher coefficient time factor on the fold.
421+-- We hope that this implementation is fairly fast, but we
422+-- haven't chosen this implementation because it's fast, we've chosen
423+-- this implementation because it's simple.
424+-- If someone wants to create a fast Purescript ArrayBuffer serialization
425+-- library, then they can benchmark against this one to prove that the new
428+-- One relatively cheap and simple performance improvement for this library would be to
429+-- remove the Null constructor of `Builder` and instead use Javascript nulls.
431+-- In the longer term, it might make sense to try to change the Builder so
432+-- that it works like the
433+-- https://hackage.haskell.org/package/bytestring/docs/Data-ByteString-Builder.html
435+-- Here are some benchmarks of different Haskell ByteString builders
436+-- https://github.com/haskell-perf/strict-bytestring-builders
438+-- We've tried to design the API for this library with minimal assumptions,
439+-- so that if we want to change the Builder implementation later then we can.
441+-- Another way this could go:
442+-- https://kodimensional.dev/posts/2019-03-25-comonadic-builders