@@ -16,6 +16,7 @@ module Data.ArrayBuffer.Builder
1616, execPut
1717, subBuilder
1818, putArrayBuffer
19+, putDataView
1920, putUint8
2021, putInt8
2122, putUint16be
@@ -32,11 +33,16 @@ module Data.ArrayBuffer.Builder
3233, putFloat64le
3334, Builder
3435, (<>>)
36+, Bytes
37+, toView
3538, execBuilder
3639, length
3740, singleton
41+, singleton_
3842, cons
43+, cons_
3944, snoc
45+, snoc_
4046, encodeUint8
4147, encodeInt8
4248, encodeUint16be
@@ -60,7 +66,7 @@ import Control.Monad.Writer.Trans (WriterT, execWriterT, tell, lift)
6066import Data.ArrayBuffer.ArrayBuffer as AB
6167import Data.ArrayBuffer.DataView as DV
6268import Data.ArrayBuffer.Typed as AT
63−import Data.ArrayBuffer.Types (ArrayBuffer, Uint8Array)
69+import Data.ArrayBuffer.Types (ArrayBuffer, Uint8Array, DataView, ByteLength)
6470import Data.Float32 (Float32)
6571import Data.Maybe (Maybe(..))
6672import Data.UInt (UInt)
@@ -80,6 +86,15 @@ type Put = PutM Effect
8086
8187-- | Monoidal builder for `ArrayBuffer`s.
8288-- |
89+-- | We can add two types of things to the `Builder`: `ArrayBuffer`s and
90+-- | `DataView`s.
91+-- | We might prefer
92+-- | to add a `DataView` to a `Builder` when we’re adding a large slice of data
93+-- | from some other `ArrayBuffer`, so that we don’t
94+-- | need an extra intermediate copy of the slice.
95+-- | Our convention is that functions which add `DataView`s to
96+-- | the `Builder` are suffixed with underscore (`_`).
97+-- |
8398-- | ### Left-associative `<>>` append operator
8499-- |
85100-- | __TL;DR__ You probably don't want to use the `Builder` monoid directly
@@ -114,7 +129,7 @@ type Put = PutM Effect
114129-- | `snoc` case.
115130-- |
116131data Builder
117− = Node Builder ArrayBuffer Builder
132+ = Node Builder Bytes Builder
118133 | Null
119134
120135instance semigroupBuilder :: Semigroup Builder where
@@ -134,36 +149,60 @@ infixl 5 append as <>>
134149instance monoidBuilder :: Monoid Builder where
135150 mempty = Null
136151
152+-- | For distinguishing between `ArrayBuffer` and `DataView`.
153+data Bytes
154+ = ByteBuff ArrayBuffer
155+ | ByteView DataView
156+
157+toView :: Bytes -> DataView
158+toView (ByteBuff ab) = DV.whole ab
159+toView (ByteView dv) = dv
160+
137161-- | `Builder` is not an instance of `Foldable` because `Builder` is monomorphic.
138−foldl :: forall b. (b -> ArrayBuffer -> b) -> b -> Builder -> b
139−foldl f a Null = a
162+foldl :: forall b. (b -> Bytes -> b) -> b -> Builder -> b
163+foldl _ a Null = a
140164foldl f a (Node l x r) = foldl f (f (foldl f a l) x) r
141165
142166-- | Monomorphic foldM copied from
143167-- | [`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
168+foldM :: forall b m. (Monad m) => (b -> Bytes -> m b) -> b -> Builder -> m b
145169foldM f a0 = foldl (\ma b -> ma >>= flip f b) (pure a0)
146170
147171-- | Construct a `Builder` with a single `ArrayBuffer`. *O(1)*
148172singleton :: ArrayBuffer -> Builder
149−singleton buf = Node Null buf Null
173+singleton buf = Node Null (ByteBuff buf) Null
174+
175+-- | Construct a `Builder` with a single `DataView`. *O(1)*
176+singleton_ :: DataView -> Builder
177+singleton_ buf = Node Null (ByteView buf) Null
150178
151179-- | Prepend an `ArrayBuffer` to the beginning of the `Builder`. *O(1)*
152180cons :: ArrayBuffer -> Builder -> Builder
153−cons x bs = Node Null x bs
181+cons x bs = Node Null (ByteBuff x) bs
182+
183+-- | Prepend a `DataView` to the beginning of the `Builder`. *O(1)*
184+cons_ :: DataView -> Builder -> Builder
185+cons_ x bs = Node Null (ByteView x) bs
154186
155187-- | Append an `ArrayBuffer` to the end of the `Builder`. *O(1)*
156188snoc :: Builder -> ArrayBuffer -> Builder
157−snoc bs x = Node bs x Null
189+snoc bs x = Node bs (ByteBuff x) Null
190+
191+-- | Append a `DataView` to the end of the `Builder`. *O(1)*
192+snoc_ :: Builder -> DataView -> Builder
193+snoc_ bs x = Node bs (ByteView x) Null
158194
159195-- | 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
196+length :: Builder -> ByteLength
197+length bldr = foldl (\b a -> b + len a) 0 bldr
198+ where
199+ len (ByteBuff ab) = AB.byteLength ab
200+ len (ByteView dv) = DV.byteLength dv
162201
163202-- | Build a single `ArrayBuffer` from a `Builder`. *O(n)*
164203execBuilder :: forall m. (MonadEffect m) => Builder -> m ArrayBuffer
165204execBuilder bldr = do
166− let buflen = foldl (\b a -> b + AB.byteLength a) 0 bldr
205+ let buflen = length bldr
167206 -- Allocate the final ArrayBuffer
168207 buf <- liftEffect $ AB.empty buflen
169208 -- Then copy each ArrayBuffer into the final ArrayBuffer.
@@ -172,11 +211,16 @@ execBuilder bldr = do
172211 newview <- liftEffect (AT.whole buf :: Effect Uint8Array)
173212 _ <- foldM
174213 ( \offset a -> do
175− aview <- liftEffect (AT.whole a :: Effect Uint8Array)
214+ aview <- liftEffect $ toUint8Array a
176215 _ <- liftEffect $ AT.setTyped newview (Just offset) aview
177− pure $ offset + AB.byteLength a
216+ pure $ offset + AT.byteLength aview
178217 ) 0 bldr
179218 pure buf
219+ where
220+ toUint8Array :: Bytes -> Effect Uint8Array
221+ toUint8Array (ByteBuff ab) = AT.whole ab
222+ toUint8Array (ByteView dv) =
223+ AT.part (DV.buffer dv) (DV.byteOffset dv) (DV.byteLength dv)
180224
181225-- | Build an `ArrayBuffer` with do-notation in any `MonadEffect`. *O(n)*
182226execPutM :: forall m. (MonadEffect m) => PutM m Unit -> m ArrayBuffer
@@ -212,6 +256,10 @@ subBuilder = lift <<< execWriterT
212256putArrayBuffer :: forall m. (MonadEffect m) => ArrayBuffer -> PutM m Unit
213257putArrayBuffer = tell <<< singleton
214258
259+-- | Append a `DataView` to the builder.
260+putDataView :: forall m. (MonadEffect m) => DataView -> PutM m Unit
261+putDataView = tell <<< singleton_
262+
215263-- | Serialize an 8-bit unsigned integer (byte) into a new `ArrayBuffer`.
216264encodeUint8 :: forall m. (MonadEffect m) => UInt -> m ArrayBuffer
217265encodeUint8 x = do
@@ -379,7 +427,7 @@ putFloat64le = putArrayBuffer <=< encodeFloat64le
379427-- left or right tree is a singleton. If that's not true, then in the
380428-- unlikely worst case `append` might be *O(n)*.
381429--
382−-- `Builder` is optimized what we consider to be normal usage, that is,
430+-- `Builder` is optimized for what we consider to be normal usage, that is,
383431-- `snoc`ing singleton elements to the end of the `Builder`.
384432--
385433-- If a Builder is built entirely by `snoc`ing, it will look like a
@@ -427,6 +475,7 @@ putFloat64le = putArrayBuffer <=< encodeFloat64le
427475--
428476-- One relatively cheap and simple performance improvement for this library would be to
429477-- remove the Null constructor of `Builder` and instead use Javascript nulls.
478+-- UPDATE Actually no, we need the Null constructor for the Monoid instance.
430479--
431480-- In the longer term, it might make sense to try to change the Builder so
432481-- that it works like the