Skip to content

Commit a561228

Browse files
committed
Reserve capacity for encode_non_ascii
1 parent 9a716d4 commit a561228

File tree

1 file changed

+9
-1
lines changed

1 file changed

+9
-1
lines changed

src/convert.rs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -115,7 +115,13 @@ impl<T: Bos<str>> IriRef<T> {
115115
}
116116

117117
fn encode_non_ascii(r: RmrRef<'_, '_>) -> (String, Meta) {
118-
let mut buf = String::new();
118+
let len = r
119+
.as_str()
120+
.chars()
121+
.map(|c| if c.is_ascii() { 1 } else { c.len_utf8() * 3 })
122+
.sum();
123+
124+
let mut buf = String::with_capacity(len);
119125
let mut meta = Meta::default();
120126

121127
if let Some(scheme) = r.scheme_opt() {
@@ -162,6 +168,8 @@ fn encode_non_ascii(r: RmrRef<'_, '_>) -> (String, Meta) {
162168
encode_non_ascii_str(&mut buf, fragment.as_str());
163169
}
164170

171+
debug_assert_eq!(buf.len(), len);
172+
165173
(buf, meta)
166174
}
167175

0 commit comments

Comments
 (0)