Просмотр исходного кода

perf: Pre-allocate the strings in processing (-5%)

This might not be exact, but it should be a good estimate. I don't think
this will ever over allocate in any case(?).
Markus Westerlind 8 лет назад
Родитель
Сommit
3071ad4156
1 измененных файлов с 3 добавлено и 2 удалено
  1. 3 2
      idna/src/uts46.rs

+ 3 - 2
idna/src/uts46.rs

@@ -278,11 +278,12 @@ fn validate(label: &str, is_bidi_domain: bool, flags: Flags, errors: &mut Vec<Er
 
 
 /// http://www.unicode.org/reports/tr46/#Processing
 /// http://www.unicode.org/reports/tr46/#Processing
 fn processing(domain: &str, flags: Flags, errors: &mut Vec<Error>) -> String {
 fn processing(domain: &str, flags: Flags, errors: &mut Vec<Error>) -> String {
-    let mut mapped = String::new();
+    let mut mapped = String::with_capacity(domain.len());
     for c in domain.chars() {
     for c in domain.chars() {
         map_char(c, flags, &mut mapped, errors)
         map_char(c, flags, &mut mapped, errors)
     }
     }
-    let normalized: String = mapped.nfc().collect();
+    let mut normalized = String::with_capacity(mapped.len());
+    normalized.extend(mapped.nfc());
 
 
     // Find out if it's a Bidi Domain Name
     // Find out if it's a Bidi Domain Name
     //
     //