@@ -5,67 +5,47 @@
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 operations for working with `ArrayBuffer`, see
8+-- | For other 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 __purescript-arraybuffer__.
11+-- | in package __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
19−, putUint8
21+ , putUint8
20−, putInt8
22+ , putInt8
21−, putUint16be
23+ , putUint16be
22−, putUint16le
24+ , putUint16le
23−, putInt16be
25+ , putInt16be
24−, putInt16le
26+ , putInt16le
25−, putUint32be
27+ , putUint32be
26−, putUint32le
28+ , putUint32le
27−, putInt32be
29+ , putInt32be
28−, putInt32le
30+ , putInt32le
29−, putFloat32be
31+ , putFloat32be
30−, putFloat32le
32+ , putFloat32le
31−, putFloat64be
33+ , putFloat64be
32−, putFloat64le
34+ , putFloat64le
33−, Builder
35+ , module Data.ArrayBuffer.Builder.Internal
57import Prelude
38import Prelude
40+import Control.Monad.Rec.Class (class MonadRec)
59import Control.Monad.Writer.Trans (WriterT, execWriterT, tell, lift)
41import Control.Monad.Writer.Trans (WriterT, execWriterT, tell, lift)
60−import Data.ArrayBuffer.ArrayBuffer as AB
42+import Data.ArrayBuffer.Builder.Internal (Builder, DataBuff(..), length, toView, (<>>))
61−import Data.ArrayBuffer.DataView as DV
62−import Data.ArrayBuffer.Typed as AT
43+import Data.ArrayBuffer.Builder.Internal as Internal
63−import Data.ArrayBuffer.Types (ArrayBuffer, Uint8Array)
44+import Data.ArrayBuffer.Types (ArrayBuffer, DataView)
64import Data.Float32 (Float32)
45import Data.Float32 (Float32)
65−import Data.Maybe (Maybe(..))
66import Data.UInt (UInt)
46import Data.UInt (UInt)
67import Effect (Effect)
47import Effect (Effect)
68−import Effect.Class (class MonadEffect, liftEffect)
48+import Effect.Class (class MonadEffect)
70-- | The `PutM` monad is a `WriterT Builder` transformer monad which
50-- | The `PutM` monad is a `WriterT Builder` transformer monad which
71-- | gives us do-notation for the `Builder` monoid. The base monad must be
51-- | gives us do-notation for the `Builder` monoid. The base monad must be
@@ -78,111 +58,11 @@ type PutM = WriterT Builder
78-- | The `PutM` type reified to `Effect`, in other words `WriterT Builder Effect`.
58-- | The `PutM` type reified to `Effect`, in other words `WriterT Builder Effect`.
79type Put = PutM Effect
59type Put = PutM Effect
81−-- | Monoidal builder for `ArrayBuffer`s.
61+-- | Build an `ArrayBuffer` with do-notation in any `MonadEffect`. *O(n)* Stack-safe.
82−-- |
62+execPutM :: forall m. MonadEffect m => MonadRec m => PutM m Unit -> m ArrayBuffer
83−-- | ### Left-associative `<>>` append operator
63+execPutM = Internal.execBuilder <=< execWriterT
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
181−-- | Build an `ArrayBuffer` with do-notation in any `MonadEffect`. *O(n)*
65+-- | Build an `ArrayBuffer` with do-notation in `Effect`. *O(n)* Stack-safe.
182−execPutM :: forall m. (MonadEffect m) => PutM m Unit -> m ArrayBuffer
183−execPutM = execBuilder <=< execWriterT
185−-- | Build an `ArrayBuffer` with do-notation. *O(n)*
186execPut :: Put Unit -> Effect ArrayBuffer
66execPut :: Put Unit -> Effect ArrayBuffer
187execPut = execPutM
67execPut = execPutM
@@ -195,7 +75,6 @@ execPut = execPutM
196-- | In a `PutM` monad do-block, we can
76-- | In a `PutM` monad do-block, we can
201-- | messageBuilder <- subBuilder $ do
80-- | messageBuilder <- subBuilder $ do
@@ -205,239 +84,73 @@ execPut = execPutM
205-- | putInt32be $ length messageBuilder
84-- | putInt32be $ length messageBuilder
206-- | tell messageBuilder
85-- | tell messageBuilder
208−subBuilder :: forall m. (MonadEffect m) => PutM m Unit -> PutM m Builder
87+subBuilder :: forall m. Monad m => PutM m Unit -> PutM m Builder
209subBuilder = lift <<< execWriterT
88subBuilder = lift <<< execWriterT
211-- | Append an `ArrayBuffer` to the builder.
90-- | Append an `ArrayBuffer` to the builder.
212−putArrayBuffer :: forall m. (MonadEffect m) => ArrayBuffer -> PutM m Unit
91+putArrayBuffer :: forall m. Monad m => ArrayBuffer -> PutM m Unit
213−putArrayBuffer = tell <<< singleton
92+putArrayBuffer = tell <<< Internal.singleton <<< Internal.Buff
215−-- | Serialize an 8-bit unsigned integer (byte) into a new `ArrayBuffer`.
94+-- | Append a `DataView` to the builder.
216−encodeUint8 :: forall m. (MonadEffect m) => UInt -> m ArrayBuffer
95+putDataView :: forall m. Monad m => DataView -> PutM m Unit
217−encodeUint8 x = do
96+putDataView = tell <<< Internal.singleton <<< Internal.View
218− buf <- liftEffect $ AB.empty 1
219− _ <- liftEffect $ DV.setUint8 (DV.whole buf) 0 x
222−-- | Append an 8-bit unsigned integer (byte) to the builder.
98+-- | Append either an `ArrayBuffer` or a `DataView` to the builder.
223−putUint8 :: forall m. (MonadEffect m) => UInt -> PutM m Unit
99+putDataBuff :: forall m. Monad m => DataBuff -> PutM m Unit
224−putUint8 = putArrayBuffer <=< encodeUint8
100+putDataBuff = tell <<< Internal.singleton
226−-- | Serialize an 8-bit two’s-complement signed integer (char) into a new `ArrayBuffer`.
102+-- | Append an 8-bit unsigned integer (byte) to the builder.
227−encodeInt8 :: forall m. (MonadEffect m) => Int -> m ArrayBuffer
103+putUint8 :: forall m. MonadEffect m => UInt -> PutM m Unit
228−encodeInt8 x = do
104+putUint8 = putArrayBuffer <=< Internal.encodeUint8
229− buf <- liftEffect $ AB.empty 1
230− _ <- liftEffect $ DV.setInt8 (DV.whole buf) 0 x
233-- | Append an 8-bit two’s-complement signed integer (char) to the builder.
106-- | Append an 8-bit two’s-complement signed integer (char) to the builder.
234−putInt8 :: forall m. (MonadEffect m) => Int -> PutM m Unit
107+putInt8 :: forall m. MonadEffect m => Int -> PutM m Unit
235−putInt8 = putArrayBuffer <=< encodeInt8
108+putInt8 = putArrayBuffer <=< Internal.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
244-- | Append a 16-bit big-endian unsigned integer to the builder.
110-- | Append a 16-bit big-endian unsigned integer to the builder.
245−putUint16be :: forall m. (MonadEffect m) => UInt -> PutM m Unit
111+putUint16be :: forall m. MonadEffect m => UInt -> PutM m Unit
246−putUint16be = putArrayBuffer <=< encodeUint16be
112+putUint16be = putArrayBuffer <=< Internal.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
255-- | Append a 16-bit little-endian unsigned integer to the builder.
114-- | Append a 16-bit little-endian unsigned integer to the builder.
256−putUint16le :: forall m. (MonadEffect m) => UInt -> PutM m Unit
115+putUint16le :: forall m. MonadEffect m => UInt -> PutM m Unit
257−putUint16le = putArrayBuffer <=< encodeUint16le
116+putUint16le = putArrayBuffer <=< Internal.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
266-- | Append a 16-bit big-endian two’s-complement signed integer to the builder.
118-- | Append a 16-bit big-endian two’s-complement signed integer to the builder.
267−putInt16be :: forall m. (MonadEffect m) => Int -> PutM m Unit
119+putInt16be :: forall m. MonadEffect m => Int -> PutM m Unit
268−putInt16be = putArrayBuffer <=< encodeInt16be
120+putInt16be = putArrayBuffer <=< Internal.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
277-- | Append a 16-bit little-endian two’s-complement signed integer to the builder.
122-- | Append a 16-bit little-endian two’s-complement signed integer to the builder.
278−putInt16le :: forall m. (MonadEffect m) => Int -> PutM m Unit
123+putInt16le :: forall m. MonadEffect m => Int -> PutM m Unit
279−putInt16le = putArrayBuffer <=< encodeInt16le
124+putInt16le = putArrayBuffer <=< Internal.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
288-- | Append a 32-bit big-endian unsigned integer to the builder.
126-- | Append a 32-bit big-endian unsigned integer to the builder.
289−putUint32be :: forall m. (MonadEffect m) => UInt -> PutM m Unit
127+putUint32be :: forall m. MonadEffect m => UInt -> PutM m Unit
290−putUint32be = putArrayBuffer <=< encodeUint32be
128+putUint32be = putArrayBuffer <=< Internal.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
299-- | Append a 32-bit little-endian unsigned integer to the builder.
130-- | Append a 32-bit little-endian unsigned integer to the builder.
300−putUint32le :: forall m. (MonadEffect m) => UInt -> PutM m Unit
131+putUint32le :: forall m. MonadEffect m => UInt -> PutM m Unit
301−putUint32le = putArrayBuffer <=< encodeUint32le
132+putUint32le = putArrayBuffer <=< Internal.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
310-- | Append a 32-bit big-endian two’s-complement signed integer to the builder.
134-- | Append a 32-bit big-endian two’s-complement signed integer to the builder.
311−putInt32be :: forall m. (MonadEffect m) => Int -> PutM m Unit
135+putInt32be :: forall m. MonadEffect m => Int -> PutM m Unit
312−putInt32be = putArrayBuffer <=< encodeInt32be
136+putInt32be = putArrayBuffer <=< Internal.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
321-- | Append a 32-bit little-endian two’s-complement signed integer to the builder.
138-- | Append a 32-bit little-endian two’s-complement signed integer to the builder.
322−putInt32le :: forall m. (MonadEffect m) => Int -> PutM m Unit
139+putInt32le :: forall m. MonadEffect m => Int -> PutM m Unit
323−putInt32le = putArrayBuffer <=< encodeInt32le
140+putInt32le = putArrayBuffer <=< Internal.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
332-- | Append a 32-bit big-endian IEEE single-precision float to the builder.
142-- | Append a 32-bit big-endian IEEE single-precision float to the builder.
333−putFloat32be :: forall m. (MonadEffect m) => Float32 -> PutM m Unit
143+putFloat32be :: forall m. MonadEffect m => Float32 -> PutM m Unit
334−putFloat32be = putArrayBuffer <=< encodeFloat32be
144+putFloat32be = putArrayBuffer <=< Internal.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
343-- | Append a 32-bit little-endian IEEE single-precision float to the builder.
146-- | Append a 32-bit little-endian IEEE single-precision float to the builder.
344−putFloat32le :: forall m. (MonadEffect m) => Float32 -> PutM m Unit
147+putFloat32le :: forall m. MonadEffect m => Float32 -> PutM m Unit
345−putFloat32le = putArrayBuffer <=< encodeFloat32le
148+putFloat32le = putArrayBuffer <=< Internal.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
354-- | Append a 64-bit big-endian IEEE double-precision float to the builder.
150-- | Append a 64-bit big-endian IEEE double-precision float to the builder.
355−putFloat64be :: forall m. (MonadEffect m) => Number -> PutM m Unit
151+putFloat64be :: forall m. MonadEffect m => Number -> PutM m Unit
356−putFloat64be = putArrayBuffer <=< encodeFloat64be
152+putFloat64be = putArrayBuffer <=< Internal.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
365-- | Append a 64-bit little-endian IEEE double-precision float to the builder.
154-- | Append a 64-bit little-endian IEEE double-precision float to the builder.
366−putFloat64le :: forall m. (MonadEffect m) => Number -> PutM m Unit
155+putFloat64le :: forall m. MonadEffect m => Number -> PutM m Unit
367−putFloat64le = putArrayBuffer <=< encodeFloat64le
156+putFloat64le = putArrayBuffer <=< Internal.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