From 35d6ad1f3367e25f6b69fa24d878cfbc80898c63 Mon Sep 17 00:00:00 2001 From: Tomek Czajka Date: Fri, 23 Oct 2020 20:36:52 -0700 Subject: [PATCH] Shrink overallocated capacity if a number gets a lot smaller. If a number gets below 1/4 of capacity, shrink_to_fit. Since Vec grows by 2, this amortizes well for any sequence of sizes. --- src/biguint.rs | 3 +++ 1 file changed, 3 insertions(+) diff --git a/src/biguint.rs b/src/biguint.rs index 0a582e02..7d8ab710 100644 --- a/src/biguint.rs +++ b/src/biguint.rs @@ -2617,6 +2617,9 @@ impl BigUint { while let Some(&0) = self.data.last() { self.data.pop(); } + if self.data.len() < self.data.capacity() / 4 { + self.data.shrink_to_fit(); + } } /// Returns a normalized `BigUint`.