Ver código fonte

Reduce unnecessary String allocations

- Remove uses of to_string() on a &str where it wasn't necessary
- Use SendStr to reduce allocations further
Ben S 11 anos atrás
pai
commit
d72be30c30
3 arquivos alterados com 23 adições e 22 exclusões
  1. 2 2
      src/column.rs
  2. 8 7
      src/file.rs
  3. 13 13
      src/format.rs

+ 2 - 2
src/column.rs

@@ -49,8 +49,8 @@ impl Column {
 impl Alignment {
     pub fn pad_string(&self, string: &String, padding: uint) -> String {
         match *self {
-            Alignment::Left => string.clone() + " ".to_string().repeat(padding).as_slice(),
-            Alignment::Right => " ".to_string().repeat(padding) + string.as_slice(),
+            Alignment::Left => string.clone() + " ".repeat(padding).as_slice(),
+            Alignment::Right => " ".repeat(padding) + string.as_slice(),
         }
     }
 }

+ 8 - 7
src/file.rs

@@ -1,5 +1,6 @@
 use std::io::{fs, IoResult};
 use std::io;
+use std::str::SendStr;
 
 use ansi_term::{Paint, Colour, Plain, Style, Red, Green, Yellow, Blue, Purple, Cyan, Fixed};
 
@@ -216,14 +217,14 @@ impl<'a> File<'a> {
         }
     }
 
-    fn type_char(&self) -> String {
+    fn type_char(&self) -> SendStr {
         return match self.stat.kind {
-            io::TypeFile         => ".".to_string(),
-            io::TypeDirectory    => Blue.paint("d"),
-            io::TypeNamedPipe    => Yellow.paint("|"),
-            io::TypeBlockSpecial => Purple.paint("s"),
-            io::TypeSymlink      => Cyan.paint("l"),
-            io::TypeUnknown      => "?".to_string(),
+            io::TypeFile         => ".".into_maybe_owned(),
+            io::TypeDirectory    => Blue.paint("d").into_maybe_owned(),
+            io::TypeNamedPipe    => Yellow.paint("|").into_maybe_owned(),
+            io::TypeBlockSpecial => Purple.paint("s").into_maybe_owned(),
+            io::TypeSymlink      => Cyan.paint("l").into_maybe_owned(),
+            io::TypeUnknown      => "?".into_maybe_owned(),
         }
     }
 

+ 13 - 13
src/format.rs

@@ -6,7 +6,7 @@ static IEC_PREFIXES: &'static [&'static str] = &[
     "", "Ki", "Mi", "Gi", "Ti", "Pi", "Ei", "Zi", "Yi"
 ];
 
-fn format_bytes(mut amount: f64, kilo: f64, prefixes: &[&str]) -> (String, String) {
+fn format_bytes<'s>(mut amount: f64, kilo: f64, prefixes: &[&'s str]) -> (String, &'s str) {
     let mut prefix = 0;
     while amount >= kilo {
         amount /= kilo;
@@ -14,67 +14,67 @@ fn format_bytes(mut amount: f64, kilo: f64, prefixes: &[&str]) -> (String, Strin
     }
 
     if amount < 10.0 && prefix != 0 {
-        (format!("{:.1}", amount), prefixes[prefix].to_string())
+        (format!("{:.1}", amount), prefixes[prefix])
     }
     else {
-        (format!("{:.0}", amount), prefixes[prefix].to_string())
+        (format!("{:.0}", amount), prefixes[prefix])
     }
 }
 
 #[allow(non_snake_case)]
-pub fn format_IEC_bytes(amount: u64) -> (String, String) {
+pub fn format_IEC_bytes<'s>(amount: u64) -> (String, &'s str) {
     format_bytes(amount as f64, 1024.0, IEC_PREFIXES)
 }
 
-pub fn format_metric_bytes(amount: u64) -> (String, String) {
+pub fn format_metric_bytes<'s>(amount: u64) -> (String, &'s str) {
     format_bytes(amount as f64, 1000.0, METRIC_PREFIXES)
 }
 
 #[test]
 fn test_0() {
     let kk = format_metric_bytes(0);
-    assert!(kk == ("0".to_string(), "".to_string()));
+    assert!(kk == ("0".to_string(), ""));
 }
 
 #[test]
 fn test_999() {
     let kk = format_metric_bytes(999);
-    assert!(kk == ("999".to_string(), "".to_string()));
+    assert!(kk == ("999".to_string(), ""));
 }
 
 #[test]
 fn test_1000() {
     let kk = format_metric_bytes(1000);
-    assert!(kk == ("1.0".to_string(), "K".to_string()));
+    assert!(kk == ("1.0".to_string(), "K"));
 }
 
 #[test]
 fn test_1030() {
     let kk = format_metric_bytes(1030);
-    assert!(kk == ("1.0".to_string(), "K".to_string()));
+    assert!(kk == ("1.0".to_string(), "K"));
 }
 
 #[test]
 fn test_1100() {
     let kk = format_metric_bytes(1100);
-    assert!(kk == ("1.1".to_string(), "K".to_string()));
+    assert!(kk == ("1.1".to_string(), "K"));
 }
 
 #[test]
 fn test_1111() {
     let kk = format_metric_bytes(1111);
-    assert!(kk == ("1.1".to_string(), "K".to_string()));
+    assert!(kk == ("1.1".to_string(), "K"));
 }
 
 #[test]
 fn test_104857() {
     let kk = format_IEC_bytes(126456);
-    assert!(kk == ("123".to_string(), "Ki".to_string()));
+    assert!(kk == ("123".to_string(), "Ki"));
 }
 
 #[test]
 fn test_1048576() {
     let kk = format_IEC_bytes(1048576);
-    assert!(kk == ("1.0".to_string(), "Mi".to_string()));
+    assert!(kk == ("1.0".to_string(), "Mi"));
 }