-
Notifications
You must be signed in to change notification settings - Fork 196
fix: add cache to speed up fn tipset_by_height
#5944
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
Changes from 4 commits
f0fdbf6
0500263
8fb563f
518014f
1e0a4da
b49d29b
54c67d6
3c5cc35
cbf06fc
c289198
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -1,6 +1,7 @@ | ||
| // Copyright 2019-2025 ChainSafe Systems | ||
| // SPDX-License-Identifier: Apache-2.0, MIT | ||
|
|
||
| use std::sync::LazyLock; | ||
| use std::{num::NonZeroUsize, sync::Arc}; | ||
|
|
||
| use crate::beacon::{BeaconEntry, IGNORE_DRAND_VAR}; | ||
|
|
@@ -13,6 +14,7 @@ use crate::utils::misc::env::is_env_truthy; | |
| use fvm_ipld_blockstore::Blockstore; | ||
| use itertools::Itertools; | ||
| use nonzero_ext::nonzero; | ||
| use num::Integer; | ||
|
|
||
| const DEFAULT_TIPSET_CACHE_SIZE: NonZeroUsize = nonzero!(131072_usize); | ||
|
|
||
|
|
@@ -120,6 +122,23 @@ impl<DB: Blockstore> ChainIndex<DB> { | |
| from: Arc<Tipset>, | ||
| resolve: ResolveNullTipset, | ||
| ) -> Result<Arc<Tipset>, Error> { | ||
| const CHECKPOINT_INTERVAL: ChainEpoch = 1000; | ||
| static CACHE: LazyLock<SizeTrackingLruCache<ChainEpoch, Arc<Tipset>>> = | ||
|
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Why choose
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Good point. Change to using
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Will a chekpoint tipset value be cached in the
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Change to using |
||
| LazyLock::new(|| { | ||
| SizeTrackingLruCache::new_with_default_metrics_registry( | ||
| "tipset_by_height".into(), | ||
| 4096.try_into().expect("infallible"), | ||
| ) | ||
| }); | ||
|
|
||
| fn next_checkpoint(epoch: ChainEpoch) -> ChainEpoch { | ||
| epoch - epoch.mod_floor(&CHECKPOINT_INTERVAL) + CHECKPOINT_INTERVAL | ||
| } | ||
|
|
||
| let checkpoint_from_epoch = next_checkpoint(to); | ||
| let checkpoint_from = CACHE.get_cloned(&checkpoint_from_epoch); | ||
| let from = checkpoint_from.unwrap_or(from); | ||
|
|
||
|
coderabbitai[bot] marked this conversation as resolved.
Outdated
|
||
| if to == 0 { | ||
| return Ok(Arc::new(Tipset::from(from.genesis(&self.db)?))); | ||
| } | ||
|
|
@@ -131,6 +150,10 @@ impl<DB: Blockstore> ChainIndex<DB> { | |
| } | ||
|
|
||
| for (child, parent) in self.chain(from).tuple_windows() { | ||
| if child.epoch() % CHECKPOINT_INTERVAL == 0 { | ||
| CACHE.push(child.epoch(), child.clone()); | ||
| } | ||
|
|
||
| if to == child.epoch() { | ||
| return Ok(child); | ||
| } | ||
|
|
||
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Could we use the chain finality constant here, or a value expressed in terms of chain finality?
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Fixed