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

Rewrite LedgerDB in-memory representation #1021

Merged
merged 2 commits into from
Sep 13, 2019
Merged

Conversation

edsko
Copy link
Contributor

@edsko edsko commented Sep 12, 2019

I rewrote the in-memory representation of the ledger DB. I just tested it against the Byron proxy running in profiling mode. With the old version of the Ledger DB, the top of the time profile looks like

ledgerDbReapply        Ouroboros.Storage.LedgerDB.InMemory     src/Ouroboros/Storage/LedgerDB/InMemory.hs:361:1-79               15.2   27.9
copyAndFreeze          Data.ByteArray.Methods                  Data/ByteArray/Methods.hs:(237,1)-(240,21)                         6.7    1.6
ledgerDbPush           Ouroboros.Storage.LedgerDB.InMemory     src/Ouroboros/Storage/LedgerDB/InMemory.hs:(332,1)-(356,11)        6.5   21.3
==                     Cardano.Chain.Common.KeyHash            src/Cardano/Chain/Common/KeyHash.hs:31:16-17                       4.3    7.0
ref                    Ouroboros.Storage.LedgerDB.InMemory     src/Ouroboros/Storage/LedgerDB/InMemory.hs:(122,1)-(123,17)        4.2    0.0

With the new version:

==                     Cardano.Chain.Common.KeyHash   src/Cardano/Chain/Common/KeyHash.hs:31:16-17             29.5   47.3
copyAndFreeze          Data.ByteArray.Methods         Data/ByteArray/Methods.hs:(237,1)-(240,21)                3.3    1.9
compare                Cardano.Chain.Common.KeyHash   src/Cardano/Chain/Common/KeyHash.hs:32:16-18              2.9    6.6
lookup                 Data.Map.Internal              Data/Map/Internal.hs:(561,1)-(567,18)                     2.8    3.2
deserialiseIncremental Codec.CBOR.Read                src/Codec/CBOR/Read.hs:(165,1)-(167,46)                   2.8    1.3

where the Ledger DB has disappeared entirely from the profile.

That top entry, a whopping 30% of time and 48% of memory allocation spent in (==) from Cardano.Chain.Common.KeyHash seems like it might benefit from a closer look also!

The heap profile with the existing implementation:

cardano-byron-proxy

And with the new implementation:

cardano-byron-proxy

I updated the tests; the hardest part was modifying the tests to figure out which snapshots we store (that makes sense, that is precisely where the new implementation provides weaker guarantees than the old version), and then ran it over 1,000,000 tests, all of which passed.

@edsko edsko requested a review from nfrisby September 12, 2019 14:27
@edsko edsko added the consensus issues related to ouroboros-consensus label Sep 12, 2019
@edsko
Copy link
Contributor Author

edsko commented Sep 12, 2019

Reviewed the design and many of the claims with @brunjlar . @nfrisby will do a review of the implementation, but with the knowledge that the tests pass.

Copy link
Contributor

@nfrisby nfrisby left a comment

Choose a reason for hiding this comment

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

Approved. I've made some comments along the lines of typos and e/amendments in comments, and some dead-"code" removal.

I didn't comb through the bulk of the new logic (in Ouroboros.Storage.LedgerDB.InMemory), since I'm unfamiliar there. However, the overview comments are good, the diff in the tests looks good, and Edsko said he'd already reviewed the changes with Lars.

@edsko
Copy link
Contributor Author

edsko commented Sep 13, 2019

bors r+

iohk-bors bot added a commit that referenced this pull request Sep 13, 2019
1021: Rewrite LedgerDB in-memory representation r=edsko a=edsko

I rewrote the in-memory representation of the ledger DB. I just tested it against the Byron proxy running in profiling mode. With the old version of the Ledger DB, the top of the time profile looks like

