-
Notifications
You must be signed in to change notification settings - Fork 5
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
encodeVarint64 performance improvement preallocate ArrayBuffer #31
Comments
case n of
_ | n < 128 -> do
Builder.putUint8 n
_ | n < 16384 -> do
buf <- ArrayBuffer.empty 2
dv <- DataView.whole buf
DataView.setInt8 dv 0 $ contGroup $ n `and` u0x7F
DataView.setInt8 dv 1 $ (n `zsh` 7) `and` u0x7F
Builder.putArrayBuffer buf
_ | n < 2097152 -> do |
Just for funWhat if we had a set of builder primitives which returned
type Putter :: Tuple ByteSize (ByteOffset -> ArrayBuffer -> Effect Unit)
putInt8 :: Int -> Putter
putStringUTF8 :: String -> Putter
putFloat64 :: Number -> Putter Then we could build an
Of course we would have to allocate all of these temporary Maybe there is a way to carry the Or something like this monoidal append? <> :: Putter -> Putter -> Putter
Tuple sl wl <> Tuple sr wr = Tuple (sl + sr) (\o ab -> wl o ab *> wr (o + sl) ab) The thing is that, in cases like encoding a UTF8 string, if you want to know the required size of the encoded string, you have to walk the whole string and do the encoding in order to calculate the size. Won't it always be faster to just write the encoding to a temporary buffer while you're at it, and then copy that temporary buffer later? I guess in the case of encoding a UTF8 string we could encode the temporary buffer and enclose it with the writer function which, when called, just memcpys the temporary buffer. I'm not saying that's a good idea. But it's a thing we could do. |
Just for funWhat we want to do is read a https://hackage.haskell.org/package/codec-0.2.1/docs/Control-Monad-Codec.html |
In
encodeVarint64
, instead of creating a separateArrayBuffer
for each byte, we could preallocate anArrayBuffer
with the right number of bytes for the entire varint by examining its value. And the same forencodeVarint32
.https://github.com/xc-jp/purescript-protobuf/blob/b4ddb67b72b4a3db8b5e5f6ec046182a802e18aa/src/Protobuf/Encode.purs#L311-L321
The text was updated successfully, but these errors were encountered: