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

feat: chain: make chain tipset fetching 1000x faster #10423

Merged
merged 1 commit into from
Mar 9, 2023
Merged

Conversation

arajasek
Copy link
Contributor

@arajasek arajasek commented Mar 8, 2023

Related Issues

@jennijuju flagged that we need faster fetching of tipsets by height for Eth tooling support.

Proposed Changes

  • Increase the skipcache size to 2^20 entries
  • Drop the tipset itself from the skipcache entries. This is a lot of information to hold in memory that we don't actually need.
  • Drop the parentHeight from the skipcache entries. This is just 8 bytes, but I don't think we need it.
  • With that, we can easily rely on the skip cache to load 20 million epochs (so good until 2040).
  • Each cache entry itself is ~308 bytes (an int64 + a TSK, using ~5 CIDs per TSK, which is aggressive), which means the total cache size when we get to 20M epochs will be ~0.3 GiB.

Additional Info

Checklist

Before you mark the PR ready for review, please make sure that:

  • Commits have a clear commit message.
  • PR title is in the form of of <PR type>: <area>: <change being made>
    • example: fix: mempool: Introduce a cache for valid signatures
    • PR type: fix, feat, build, chore, ci, docs, perf, refactor, revert, style, test
    • area, e.g. api, chain, state, market, mempool, multisig, networking, paych, proving, sealing, wallet, deps
  • New features have usage guidelines and / or documentation updates in
  • Tests exist for new functionality or change in behavior
  • CI is green

@arajasek arajasek requested a review from a team as a code owner March 8, 2023 16:59
@arajasek
Copy link
Contributor Author

arajasek commented Mar 8, 2023

Timing results on mainnet:

aayush@aayush-desktop ~/p/lotus (asr/fast-index)> time ./lotus chain list --count=3 --height=200
198: (Aug 24 19:39:00) [ bafy2bzacear4yqnylpchlufj5nccvdv56zi7655jtich2chbk2w7oci4fv4b6: f01002, ]
199: (Aug 24 19:39:30) [ bafy2bzacecrlj6vujyqwm5daygo27ksaibr37wyklcby6ihb4kiejlfludzyi: f01002, ]
200: (Aug 24 19:40:00) [ bafy2bzacedgt32ubhpx4hu3hrbu663djcykuplt4jc5mnyii2wlzx5ovsj7cs: f01000,bafy2bzaceb6ksh75v3m6dydsutfrcaoe542d44rotznniq5poi2bfirthl6se: f01002, ]

________________________________________________________
Executed in  342.67 secs      fish           external
   usr time  663.49 millis  572.00 micros  662.92 millis
   sys time   60.26 millis    0.00 micros   60.26 millis

aayush@aayush-desktop ~/p/lotus (asr/fast-index)> time ./lotus chain list --count=3 --height=200
198: (Aug 24 19:39:00) [ bafy2bzacear4yqnylpchlufj5nccvdv56zi7655jtich2chbk2w7oci4fv4b6: f01002, ]
199: (Aug 24 19:39:30) [ bafy2bzacecrlj6vujyqwm5daygo27ksaibr37wyklcby6ihb4kiejlfludzyi: f01002, ]
200: (Aug 24 19:40:00) [ bafy2bzacedgt32ubhpx4hu3hrbu663djcykuplt4jc5mnyii2wlzx5ovsj7cs: f01000,bafy2bzaceb6ksh75v3m6dydsutfrcaoe542d44rotznniq5poi2bfirthl6se: f01002, ]

________________________________________________________
Executed in  287.58 millis    fish           external
   usr time  475.32 millis  345.00 micros  474.98 millis
   sys time   37.61 millis  115.00 micros   37.50 millis

Warming the cache takes ~6 minutes, subsequent queries are less than a second.

@arajasek arajasek changed the title feat: make chain tipset fetching 1000x faster feat: chain: make chain tipset fetching 1000x faster Mar 8, 2023
Copy link
Member

@Stebalien Stebalien left a comment

Choose a reason for hiding this comment

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

LGTM. A couple of comments:

  1. I'd just drop the ARC cache and just use a map. We're likely wasting a lot of time updating the "recency" metrics on that cache every time we use it (and the memory locality is likely really bad).
  2. I'm somewhat concerned that a single query might take 6 minutes (will timeout on most services). We might want some form of background job to warm the cache. But (a) that can happen later and (b) service providers can likely work around this issue themselves.

@Stebalien
Copy link
Member

If this isn't enough, we can consider implementing a real skiplist (with multiple levels).

@arajasek
Copy link
Contributor Author

arajasek commented Mar 9, 2023

@Stebalien Thanks for the review and the suggestions!

I switched to using a map. It appears to have made the warmup slower, which is somewhat unexpected, but subsequent lookups faster. I think that's still a win, in addition to the overall simplicity, so I'm gonna push that up.

return &ChainIndex{
skipCache: sc,
indexCache: make(map[types.TipSetKey]*lbEntry),
Copy link
Member

Choose a reason for hiding this comment

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

It's slower because you're not initializing the map to DefaultChainIndexCacheSize.

Copy link
Member

Choose a reason for hiding this comment

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

Also note: I think you can just use lbEntry by value to save some allocations which should speed things up even more.

@arajasek arajasek force-pushed the asr/fast-index branch 2 times, most recently from fe169b3 to d53878b Compare March 9, 2023 17:21
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.

2 participants