git.iko.soy/rowtype-yoga/purescript-arraybuffer-builder
on master help sign in
Change name of `Bytes` to `DataBuff`.
commite09961e9160ffa0bd25e4bb858a4d34df2cedac3 authorJames Brock <jamesbrock@gmail.com> dateThu 22 Jul 2021, 19:42 (5 years ago) parenta7cd403
4 files changed+95−108
CHANGELOG.md+5 src/Data/ArrayBuffer/Builder.purs+9−10 src/Data/ArrayBuffer/Internal.purs+38−55 test/Main.purs+43−43
CHANGELOG.md+5
@@ -1,5 +1,10 @@
11# Changelog
22
3+## v2.1.0 2021-07-22
4+
5+Renamed `Data.ArrayBuffer.Internal.Bytes` and exported
6+it as `Data.ArrayBuffer.DataBuff`.
7+
38## v2.0.0 2021-07-19
49
510Build for PureScript v0.14.
src/Data/ArrayBuffer/Builder.purs+9−10
@@ -17,6 +17,7 @@ module Data.ArrayBuffer.Builder
1717, subBuilder
1818, putArrayBuffer
1919, putDataView
20+, putDataBuff
2021, putUint8
2122, putInt8
2223, putUint16be
@@ -35,17 +36,11 @@ module Data.ArrayBuffer.Builder
3536)
3637where
3738
38−import Data.ArrayBuffer.Builder.Internal
39− ( Builder
40− , (<>>)
41− , length
42− )
43−
44−import Data.ArrayBuffer.Builder.Internal as Internal
45−
4639import Prelude
4740
4841import 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
4944import Data.ArrayBuffer.Types (ArrayBuffer, DataView)
5045import Data.Float32 (Float32)
5146import Data.UInt (UInt)
@@ -94,11 +89,15 @@ subBuilder = lift <<< execWriterT
9489
9590-- | Append an `ArrayBuffer` to the builder.
9691putArrayBuffer :: forall m. (MonadEffect m) => ArrayBuffer -> PutM m Unit
97−putArrayBuffer = tell <<< Internal.singleton
92+putArrayBuffer = tell <<< Internal.singleton <<< Internal.Buff
9893
9994-- | Append a `DataView` to the builder.
10095putDataView :: forall m. (MonadEffect m) => DataView -> PutM m Unit
101−putDataView = tell <<< Internal.singleton_
96+putDataView = tell <<< Internal.singleton <<< Internal.View
97+
98+-- | Append either an `ArrayBuffer` or a `DataView` to the builder.
99+putDataBuff :: forall m. (MonadEffect m) => DataBuff -> PutM m Unit
100+putDataBuff = tell <<< Internal.singleton
102101
103102-- | Append an 8-bit unsigned integer (byte) to the builder.
104103putUint8 :: forall m. (MonadEffect m) => UInt -> PutM m Unit
src/Data/ArrayBuffer/Internal.purs+38−55
@@ -57,19 +57,20 @@
5757-- | __bytestring-tree-builder__
5858-- | which “according to the benchmarks … beats all the alternatives.”
5959-- | However, we
60−-- | haven't chosen this implementation because it's fast, we've chosen
61−-- | this implementation because it's simple.
62−-- | If someone wants to create a fast PureScript ArrayBuffer serialization
60+-- | haven’t chosen this implementation because it’s fast, we've chosen
61+-- | this implementation because it’s simple.
62+-- | If someone wants to create a fast PureScript `ArrayBuffer` serialization
6363-- | library, then they can benchmark against this one to prove that the new
6464-- | one is fast.
6565-- |
6666-- | One relatively cheap and simple performance improvement for this library would be to
67−-- | remove the Null constructor of `Builder` and instead use Javascript nulls.
68−-- | __UPDATE__ Actually no, we need the Null constructor for the Monoid instance.
67+-- | remove the `Null` constructor of `Builder` and instead use Javascript nulls?
6968-- |
7069-- | In the longer term, it might make sense to try to change the `Builder` so
7170-- | that it works like the
72−-- | https://hackage.haskell.org/package/bytestring/docs/Data-ByteString-Builder.html
71+-- | https://hackage.haskell.org/package/bytestring/docs/Data-ByteString-Builder.html .
72+-- | That’s the approach taken by
73+-- | https://pursuit.purescript.org/packages/purescript-dynamic-buffers .
7374-- |
7475-- | Here are some benchmarks of different Haskell ByteString builders
7576-- | https://github.com/haskell-perf/strict-bytestring-builders
@@ -77,27 +78,18 @@
7778-- | We've tried to design the API for this library with minimal assumptions,
7879-- | so that if we want to change the `Builder` implementation later then we can.
7980-- |
80−-- | Another way this could go:
81−-- | https://kodimensional.dev/posts/2019-03-25-comonadic-builders
82−-- |
83−-- | Our convention is that functions which add `DataView`s to
84−-- | the `Builder` are suffixed with underscore (`_`).
85−-- |
8681module Data.ArrayBuffer.Builder.Internal
8782( Builder(..)
8883, (<>>)
89−, Bytes(..)
84+, DataBuff(..)
9085, toView
9186, execBuilder
9287, length
9388, foldl
9489, foldM
9590, singleton
96−, singleton_
9791, cons
98−, cons_
9992, snoc
100−, snoc_
10193, encodeUint8
10294, encodeInt8
10395, encodeUint16be
@@ -127,6 +119,19 @@ import Data.UInt (UInt)
127119import Effect (Effect)
128120import Effect.Class (class MonadEffect, liftEffect)
129121
122+-- | For distinguishing between `ArrayBuffer` and `DataView`.
123+data DataBuff
124+ = Buff ArrayBuffer
125+ | View DataView
126+
127+-- Concerning DataViews.
128+-- https://v8.dev/blog/dataview
129+
130+ -- | View the contents of `DataBuff` as a `DataView`.
131+toView :: DataBuff -> DataView
132+toView (Buff ab) = DV.whole ab
133+toView (View dv) = dv
134+
130135-- | Monoidal builder for `ArrayBuffer`s.
131136-- |
132137-- | We can add two types of things to the `Builder`:
@@ -138,7 +143,7 @@ import Effect.Class (class MonadEffect, liftEffect)
138143-- | from some other `ArrayBuffer`, so that we don’t
139144-- | need an extra intermediate copy of the slice.
140145data Builder
141− = Node Builder Bytes Builder
146+ = Node Builder DataBuff Builder
142147 | Null
143148
144149-- | ### Left-associative `<>>` append operator
@@ -193,57 +198,35 @@ infixl 5 append as <>>
193198instance monoidBuilder :: Monoid Builder where
194199 mempty = Null
195200
196−-- | For distinguishing between `ArrayBuffer` and `DataView`.
197−data Bytes
198− = ByteBuff ArrayBuffer
199− | ByteView DataView
200−
201201-- | `Builder` is not an instance of `Foldable` because `Builder` is monomorphic.
202−foldl :: forall b. (b -> Bytes -> b) -> b -> Builder -> b
202+foldl :: forall b. (b -> DataBuff -> b) -> b -> Builder -> b
203203foldl _ a Null = a
204204foldl f a (Node l x r) = foldl f (f (foldl f a l) x) r
205205
206206-- | Monomorphic foldM copied from
207207-- | [`Data.Foldable.foldM`](https://pursuit.purescript.org/packages/purescript-foldable-traversable/4.1.1/docs/Data.Foldable#v:foldM)
208−foldM :: forall b m. (Monad m) => (b -> Bytes -> m b) -> b -> Builder -> m b
208+foldM :: forall b m. (Monad m) => (b -> DataBuff -> m b) -> b -> Builder -> m b
209209foldM f a0 = foldl (\ma b -> ma >>= flip f b) (pure a0)
210210
211−-- | Construct a `Builder` with a single `ArrayBuffer`. *O(1)*
212−singleton :: ArrayBuffer -> Builder
213−singleton buf = Node Null (ByteBuff buf) Null
214−
215−-- | Construct a `Builder` with a single `DataView`. *O(1)*
216−singleton_ :: DataView -> Builder
217−singleton_ buf = Node Null (ByteView buf) Null
211+-- | Construct a `Builder` with a single `DataBuff`. *O(1)*
212+singleton :: DataBuff -> Builder
213+singleton buf = Node Null buf Null
218214
219215-- | Calculate the total byte length of the `Builder`, without actually
220216-- | building it yet. *O(n)*
221217length :: Builder -> ByteLength
222218length bldr = foldl (\b a -> b + len a) 0 bldr
223219 where
224− len (ByteBuff ab) = AB.byteLength ab
225− len (ByteView dv) = DV.byteLength dv
226−
227− -- | View the contents of `Bytes` as a `DataView`.
228−toView :: Bytes -> DataView
229−toView (ByteBuff ab) = DV.whole ab
230−toView (ByteView dv) = dv
231−
232−-- | Prepend an `ArrayBuffer` to the beginning of the `Builder`. *O(1)*
233−cons :: ArrayBuffer -> Builder -> Builder
234−cons x bs = Node Null (ByteBuff x) bs
235−
236−-- | Prepend a `DataView` to the beginning of the `Builder`. *O(1)*
237−cons_ :: DataView -> Builder -> Builder
238−cons_ x bs = Node Null (ByteView x) bs
220+ len (Buff ab) = AB.byteLength ab
221+ len (View dv) = DV.byteLength dv
239222
240−-- | Append an `ArrayBuffer` to the end of the `Builder`. *O(1)*
241−snoc :: Builder -> ArrayBuffer -> Builder
242−snoc bs x = Node bs (ByteBuff x) Null
223+-- | Prepend a `DataBuff` to the beginning of the `Builder`. *O(1)*
224+cons :: DataBuff -> Builder -> Builder
225+cons x bs = Node Null x bs
243226
244−-- | Append a `DataView` to the end of the `Builder`. *O(1)*
245−snoc_ :: Builder -> DataView -> Builder
246−snoc_ bs x = Node bs (ByteView x) Null
227+-- | Append a `DataBuff` to the end of the `Builder`. *O(1)*
228+snoc :: Builder -> DataBuff -> Builder
229+snoc bs x = Node bs x Null
247230
248231-- | Build a single `ArrayBuffer` from a `Builder`. *O(n)*
249232execBuilder :: forall m. (MonadEffect m) => Builder -> m ArrayBuffer
@@ -263,9 +246,9 @@ execBuilder bldr = do
263246 ) 0 bldr
264247 pure buf
265248 where
266− toUint8Array :: Bytes -> Effect Uint8Array
267− toUint8Array (ByteBuff ab) = AT.whole ab
268− toUint8Array (ByteView dv) =
249+ toUint8Array :: DataBuff -> Effect Uint8Array
250+ toUint8Array (Buff ab) = AT.whole ab
251+ toUint8Array (View dv) =
269252 AT.part (DV.buffer dv) (DV.byteOffset dv) (DV.byteLength dv)
270253
271254-- | Serialize an 8-bit unsigned integer (byte) into a new `ArrayBuffer`.
test/Main.purs+43−43
@@ -3,7 +3,7 @@ module Test.Main where
33import Data.ArrayBuffer.Builder
44
55import Control.Monad.Writer.Trans (tell)
6−import Data.ArrayBuffer.Builder.Internal (cons, encodeInt8, execBuilder, length, singleton, singleton_, (<>>))
6+import Data.ArrayBuffer.Builder.Internal (cons, encodeInt8, execBuilder, length, singleton, (<>>))
77import Data.ArrayBuffer.DataView as DV
88import Data.ArrayBuffer.Typed as AT
99import Data.ArrayBuffer.Types (ArrayBuffer, Uint8Array)
@@ -41,55 +41,55 @@ main = do
4141 putInt32le 3
4242
4343 buildTest "Test 4" [1,2,3,4] $ do
44− b1 <- encodeInt8 1
45− b2 <- encodeInt8 2
46− b3 <- encodeInt8 3
47− b4 <- encodeInt8 4
44+ b1 <- Buff <$> encodeInt8 1
45+ b2 <- Buff <$> encodeInt8 2
46+ b3 <- Buff <$> encodeInt8 3
47+ b4 <- Buff <$> encodeInt8 4
4848 pure $ singleton b1 <> singleton b2 <> singleton b3 <> singleton b4
4949
5050 buildTest "Test 5" [1,2,3,4] $ do
51− b1 <- encodeInt8 1
52− b2 <- encodeInt8 2
53− b3 <- encodeInt8 3
54− b4 <- encodeInt8 4
51+ b1 <- Buff <$> encodeInt8 1
52+ b2 <- Buff <$> encodeInt8 2
53+ b3 <- Buff <$> encodeInt8 3
54+ b4 <- Buff <$> encodeInt8 4
5555 pure $ singleton b1 <>> singleton b2 <>> singleton b3 <>> singleton b4
5656
5757 buildTest "Test 6" [1,2,3,4] $ do
58− b1 <- encodeInt8 1
59− b2 <- encodeInt8 2
60− b3 <- encodeInt8 3
61− b4 <- encodeInt8 4
58+ b1 <- Buff <$> encodeInt8 1
59+ b2 <- Buff <$> encodeInt8 2
60+ b3 <- Buff <$> encodeInt8 3
61+ b4 <- Buff <$> encodeInt8 4
6262 pure $ singleton b1 <> (singleton b2 <> singleton b3) <> singleton b4
6363
6464 buildTest "Test 6" [1,2,3,4,5,6,7,8] $ do
65− b1 <- encodeInt8 1
66− b2 <- encodeInt8 2
67− b3 <- encodeInt8 3
68− b4 <- encodeInt8 4
69− b5 <- encodeInt8 5
70− b6 <- encodeInt8 6
71− b7 <- encodeInt8 7
72− b8 <- encodeInt8 8
65+ b1 <- Buff <$> encodeInt8 1
66+ b2 <- Buff <$> encodeInt8 2
67+ b3 <- Buff <$> encodeInt8 3
68+ b4 <- Buff <$> encodeInt8 4
69+ b5 <- Buff <$> encodeInt8 5
70+ b6 <- Buff <$> encodeInt8 6
71+ b7 <- Buff <$> encodeInt8 7
72+ b8 <- Buff <$> encodeInt8 8
7373 pure $ singleton b1 <> (singleton b2 <> singleton b3) <> singleton b4
7474 <> singleton b5 <> (singleton b6 <> singleton b7) <> singleton b8
7575
7676 buildTest "Test 7" [1,2,3,4,5,6,7,8] $ do
77− b1 <- encodeInt8 1
78− b2 <- encodeInt8 2
79− b3 <- encodeInt8 3
80− b4 <- encodeInt8 4
81− b5 <- encodeInt8 5
82− b6 <- encodeInt8 6
83− b7 <- encodeInt8 7
84− b8 <- encodeInt8 8
77+ b1 <- Buff <$> encodeInt8 1
78+ b2 <- Buff <$> encodeInt8 2
79+ b3 <- Buff <$> encodeInt8 3
80+ b4 <- Buff <$> encodeInt8 4
81+ b5 <- Buff <$> encodeInt8 5
82+ b6 <- Buff <$> encodeInt8 6
83+ b7 <- Buff <$> encodeInt8 7
84+ b8 <- Buff <$> encodeInt8 8
8585 pure $ ((singleton b1 <> singleton b2) <> (singleton b3 <> singleton b4))
8686 <> ((singleton b5 <> singleton b6) <> (singleton b7 <> singleton b8))
8787
8888 buildTest "Test 8" [1,2,3,4] $ do
89− b1 <- encodeInt8 1
90− b2 <- encodeInt8 2
91− b3 <- encodeInt8 3
92− b4 <- encodeInt8 4
89+ b1 <- Buff <$> encodeInt8 1
90+ b2 <- Buff <$> encodeInt8 2
91+ b3 <- Buff <$> encodeInt8 3
92+ b4 <- Buff <$> encodeInt8 4
9393 pure $ (cons b1 (singleton b2)) <> (cons b3 (singleton b4))
9494
9595 putTest "Test 9" [1,2,2,3,4] $ do
@@ -102,14 +102,14 @@ main = do
102102 tell x
103103
104104 buildTest "Test 10" [1,2,3,4,5,6,7,8] $ do
105− b1 <- DV.whole <$> encodeInt8 1
106− b2 <- DV.whole <$> encodeInt8 2
107− b3 <- DV.whole <$> encodeInt8 3
108− b4 <- DV.whole <$> encodeInt8 4
109− b5 <- DV.whole <$> encodeInt8 5
110− b6 <- DV.whole <$> encodeInt8 6
111− b7 <- DV.whole <$> encodeInt8 7
112− b8 <- DV.whole <$> encodeInt8 8
113− pure $ ((singleton_ b1 <> singleton_ b2) <> (singleton_ b3 <> singleton_ b4))
114− <> ((singleton_ b5 <> singleton_ b6) <> (singleton_ b7 <> singleton_ b8))
105+ b1 <- View <$> DV.whole <$> encodeInt8 1
106+ b2 <- View <$> DV.whole <$> encodeInt8 2
107+ b3 <- View <$> DV.whole <$> encodeInt8 3
108+ b4 <- View <$> DV.whole <$> encodeInt8 4
109+ b5 <- View <$> DV.whole <$> encodeInt8 5
110+ b6 <- View <$> DV.whole <$> encodeInt8 6
111+ b7 <- View <$> DV.whole <$> encodeInt8 7
112+ b8 <- View <$> DV.whole <$> encodeInt8 8
113+ pure $ ((singleton b1 <> singleton b2) <> (singleton b3 <> singleton b4))
114+ <> ((singleton b5 <> singleton b6) <> (singleton b7 <> singleton b8))
115115