Skip to content

Commit 95344a9

Browse files
committed
perf: use string preallocations for string concatenation
1 parent af3b670 commit 95344a9

File tree

3 files changed

+20
-1
lines changed

3 files changed

+20
-1
lines changed

helix-stdx/src/lib.rs

+1
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ pub mod faccess;
33
pub mod path;
44
pub mod range;
55
pub mod rope;
6+
pub mod str;
67
pub mod time;
78

89
pub use range::Range;

helix-stdx/src/str.rs

+18
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
/// Concatenates strings together.
2+
///
3+
/// `concat!(a, " ", b, " ", c)` is:
4+
/// - more performant than `format!("{a} {b} {c}")`
5+
/// - more ergonomic than using `String::with_capacity` followed by a series of `String::push_str`
6+
#[macro_export]
7+
macro_rules! concat {
8+
($($value:expr),*) => {{
9+
// Rust does not allow using `+` as separator between value
10+
// so we must add that at the end of everything. The `0` is necessary
11+
// at the end so it does not end with "+ " (which would be invalid syntax)
12+
let mut buf = String::with_capacity($($value.len() + )* 0);
13+
$(
14+
buf.push_str(&$value);
15+
)*
16+
buf
17+
}}
18+
}

helix-stdx/src/time.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,5 +71,5 @@ pub fn format_relative_time(timestamp: i64, timezone_offset: i32) -> String {
7171
"from now"
7272
};
7373

74-
format!("{value} {unit} {label}")
74+
crate::concat!(value, " ", unit, " ", label)
7575
}

0 commit comments

Comments
 (0)