Skip to content

Commit 563f8c0

Browse files
committed
Reduce String allocations
Replace format! allocations used for pushing to an existing string with write! invocations.
1 parent 5f702a2 commit 563f8c0

File tree

1 file changed

+6
-5
lines changed

1 file changed

+6
-5
lines changed

src/bytecode.rs

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,4 @@
1+
use std::fmt::Write;
12
use std::collections::BTreeMap;
23
use std::str::FromStr;
34

@@ -51,7 +52,7 @@ impl LispObject {
5152
} else if (c as u32) < 32 || (c as u32) == 127 {
5253
// not printable
5354
// NOTE: cannot use escape for c in 128..=255, otherwise the string would become unibyte
54-
result += &format!("\\{:03o}", c as u32);
55+
write!(result, "\\{:03o}", c as u32).unwrap();
5556
} else {
5657
result.push(c);
5758
}
@@ -78,14 +79,14 @@ impl LispObject {
7879
27 => result += "\\e",
7980
// NOTE: do not use 0..=7 in this branch, because it takes one more byte than the next branch
8081
8..=26 => { // \^@ \^A \^B ... \^Z
81-
result += &format!("\\^{}", (*c as u32 + 64) as u8 as char);
82+
write!(&mut result, "\\^{}", (*c as u32 + 64) as u8 as char).unwrap();
8283
},
8384
0..=7 | 27..=31 | 128..=255 | 34 | 92 => { // oct, for unprintable and '"' and '\\'
84-
let oct_s = format!("\\{:o}", *c as u32);
85-
if oct_s.len() < 4 {
85+
let last_len = result.len();
86+
write!(result, "\\{:o}", *c as u32).unwrap();
87+
if result.len() - last_len < 4 {
8688
oct_escape_not_full = true;
8789
}
88-
result += &oct_s;
8990
},
9091
_ => { // printable
9192
// https://www.gnu.org/software/emacs/manual/html_node/elisp/Non_002dASCII-in-St

0 commit comments

Comments
 (0)