]> git.lizzy.rs Git - rust.git/commitdiff
Use `SmallVec` to avoid allocations in `from_decimal_string`.
authorNicholas Nethercote <nnethercote@mozilla.com>
Fri, 9 Nov 2018 06:12:52 +0000 (17:12 +1100)
committerNicholas Nethercote <nnethercote@mozilla.com>
Fri, 9 Nov 2018 07:57:18 +0000 (18:57 +1100)
This reduces the number of allocations in a "check clean" build of
`tuple-stress` by 14%, reducing instruction counts by 0.6%.

src/Cargo.lock
src/librustc_apfloat/Cargo.toml
src/librustc_apfloat/ieee.rs
src/librustc_apfloat/lib.rs

index b4317864502cee0fd48979f54fb6f057b47c0bcf..17b1744a07f2f1485928c9b55f719c9e1e0048f9 100644 (file)
@@ -2095,6 +2095,7 @@ version = "0.0.0"
 dependencies = [
  "bitflags 1.0.4 (registry+https://github.com/rust-lang/crates.io-index)",
  "rustc_cratesio_shim 0.0.0",
+ "smallvec 0.6.5 (registry+https://github.com/rust-lang/crates.io-index)",
 ]
 
 [[package]]
index 735b74f156500356c1e66759ec55fe6de32510de..a8a5da90c7a6bb41418cb0b5cf20447eca1fff38 100644 (file)
@@ -10,3 +10,4 @@ path = "lib.rs"
 [dependencies]
 bitflags = "1.0"
 rustc_cratesio_shim = { path = "../librustc_cratesio_shim" }
+smallvec = { version = "0.6.5", features = ["union"] }
index 87d59d2e763cbe89743452e59379e8888f639f3a..4f405858e350ce8269b9fba6dfe8512f2f6474ed 100644 (file)
@@ -11,6 +11,7 @@
 use {Category, ExpInt, IEK_INF, IEK_NAN, IEK_ZERO};
 use {Float, FloatConvert, ParseError, Round, Status, StatusAnd};
 
+use smallvec::{SmallVec, smallvec};
 use std::cmp::{self, Ordering};
 use std::convert::TryFrom;
 use std::fmt::{self, Write};
@@ -1962,7 +1963,7 @@ fn from_decimal_string(s: &str, round: Round) -> Result<StatusAnd<Self>, ParseEr
         // to hold the full significand, and an extra limb required by
         // tcMultiplyPart.
         let max_limbs = limbs_for_bits(1 + 196 * significand_digits / 59);
-        let mut dec_sig = Vec::with_capacity(max_limbs);
+        let mut dec_sig: SmallVec<[Limb; 1]> = SmallVec::with_capacity(max_limbs);
 
         // Convert to binary efficiently - we do almost all multiplication
         // in a Limb. When this would overflow do we do a single
@@ -2021,11 +2022,11 @@ fn from_decimal_string(s: &str, round: Round) -> Result<StatusAnd<Self>, ParseEr
 
             const FIRST_EIGHT_POWERS: [Limb; 8] = [1, 5, 25, 125, 625, 3125, 15625, 78125];
 
-            let mut p5_scratch = vec![];
-            let mut p5 = vec![FIRST_EIGHT_POWERS[4]];
+            let mut p5_scratch = smallvec![];
+            let mut p5: SmallVec<[Limb; 1]> = smallvec![FIRST_EIGHT_POWERS[4]];
 
-            let mut r_scratch = vec![];
-            let mut r = vec![FIRST_EIGHT_POWERS[power & 7]];
+            let mut r_scratch = smallvec![];
+            let mut r: SmallVec<[Limb; 1]> = smallvec![FIRST_EIGHT_POWERS[power & 7]];
             power >>= 3;
 
             while power > 0 {
@@ -2064,7 +2065,7 @@ fn from_decimal_string(s: &str, round: Round) -> Result<StatusAnd<Self>, ParseEr
             let calc_precision = (LIMB_BITS << attempt) - 1;
             attempt += 1;
 
-            let calc_normal_from_limbs = |sig: &mut Vec<Limb>,
+            let calc_normal_from_limbs = |sig: &mut SmallVec<[Limb; 1]>,
                                           limbs: &[Limb]|
              -> StatusAnd<ExpInt> {
                 sig.resize(limbs_for_bits(calc_precision), 0);
index 6ea722ba769c16181aac010ec8c025bd2eb9e5d2..69c9f385409e4a268a1c0d17a39233e64c39bdbf 100644 (file)
@@ -53,6 +53,7 @@
 
 #[macro_use]
 extern crate bitflags;
+extern crate smallvec;
 
 use std::cmp::Ordering;
 use std::fmt;