From 8c9faa16b94c9ef9064b9a2e9d521046619bcc66 Mon Sep 17 00:00:00 2001 From: Michael Debertol Date: Fri, 7 May 2021 21:50:33 +0200 Subject: [PATCH] sort: improve memory usage for extsort --- src/uu/sort/src/external_sort/mod.rs | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/src/uu/sort/src/external_sort/mod.rs b/src/uu/sort/src/external_sort/mod.rs index 725b17bbd..662250e1d 100644 --- a/src/uu/sort/src/external_sort/mod.rs +++ b/src/uu/sort/src/external_sort/mod.rs @@ -113,7 +113,7 @@ pub fn ext_sort( chunk.push(seq); - if total_read >= settings.buffer_size { + if total_read + chunk.len() * std::mem::size_of::() >= settings.buffer_size { super::sort_by(&mut chunk, &settings); write_chunk( settings, @@ -136,6 +136,9 @@ pub fn ext_sort( iter.chunks += 1; } + // We manually drop here to not go over our memory limit when we allocate below. + drop(chunk); + // initialize buffers for each chunk // // Having a right sized buffer for each chunk for smallish values seems silly to me?