```
ledgerDbReapply        Ouroboros.Storage.LedgerDB.InMemory     src/Ouroboros/Storage/LedgerDB/InMemory.hs:361:1-79               15.2   27.9
copyAndFreeze          Data.ByteArray.Methods                  Data/ByteArray/Methods.hs:(237,1)-(240,21)                         6.7    1.6
ledgerDbPush           Ouroboros.Storage.LedgerDB.InMemory     src/Ouroboros/Storage/LedgerDB/InMemory.hs:(332,1)-(356,11)        6.5   21.3
==                     Cardano.Chain.Common.KeyHash            src/Cardano/Chain/Common/KeyHash.hs:31:16-17                       4.3    7.0
ref                    Ouroboros.Storage.LedgerDB.InMemory     src/Ouroboros/Storage/LedgerDB/InMemory.hs:(122,1)-(123,17)        4.2    0.0
```

With the new version:

```
==                     Cardano.Chain.Common.KeyHash   src/Cardano/Chain/Common/KeyHash.hs:31:16-17             29.5   47.3
copyAndFreeze          Data.ByteArray.Methods         Data/ByteArray/Methods.hs:(237,1)-(240,21)                3.3    1.9
compare                Cardano.Chain.Common.KeyHash   src/Cardano/Chain/Common/KeyHash.hs:32:16-18              2.9    6.6
lookup                 Data.Map.Internal              Data/Map/Internal.hs:(561,1)-(567,18)                     2.8    3.2
deserialiseIncremental Codec.CBOR.Read                src/Codec/CBOR/Read.hs:(165,1)-(167,46)                   2.8    1.3
```

where the Ledger DB has disappeared _entirely_ from the profile. 

That top entry, a whopping 30% of time and 48% of memory allocation spent in `(==)` from `Cardano.Chain.Common.KeyHash` seems like it might benefit from a closer look also!

The heap profile with the existing implementation:

![cardano-byron-proxy](https://user-images.githubusercontent.com/935288/64791691-5e83d280-d578-11e9-84bc-bc27186a1905.png)

And with the new implementation:

![cardano-byron-proxy](https://user-images.githubusercontent.com/935288/64791703-63e11d00-d578-11e9-8c00-cb3520bd90cc.png)

I updated the tests; the hardest part was modifying the tests to figure out which snapshots we store (that makes sense, that is precisely where the new implementation provides weaker guarantees than the old version), and then ran it over 1,000,000 tests, all of which passed.

Co-authored-by: Edsko de Vries <[email protected]>
@iohk-bors
Copy link
Contributor

iohk-bors bot commented Sep 13, 2019

@iohk-bors iohk-bors bot merged commit ba0f730 into master Sep 13, 2019
@iohk-bors iohk-bors bot deleted the edsko/rewrite-ledgerDB branch September 13, 2019 08:27
iohk-bors bot added a commit to input-output-hk/cardano-byron-proxy that referenced this pull request Sep 13, 2019
43: Update for rewrite of Ledger DB r=avieth a=edsko

This updates the Byron proxy for the rewrite of the Ledger DB in-memory representation (IntersectMBO/ouroboros-network#1021). Note however that the Byron proxy does not currently build against the latest `ouroboros-network` repo master:

```
src/exec/Shelley.hs:48:66: error:
    Not in scope: type constructor or class 'AnyResponderApp'
   |
48 | type ResponderVersions = Versions NodeToNodeVersion DictVersion (AnyResponderApp Peer NodeToNodeProtocols IO Lazy.ByteString)
   |                                                                  ^^^^^^^^^^^^^^^
cabal: Failed to build exe:cardano-byron-proxy from
cardano-byron-proxy-0.1.0.0.
```

Not sure where that is coming from.

Draft PR because we should also update the hash (I tested with a local branch that rebases my Ledger DB patch against the version of `ouroboros-network` that the proxy currently builds against).

Co-authored-by: Edsko de Vries <[email protected]>
Co-authored-by: Alexander Vieth <[email protected]>
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