Skip to content
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

Add buildMessageDelimited: size-delimited streams of Messages #102

Merged
merged 1 commit into from
Aug 15, 2017

Conversation

shlevy
Copy link
Contributor

@shlevy shlevy commented Jul 29, 2017

No description provided.

@googlebot
Copy link

Thanks for your pull request. It looks like this may be your first contribution to a Google open source project. Before we can look at your pull request, you'll need to sign a Contributor License Agreement (CLA).

📝 Please visit https://cla.developers.google.com/ to sign.

Once you've signed, please reply here (e.g. I signed it!) and we'll verify. Thanks.


  • If you've already signed a CLA, it's possible we don't have your GitHub username or you're using a different email address. Check your existing CLA data and verify that your email is set on your git commits.
  • If your company signed a CLA, they designated a Point of Contact who decides which employees are authorized to participate. You may need to contact the Point of Contact for your company and ask to be added to the group of authorized contributors. If you don't know who your Point of Contact is, direct the project maintainer to go/cla#troubleshoot.
  • In order to pass this check, please resolve this problem and have the pull request author add another comment and the bot will run again.

@shlevy
Copy link
Contributor Author

shlevy commented Jul 29, 2017

Note: This currently typechecks but is not tested, testing locally next.

@judah
Copy link
Collaborator

judah commented Jul 31, 2017

Note that #61 has some prior discussion of this feature.

@shlevy
Copy link
Contributor Author

shlevy commented Aug 14, 2017

Tested to work via https://github.com/LukeHoersten/prometheus/pull/7

@shlevy
Copy link
Contributor Author

shlevy commented Aug 14, 2017

I signed it

mempty = SizedBuilder 0 mempty
mappend = (<>)

putVarInt :: Word64 -> SizedBuilder
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy this from Data.ProtoLens.Encoding.Bytes?

| otherwise = Builder.word8 (fromIntegral $ n .&. 127 .|. 128)
<> putVarInt (n `shiftR` 7)
-- | A wrapper around a 'Builder' that keeps track of its size.
data SizedBuilder = SizedBuilder
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Would it be simpler to just call toLazyByteString (or toStrict . toLazyByteString) on a builder and then get its size?

It's hard for me to reason about the performance tradeoffs of this change. We have some benchmarks in this repo in proto-lens-benchmarks; I'd be reluctant to refactor how the building code works without seeing a noticeable improvement in those benchmarks (or a new one). I could anticipate slowdowns caused by a space leak in the _size parameter (it's hard for me to reason about off the top of my head), or due to the redundant additions/bookkeeping, or due to confusing GHC's optimizer.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I would wary of the lazy _size as well. If we want to maximize encoding speed, I would expect that the approach used by http://hackage.haskell.org/package/cborg with an intermediate custom tailored token stream and a hot encoder loop would perform best. See: https://youtu.be/1WrSPSYhE6o?t=28m23s

putVarInt' n
| n < 128 = Builder.word8 (fromIntegral n)
| otherwise = Builder.word8 (fromIntegral $ n .&. 127 .|. 128)
<> putVarInt' (n `shiftR` 7)

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@judah @shlevy note that you could use http://hackage.haskell.org/package/bytestring-0.10.8.2/docs/Data-ByteString-Builder-Prim.html to avoid the recursion completely and perform the check whether the buffer is large enough in a single step. See the encodeCharUtf8HtmlEscaped example for inspiration.

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Recalling the times when I was working on the bytestring builder, I'd expect about an order-of-magnitude speedup for encoding varInts with this change.

@shlevy
Copy link
Contributor Author

shlevy commented Aug 15, 2017

@judah removed premature optimization

@judah judah merged commit e5b02db into google:master Aug 15, 2017
tathougies pushed a commit to tathougies/proto-lens that referenced this pull request Aug 24, 2017
blackgnezdo pushed a commit that referenced this pull request Aug 17, 2018
Added testReluGrad, testReluGradGrad and testFillGrad
ylecornec pushed a commit to ylecornec/proto-lens that referenced this pull request Feb 19, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants