Skip to content

Commit 8353dfd

Browse files
author
Stephan Dilly
committed
use less memory per hunk (immutable string)
1 parent a865432 commit 8353dfd

File tree

3 files changed

+13
-10
lines changed

3 files changed

+13
-10
lines changed

asyncgit/src/sync/diff.rs

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -49,7 +49,7 @@ impl Default for DiffLineType {
4949
#[derive(Default, Clone, Hash, Debug)]
5050
pub struct DiffLine {
5151
///
52-
pub content: String,
52+
pub content: Box<str>,
5353
///
5454
pub line_type: DiffLineType,
5555
///
@@ -292,7 +292,9 @@ fn raw_diff_to_file_diff<'a>(
292292
let diff_line = DiffLine {
293293
position: DiffLinePosition::from(&line),
294294
content: String::from_utf8_lossy(line.content())
295-
.to_string(),
295+
//Note: trim await trailing newline characters
296+
.trim_matches(is_newline)
297+
.into(),
296298
line_type: line.origin_value().into(),
297299
};
298300

@@ -376,6 +378,10 @@ fn raw_diff_to_file_diff<'a>(
376378
Ok(res.into_inner())
377379
}
378380

381+
const fn is_newline(c: char) -> bool {
382+
c == '\n' || c == '\r'
383+
}
384+
379385
fn new_file_content(path: &Path) -> Option<Vec<u8>> {
380386
if let Ok(meta) = fs::symlink_metadata(path) {
381387
if meta.file_type().is_symlink() {
@@ -429,7 +435,7 @@ mod tests {
429435
get_diff(repo_path, "foo/bar.txt", false, None).unwrap();
430436

431437
assert_eq!(diff.hunks.len(), 1);
432-
assert_eq!(diff.hunks[0].lines[1].content, "test\n");
438+
assert_eq!(&*diff.hunks[0].lines[1].content, "test\n");
433439
}
434440

435441
#[test]
@@ -552,7 +558,7 @@ mod tests {
552558
)
553559
.unwrap();
554560

555-
assert_eq!(diff.hunks[0].lines[1].content, "test");
561+
assert_eq!(&*diff.hunks[0].lines[1].content, "test");
556562
}
557563

558564
#[test]

asyncgit/src/sync/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -332,7 +332,7 @@ mod tests {
332332
let diff =
333333
get_diff(repo_path, "test.txt", true, None).unwrap();
334334
assert_eq!(
335-
diff.hunks[0].lines[0].content,
335+
&*diff.hunks[0].lines[0].content,
336336
String::from("@@ -1 +1 @@\n")
337337
);
338338
}

src/components/diff.rs

Lines changed: 2 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -417,15 +417,12 @@ impl DiffComponent {
417417
}
418418
};
419419

420-
let trimmed =
421-
line.content.trim_matches(|c| c == '\n' || c == '\r');
422-
423420
let filled = if selected {
424421
// selected line
425-
format!("{:w$}\n", trimmed, w = width as usize)
422+
format!("{:w$}\n", line.content, w = width as usize)
426423
} else {
427424
// weird eof missing eol line
428-
format!("{}\n", trimmed)
425+
format!("{}\n", line.content)
429426
};
430427

431428
Spans::from(vec![

0 commit comments

Comments
 (0)