Skip to content

Commit 9c7ac0f

Browse files
author
Stephan Dilly
committed
some more immutable string optimizations
and precompute diff line trimming (newlines)
1 parent 8353dfd commit 9c7ac0f

File tree

5 files changed

+19
-23
lines changed

5 files changed

+19
-23
lines changed

asyncgit/src/sync/diff.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -435,7 +435,7 @@ mod tests {
435435
get_diff(repo_path, "foo/bar.txt", false, None).unwrap();
436436

437437
assert_eq!(diff.hunks.len(), 1);
438-
assert_eq!(&*diff.hunks[0].lines[1].content, "test\n");
438+
assert_eq!(&*diff.hunks[0].lines[1].content, "test");
439439
}
440440

441441
#[test]

asyncgit/src/sync/staging/stage_tracked.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -100,10 +100,7 @@ mod test {
100100
let diff = get_diff(path, "test.txt", true, None).unwrap();
101101

102102
assert_eq!(diff.lines, 3);
103-
assert_eq!(
104-
diff.hunks[0].lines[0].content,
105-
String::from("@@ -1 +1,2 @@\n")
106-
);
103+
assert_eq!(&*diff.hunks[0].lines[0].content, "@@ -1 +1,2 @@");
107104
}
108105

109106
#[test]
@@ -142,10 +139,7 @@ c = 4";
142139
let diff = get_diff(path, "test.txt", true, None).unwrap();
143140

144141
assert_eq!(diff.lines, 5);
145-
assert_eq!(
146-
diff.hunks[0].lines[0].content,
147-
String::from("@@ -1,2 +1 @@\n")
148-
);
142+
assert_eq!(&*diff.hunks[0].lines[0].content, "@@ -1,2 +1 @@");
149143
}
150144

151145
#[test]

asyncgit/src/sync/utils.rs

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -331,10 +331,7 @@ mod tests {
331331
// And that file is test.txt
332332
let diff =
333333
get_diff(repo_path, "test.txt", true, None).unwrap();
334-
assert_eq!(
335-
&*diff.hunks[0].lines[0].content,
336-
String::from("@@ -1 +1 @@\n")
337-
);
334+
assert_eq!(&*diff.hunks[0].lines[0].content, "@@ -1 +1 @@");
338335
}
339336

340337
#[test]

src/components/commitlist.rs

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ const ELEMENTS_PER_LINE: usize = 9;
2828

2929
///
3030
pub struct CommitList {
31-
title: String,
31+
title: Box<str>,
3232
selection: usize,
3333
branch: Option<String>,
3434
count_total: usize,
@@ -61,7 +61,7 @@ impl CommitList {
6161
scroll_top: Cell::new(0),
6262
theme,
6363
key_config,
64-
title: String::from(title),
64+
title: title.into(),
6565
}
6666
}
6767

@@ -258,7 +258,7 @@ impl CommitList {
258258

259259
// commit hash
260260
txt.push(Span::styled(
261-
Cow::from(e.hash_short.as_str()),
261+
Cow::from(&*e.hash_short),
262262
theme.commit_hash(selected),
263263
));
264264

@@ -298,7 +298,7 @@ impl CommitList {
298298

299299
// commit msg
300300
txt.push(Span::styled(
301-
Cow::from(e.msg.as_str()),
301+
Cow::from(&*e.msg),
302302
theme.text(true, selected),
303303
));
304304

src/components/utils/logitems.rs

Lines changed: 11 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -6,11 +6,16 @@ use crate::components::utils::emojifi_string;
66

77
static SLICE_OFFSET_RELOAD_THRESHOLD: usize = 100;
88

9+
type BoxStr = Box<str>;
10+
911
pub struct LogEntry {
12+
//TODO: cache string representation
1013
pub time: DateTime<Local>,
11-
pub author: String,
12-
pub msg: String,
13-
pub hash_short: String,
14+
//TODO: use tinyvec here
15+
pub author: BoxStr,
16+
pub msg: BoxStr,
17+
//TODO: use tinyvec here
18+
pub hash_short: BoxStr,
1419
pub id: CommitId,
1520
}
1621

@@ -28,10 +33,10 @@ impl From<CommitInfo> for LogEntry {
2833
emojifi_string(&mut msg);
2934

3035
Self {
31-
author,
32-
msg,
36+
author: author.into(),
37+
msg: msg.into(),
3338
time,
34-
hash_short: c.id.get_short_string(),
39+
hash_short: c.id.get_short_string().into(),
3540
id: c.id,
3641
}
3742
}

0 commit comments

Comments
 (0)