Skip to content

Commit

Permalink
Add naive garbage collector to StagingBelt
Browse files Browse the repository at this point in the history
  • Loading branch information
eduardobraun committed May 8, 2021
1 parent 26742fc commit a62dd38
Showing 1 changed file with 14 additions and 0 deletions.
14 changes: 14 additions & 0 deletions src/util/belt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ struct Chunk {
buffer: Buffer,
size: BufferAddress,
offset: BufferAddress,
tick: u64,
}

/// Staging belt is a machine that uploads data.
Expand All @@ -65,6 +66,7 @@ pub struct StagingBelt {
free_chunks: Vec<Chunk>,
sender: mpsc::Sender<Chunk>,
receiver: mpsc::Receiver<Chunk>,
tick: u64,
}

impl StagingBelt {
Expand All @@ -82,6 +84,7 @@ impl StagingBelt {
free_chunks: Vec::new(),
sender,
receiver,
tick: 0,
}
}

Expand Down Expand Up @@ -123,8 +126,10 @@ impl StagingBelt {
}),
size,
offset: 0,
tick: 0,
}
};
chunk.tick = self.tick;

encoder.copy_buffer_to_buffer(&chunk.buffer, chunk.offset, target, offset, size.get());
let old_offset = chunk.offset;
Expand Down Expand Up @@ -183,7 +188,16 @@ impl StagingBelt {
})
})
.collect::<Vec<_>>();
self.gc();

Join { futures }
}

fn gc(&mut self) {
self.tick += 1;
if (self.tick % 20) == 0 {
let t = self.tick;
self.free_chunks.retain(|chunk| {(t - chunk.tick) < 10});
}
}
}

0 comments on commit a62dd38

Please sign in to comment.