2020-09-22 14:53:52 +02:00
|
|
|
// Copyright 2015-2020 Parity Technologies (UK) Ltd.
|
|
|
|
// This file is part of OpenEthereum.
|
2016-07-31 00:19:27 +02:00
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is free software: you can redistribute it and/or modify
|
2016-07-31 00:19:27 +02:00
|
|
|
// it under the terms of the GNU General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
|
2020-09-22 14:53:52 +02:00
|
|
|
// OpenEthereum is distributed in the hope that it will be useful,
|
2016-07-31 00:19:27 +02:00
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU General Public License for more details.
|
|
|
|
|
|
|
|
// You should have received a copy of the GNU General Public License
|
2020-09-22 14:53:52 +02:00
|
|
|
// along with OpenEthereum. If not, see <http://www.gnu.org/licenses/>.
|
2016-07-31 00:19:27 +02:00
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
//! Database cache manager
|
|
|
|
|
2016-07-31 00:19:27 +02:00
|
|
|
use std::{
|
|
|
|
collections::{HashSet, VecDeque},
|
|
|
|
hash::Hash,
|
|
|
|
};
|
|
|
|
|
|
|
|
const COLLECTION_QUEUE_SIZE: usize = 8;
|
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
/// DB cache manager
|
2018-07-09 13:55:27 +02:00
|
|
|
pub struct CacheManager<T> {
|
2016-07-31 00:19:27 +02:00
|
|
|
pref_cache_size: usize,
|
|
|
|
max_cache_size: usize,
|
|
|
|
bytes_per_cache_entry: usize,
|
|
|
|
cache_usage: VecDeque<HashSet<T>>,
|
|
|
|
}
|
|
|
|
|
|
|
|
impl<T> CacheManager<T>
|
|
|
|
where
|
|
|
|
T: Eq + Hash,
|
|
|
|
{
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Create new cache manager with preferred (heap) sizes.
|
2016-07-31 00:19:27 +02:00
|
|
|
pub fn new(
|
|
|
|
pref_cache_size: usize,
|
|
|
|
max_cache_size: usize,
|
|
|
|
bytes_per_cache_entry: usize,
|
|
|
|
) -> Self {
|
|
|
|
CacheManager {
|
|
|
|
pref_cache_size: pref_cache_size,
|
|
|
|
max_cache_size: max_cache_size,
|
|
|
|
bytes_per_cache_entry: bytes_per_cache_entry,
|
|
|
|
cache_usage: (0..COLLECTION_QUEUE_SIZE)
|
|
|
|
.into_iter()
|
|
|
|
.map(|_| Default::default())
|
|
|
|
.collect(),
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
2016-07-31 00:19:27 +02:00
|
|
|
|
2019-01-04 14:05:46 +01:00
|
|
|
/// Mark element as used.
|
2016-07-31 00:19:27 +02:00
|
|
|
pub fn note_used(&mut self, id: T) {
|
|
|
|
if !self.cache_usage[0].contains(&id) {
|
|
|
|
if let Some(c) = self
|
|
|
|
.cache_usage
|
|
|
|
.iter_mut()
|
|
|
|
.skip(1)
|
|
|
|
.find(|e| e.contains(&id))
|
|
|
|
{
|
|
|
|
c.remove(&id);
|
|
|
|
}
|
|
|
|
self.cache_usage[0].insert(id);
|
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
2016-07-31 00:19:27 +02:00
|
|
|
|
2016-08-08 16:14:37 +02:00
|
|
|
/// Collects unused objects from cache.
|
|
|
|
/// First params is the current size of the cache.
|
|
|
|
/// Second one is an with objects to remove. It should also return new size of the cache.
|
|
|
|
pub fn collect_garbage<F>(&mut self, current_size: usize, mut notify_unused: F)
|
|
|
|
where
|
|
|
|
F: FnMut(HashSet<T>) -> usize,
|
|
|
|
{
|
|
|
|
if current_size < self.pref_cache_size {
|
2016-07-31 00:19:27 +02:00
|
|
|
self.rotate_cache_if_needed();
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
for _ in 0..COLLECTION_QUEUE_SIZE {
|
2016-10-20 23:41:15 +02:00
|
|
|
if let Some(back) = self.cache_usage.pop_back() {
|
|
|
|
let current_size = notify_unused(back);
|
|
|
|
self.cache_usage.push_front(Default::default());
|
|
|
|
if current_size < self.max_cache_size {
|
|
|
|
break;
|
|
|
|
}
|
2016-07-31 00:19:27 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn rotate_cache_if_needed(&mut self) {
|
2016-10-27 08:28:12 +02:00
|
|
|
if self.cache_usage.is_empty() {
|
|
|
|
return;
|
|
|
|
}
|
2016-10-20 23:41:15 +02:00
|
|
|
|
2016-07-31 00:19:27 +02:00
|
|
|
if self.cache_usage[0].len() * self.bytes_per_cache_entry
|
|
|
|
> self.pref_cache_size / COLLECTION_QUEUE_SIZE
|
|
|
|
{
|
2016-10-20 23:41:15 +02:00
|
|
|
if let Some(cache) = self.cache_usage.pop_back() {
|
|
|
|
self.cache_usage.push_front(cache);
|
2016-07-31 00:19:27 +02:00
|
|
|
}
|
2020-08-05 06:08:03 +02:00
|
|
|
}
|
2016-07-31 00:19:27 +02:00
|
|
|
}
|
|
|
|
}
|