-
Notifications
You must be signed in to change notification settings - Fork 144
Expand file tree
/
Copy pathchunk_cache.rs
More file actions
110 lines (93 loc) · 3.19 KB
/
Copy pathchunk_cache.rs
File metadata and controls
110 lines (93 loc) · 3.19 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
use std::{collections::VecDeque, sync::Arc};
use ahash::AHashMap;
use libcraft::ChunkPosition;
use quill::ChunkHandle;
/// This struct contains chunks that were unloaded but remain in memory, either
/// because there are still handles to them (`Arc`s) or because they have not
/// yet completed saving.
#[derive(Default)]
pub struct ChunkCache {
map: AHashMap<ChunkPosition, (ChunkHandle, bool)>, // handle + was saved
unload_queue: VecDeque<ChunkPosition>,
}
impl ChunkCache {
pub fn new() -> Self {
Self {
map: AHashMap::new(),
unload_queue: VecDeque::new(),
}
}
fn ref_count(&self, pos: &ChunkPosition) -> Option<usize> {
self.map.get(pos).map(|(arc, _)| Arc::strong_count(arc))
}
/// Purges all chunks in the cache that have no remaining references
/// and have been saved.
pub fn purge_old_unused(&mut self) -> usize {
let mut processed = 0;
let mut purged = 0;
let total = self.unload_queue.len();
while let Some(pos) = self.unload_queue.pop_front() {
if !self.map.contains_key(&pos) {
processed += 1;
continue;
}
let (_handle, was_saved) = self.map.get(&pos).unwrap();
if self.ref_count(&pos).unwrap() > 1 || !*was_saved {
// Another copy of this handle still exists, or
// the chunk has not yet been saved.
self.unload_queue.push_back(pos);
} else {
self.map.remove_entry(&pos);
purged += 1;
}
processed += 1;
if processed >= total {
break;
}
}
purged
}
#[allow(unused)]
pub fn get(&self, pos: ChunkPosition) -> Option<ChunkHandle> {
self.map.get(&pos).map(|(c, _)| Arc::clone(c))
}
/// Returns whether the chunk at the position is cached.
#[allow(unused)]
pub fn contains(&self, pos: &ChunkPosition) -> bool {
self.map.contains_key(pos)
}
/// Inserts a chunk handle into the cache, returning the previous handle if there was one.
pub fn insert(
&mut self,
pos: ChunkPosition,
handle: ChunkHandle,
was_saved: bool,
) -> Option<ChunkHandle> {
if was_saved && Arc::strong_count(&handle) == 1 {
// No need to cache the chunk, as it's been saved
// and has no other handles alive.
return None;
}
self.unload_queue.push_back(pos);
self.map
.insert(pos, (handle, was_saved))
.map(|(handle, _)| handle)
}
/// Marks the given chunk as saved.
pub fn mark_saved(&mut self, pos: ChunkPosition) {
if let Some((_, saved)) = self.map.get_mut(&pos) {
*saved = true;
}
}
/// Removes the chunk handle at the given position, returning the handle if it was cached.
pub fn remove(&mut self, pos: ChunkPosition) -> Option<ChunkHandle> {
self.map.remove(&pos).map(|(handle, _)| handle)
}
pub fn len(&self) -> usize {
self.map.len()
}
#[allow(dead_code)]
pub fn is_empty(&self) -> bool {
self.map.is_empty()
}
}