Skip to content

Commit 85a9971

Browse files
committed
bound flume channel
Multi threade rendering results are now sent to a bounded channel. Meaning rendering threads will be temporarily blocked if they outpace the main thread transferring results to the final image. This wastes less memory. It is also potentially faster, since the main thread should be assigned priority.
1 parent 8855245 commit 85a9971

File tree

1 file changed

+6
-1
lines changed

1 file changed

+6
-1
lines changed

src/render/function.rs

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -221,9 +221,14 @@ pub fn render(
221221
let mut line_num: u32 = 0;
222222
let mut longest_line_chars = 0;
223223
let mut background = None;
224+
// An atomic integer used to tell threads which file to render next.
225+
// Threads read a value and then incrment it.
226+
// This is cheaper than creating a channel and sending the content to
227+
// render via channel to each thread.
224228
let file_index = AtomicUsize::default();
229+
225230
std::thread::scope(|scope| -> anyhow::Result<()> {
226-
let (ttx, trx) = flume::unbounded();
231+
let (ttx, trx) = flume::bounded(threads);
227232
for tid in 0..threads {
228233
scope.spawn({
229234
let ttx = ttx.clone();

0 commit comments

Comments
 (0)