Skip to content

Commit 2adaf77

Browse files
committed
refactor: use encoded_size as memory usage
1 parent bd8c970 commit 2adaf77

File tree

4 files changed

+25
-10
lines changed

4 files changed

+25
-10
lines changed

analytic_engine/src/memtable/skiplist/mod.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -153,12 +153,13 @@ impl<A: Arena<Stats = BasicStats> + Clone + Sync + Send + 'static> MemTable
153153
}
154154

155155
fn approximate_memory_usage(&self) -> usize {
156-
// Mem size of skiplist is u32, need to cast to usize
157-
match self.skiplist.mem_size().try_into() {
158-
Ok(v) => v,
159-
// The skiplist already use bytes larger than usize
160-
Err(_) => usize::MAX,
161-
}
156+
let encoded_size = self
157+
.metrics
158+
.row_encoded_size
159+
.load(atomic::Ordering::Relaxed);
160+
161+
let arena_block_size = self.skiplist.arena_block_size();
162+
encoded_size / arena_block_size * arena_block_size + arena_block_size
162163
}
163164

164165
fn set_last_sequence(&self, sequence: SequenceNumber) -> Result<()> {

components/arena/src/arena_trait.rs

+3
Original file line numberDiff line numberDiff line change
@@ -36,6 +36,9 @@ pub trait Arena {
3636
/// Get arena's statistics.
3737
fn stats(&self) -> Self::Stats;
3838

39+
/// Get arena's block size.
40+
fn block_size(&self) -> usize;
41+
3942
// provided methods
4043

4144
/// Allocate required memory. Panic if failed.

components/arena/src/mono_inc.rs

+11-4
Original file line numberDiff line numberDiff line change
@@ -38,21 +38,24 @@ const DEFAULT_ALIGN: usize = 8;
3838
#[derive(Clone)]
3939
pub struct MonoIncArena {
4040
core: Arc<RwLock<ArenaCore>>,
41+
block_size: usize,
4142
}
4243

4344
impl MonoIncArena {
44-
pub fn new(regular_block_size: usize) -> Self {
45+
pub fn new(block_size: usize) -> Self {
4546
Self {
4647
core: Arc::new(RwLock::new(ArenaCore::new(
47-
regular_block_size,
48+
block_size,
4849
Arc::new(NoopCollector {}),
4950
))),
51+
block_size,
5052
}
5153
}
5254

53-
pub fn with_collector(regular_block_size: usize, collector: CollectorRef) -> Self {
55+
pub fn with_collector(block_size: usize, collector: CollectorRef) -> Self {
5456
Self {
55-
core: Arc::new(RwLock::new(ArenaCore::new(regular_block_size, collector))),
57+
core: Arc::new(RwLock::new(ArenaCore::new(block_size, collector))),
58+
block_size,
5659
}
5760
}
5861
}
@@ -71,6 +74,10 @@ impl Arena for MonoIncArena {
7174
fn alloc(&self, layout: Layout) -> NonNull<u8> {
7275
self.core.write().unwrap().alloc(layout)
7376
}
77+
78+
fn block_size(&self) -> usize {
79+
self.block_size
80+
}
7481
}
7582

7683
struct ArenaCore {

components/skiplist/src/list.rs

+4
Original file line numberDiff line numberDiff line change
@@ -252,6 +252,10 @@ impl<C, A: Arena<Stats = BasicStats> + Clone> Skiplist<C, A> {
252252
fn height(&self) -> usize {
253253
self.core.height.load(Ordering::SeqCst)
254254
}
255+
256+
pub fn arena_block_size(&self) -> usize {
257+
self.core.arena.block_size()
258+
}
255259
}
256260

257261
impl<C: KeyComparator, A: Arena<Stats = BasicStats> + Clone> Skiplist<C, A> {

0 commit comments

Comments
 (0)