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

Use ShortByteString for OneEraHash and ConvertRawHash #2266

Merged
merged 1 commit into from
Jul 16, 2020

Conversation

mrBliss
Copy link
Contributor

@mrBliss mrBliss commented Jun 15, 2020

Since the Byron and Shelley hash types are ShortByteStrings under the hood,
switch over the representation of OneEraHash from Strict.ByteString to
ShortByteString too.

To avoid the intermediary Strict.ByteString in the conversion, add
toShortRawHash and fromShortRawHash to ConvertRawHash.

This avoids the memory overhead that a regular strict ByteString has.
According to the Haddocks of ShortByteString, using a
ByteString (unshared) for a 32 byte hash (which is the case for Byron and
Shelley) requires 72 + 32 bytes, but a ShortByteString requires only 32 + 32
bytes. This can make a noticeable difference in memory usage as we're keeping
lots of hashes in memory:

  • O(k) in the in-memory indices of the VolatileDB (hash and prev hash)
  • O(cached epochs * epoch size) in the secondary index caches of the
    ImmutableDB
  • ...

@mrBliss mrBliss added the consensus issues related to ouroboros-consensus label Jun 15, 2020
@mrBliss mrBliss requested a review from edsko June 15, 2020 17:07
@mrBliss mrBliss force-pushed the mrBliss/short-bytestring branch from 86748c6 to f5950f3 Compare June 15, 2020 17:08
Copy link
Contributor Author

@mrBliss mrBliss left a comment

Choose a reason for hiding this comment

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

Currently a draft, as we currently require going through a regular ByteString for both Byron and Shelley (see my inline comments).

If/when we decide to switch to ShortByteStrings, it would be good to measure the actual difference in memory usage.

UPDATE: both Byron's and Shelley's hash type now uses a ShortByteString under the hood and expose the necessary conversions (avoiding going through an intermediary Strict.ByteString).

@mrBliss mrBliss force-pushed the mrBliss/short-bytestring branch from f5950f3 to 12820e3 Compare June 24, 2020 08:52
@mrBliss mrBliss mentioned this pull request Jul 1, 2020
@mrBliss mrBliss force-pushed the mrBliss/short-bytestring branch from 12820e3 to dea8236 Compare July 16, 2020 10:22
@mrBliss mrBliss changed the title Use ShortByteString for OneEraHash Use ShortByteString for OneEraHash and ConvertRawHash Jul 16, 2020
@mrBliss mrBliss marked this pull request as ready for review July 16, 2020 10:25
Since the Byron and Shelley hash types are `ShortByteString`s under the hood,
switch over the representation of `OneEraHash` from `Strict.ByteString` to
`ShortByteString` too.

To avoid the intermediary `Strict.ByteString` in the conversion, add
`toShortRawHash` and `fromShortRawHash` to `ConvertRawHash`.

This avoids the memory overhead that a regular strict `ByteString` has.
According to the [Haddocks of `ShortByteString`][SBS], using a
`ByteString` (unshared) for a 32 byte hash (which is the case for Byron and
Shelley) requires 72 + 32 bytes, but a `ShortByteString` requires only 32 + 32
bytes. This can make a noticeable difference in memory usage as we're keeping
lots of hashes in memory:

* O(k) in the in-memory indices of the VolatileDB (hash and prev hash)
* O(cached epochs * epoch size) in the secondary index caches of the
  ImmutableDB
* ...

[SBS]: http://hackage.haskell.org/package/bytestring-0.10.10.0/docs/Data-ByteString-Short.html#g:2
@mrBliss mrBliss force-pushed the mrBliss/short-bytestring branch from dea8236 to 458548a Compare July 16, 2020 10:41
@edsko
Copy link
Contributor

edsko commented Jul 16, 2020

Glad to have this major performance penalty gone finally 😏

@mrBliss
Copy link
Contributor Author

mrBliss commented Jul 16, 2020

bors ping

@iohk-bors
Copy link
Contributor

iohk-bors bot commented Jul 16, 2020

pong

@mrBliss
Copy link
Contributor Author

mrBliss commented Jul 16, 2020

bors merge

@iohk-bors
Copy link
Contributor

iohk-bors bot commented Jul 16, 2020

@iohk-bors iohk-bors bot merged commit f0edc55 into master Jul 16, 2020
@iohk-bors iohk-bors bot deleted the mrBliss/short-bytestring branch July 16, 2020 12:16
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
consensus issues related to ouroboros-consensus
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants