| 5530 | } |
| 5531 | |
| 5532 | void ChainstateManager::MaybeRebalanceCaches() |
| 5533 | { |
| 5534 | AssertLockHeld(::cs_main); |
| 5535 | if (m_ibd_chainstate && !m_snapshot_chainstate) { |
| 5536 | LogPrintf("[snapshot] allocating all cache to the IBD chainstate\n"); |
| 5537 | // Allocate everything to the IBD chainstate. |
| 5538 | m_ibd_chainstate->ResizeCoinsCaches(m_total_coinstip_cache, m_total_coinsdb_cache); |
| 5539 | } |
| 5540 | else if (m_snapshot_chainstate && !m_ibd_chainstate) { |
| 5541 | LogPrintf("[snapshot] allocating all cache to the snapshot chainstate\n"); |
| 5542 | // Allocate everything to the snapshot chainstate. |
| 5543 | m_snapshot_chainstate->ResizeCoinsCaches(m_total_coinstip_cache, m_total_coinsdb_cache); |
| 5544 | } |
| 5545 | else if (m_ibd_chainstate && m_snapshot_chainstate) { |
| 5546 | // If both chainstates exist, determine who needs more cache based on IBD status. |
| 5547 | // |
| 5548 | // Note: shrink caches first so that we don't inadvertently overwhelm available memory. |
| 5549 | if (m_snapshot_chainstate->IsInitialBlockDownload()) { |
| 5550 | m_ibd_chainstate->ResizeCoinsCaches( |
| 5551 | m_total_coinstip_cache * 0.05, m_total_coinsdb_cache * 0.05); |
| 5552 | m_snapshot_chainstate->ResizeCoinsCaches( |
| 5553 | m_total_coinstip_cache * 0.95, m_total_coinsdb_cache * 0.95); |
| 5554 | } else { |
| 5555 | m_snapshot_chainstate->ResizeCoinsCaches( |
| 5556 | m_total_coinstip_cache * 0.05, m_total_coinsdb_cache * 0.05); |
| 5557 | m_ibd_chainstate->ResizeCoinsCaches( |
| 5558 | m_total_coinstip_cache * 0.95, m_total_coinsdb_cache * 0.95); |
| 5559 | } |
| 5560 | } |
| 5561 | } |