Skip to content

Conversation

@kderme
Copy link
Contributor

@kderme kderme commented Feb 21, 2022

Optimizations mentioned in #1065
Wip because we want #1058 first

This caches the MultiAssetId in memory to avoid unnecessary queries. The create multiasssets. benchmark which focuses on crteating multiassets outputs, shows a remarkable speedup.
without caching:

benchmarking cardano-chain/bench/create multiasssets./1 block
mean                 986.6 ms   (948.8 ms .. 1.023 s)
std dev              63.15 ms   (0.0 s .. 63.91 ms)

benchmarking cardano-chain/bench/create multiasssets./10 blocks
mean                 8.438 s    (8.185 s .. 8.581 s)
std dev              224.6 ms   (0.0 s .. 248.2 ms)

benchmarking cardano-chain/bench/create multiasssets./100 blocks
mean                 113.5 s    (113.5 s .. 113.5 s)
std dev              0.0 s      (0.0 s .. 0.0 s)

with caching:

benchmarking cardano-chain/bench/create multiasssets./1 block
mean                 525.4 ms   (524.1 ms .. 526.3 ms)
std dev              1.286 ms   (0.0 s .. 1.476 ms)

benchmarking cardano-chain/bench/create multiasssets./10 blocks
mean                 2.784 s    (2.711 s .. 2.820 s)
std dev              63.34 ms   (0.0 s .. 65.33 ms)

benchmarking cardano-chain/bench/create multiasssets./100 blocks
mean                 25.71 s    (25.71 s .. 25.71 s)
std dev              0.0 s      (0.0 s .. 0.0 s)

This benchmark creates 7000 ma_tx_out entries per block, for 1, 10 and 100 blocks. All ma_tx_out created (700000 in the 100 blocks benchmark) reference 700 distinct multiassets.

Mainnet has hundrends of millions of ma_tx_out entries, so I expect some noticeable speedup there. This remains to be checked.

Caching can be improved with a LRU cache using https://hackage.haskell.org/package/psqueues to avoid using too much memory for caching all entries.


import Database.Persist.Postgresql (SqlBackend)

newtype Cache = MACache (IORef (Map (ByteString, AssetName) DB.MultiAssetId))
Copy link
Contributor

@erikd erikd Feb 28, 2022

Choose a reason for hiding this comment

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

Why are we using (ByteString, AssetName) tuple there instead of just using the asset fingerprint?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

The fingerprint needs to be computed by hashing these two, so it takes some time.

@kderme
Copy link
Contributor Author

kderme commented Feb 28, 2022

A more general caching approach is here #1074

@erikd
Copy link
Contributor

erikd commented Apr 28, 2022

PR #1094 has been merged and it supercedes #1074 .

@erikd erikd closed this Apr 28, 2022
@kderme kderme deleted the kderme/cache-multiasset branch July 4, 2022 12:52
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.

3 participants