Skip to content

Commit 48bb8e9

Browse files
authored
perf: minimize temporary strings for better performance (#1255)
1 parent 53a5546 commit 48bb8e9

File tree

8 files changed

+35
-23
lines changed

8 files changed

+35
-23
lines changed

src/Converters/PathConverters.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.IO;
33

44
using Avalonia.Data.Converters;
@@ -22,7 +22,7 @@ public static class PathConverters
2222
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
2323
var prefixLen = home.EndsWith('/') ? home.Length - 1 : home.Length;
2424
if (v.StartsWith(home, StringComparison.Ordinal))
25-
return "~" + v.Substring(prefixLen);
25+
return $"~{v.AsSpan().Slice(prefixLen)}";
2626

2727
return v;
2828
});

src/Models/DiffResult.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Text;
34
using System.Text.RegularExpressions;
45

@@ -147,9 +148,9 @@ public TextDiffSelection MakeSelection(int startLine, int endLine, bool isCombin
147148
public void GenerateNewPatchFromSelection(Change change, string fileBlobGuid, TextDiffSelection selection, bool revert, string output)
148149
{
149150
var isTracked = !string.IsNullOrEmpty(fileBlobGuid);
150-
var fileGuid = isTracked ? fileBlobGuid.Substring(0, 8) : "00000000";
151+
var fileGuid = isTracked ? fileBlobGuid.AsSpan().Slice(0, 8) : "00000000".AsSpan();
151152

152-
var builder = new StringBuilder();
153+
var builder = new StringBuilder(512);
153154
builder.Append("diff --git a/").Append(change.Path).Append(" b/").Append(change.Path).Append('\n');
154155
if (!revert && !isTracked)
155156
builder.Append("new file mode 100644\n");

src/Models/Worktree.cs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using CommunityToolkit.Mvvm.ComponentModel;
1+
using System;
2+
using CommunityToolkit.Mvvm.ComponentModel;
23

34
namespace SourceGit.Models
45
{
@@ -22,12 +23,12 @@ public string Name
2223
get
2324
{
2425
if (IsDetached)
25-
return $"deteched HEAD at {Head.Substring(10)}";
26+
return $"deteched HEAD at {Head.AsSpan().Slice(10)}";
2627

27-
if (Branch.StartsWith("refs/heads/", System.StringComparison.Ordinal))
28+
if (Branch.StartsWith("refs/heads/", StringComparison.Ordinal))
2829
return Branch.Substring(11);
2930

30-
if (Branch.StartsWith("refs/remotes/", System.StringComparison.Ordinal))
31+
if (Branch.StartsWith("refs/remotes/", StringComparison.Ordinal))
3132
return Branch.Substring(13);
3233

3334
return Branch;

src/ViewModels/Archive.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.ComponentModel.DataAnnotations;
1+
using System;
2+
using System.ComponentModel.DataAnnotations;
23
using System.IO;
34
using System.Threading.Tasks;
45

@@ -31,7 +32,7 @@ public Archive(Repository repo, Models.Commit commit)
3132
{
3233
_repo = repo;
3334
_revision = commit.SHA;
34-
_saveFile = $"archive-{commit.SHA.Substring(0, 10)}.zip";
35+
_saveFile = $"archive-{commit.SHA.AsSpan().Slice(0, 10)}.zip";
3536
BasedOn = commit;
3637
}
3738

src/ViewModels/Blame.cs

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
using System.Collections.Generic;
1+
using System;
2+
using System.Collections.Generic;
23
using System.Threading.Tasks;
34

45
using Avalonia.Threading;
@@ -30,7 +31,7 @@ public Blame(string repo, string file, string revision)
3031
{
3132
_repo = repo;
3233

33-
Title = $"{file} @ {revision.Substring(0, 10)}";
34+
Title = $"{file} @ {revision.AsSpan().Slice(0, 10)}";
3435
Task.Run(() =>
3536
{
3637
var result = new Commands.Blame(repo, file, revision).Result();

src/ViewModels/Launcher.cs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.IO;
33

44
using Avalonia.Collections;
@@ -584,7 +584,7 @@ private void UpdateTitle()
584584
var home = Environment.GetFolderPath(Environment.SpecialFolder.UserProfile);
585585
var prefixLen = home.EndsWith('/') ? home.Length - 1 : home.Length;
586586
if (path.StartsWith(home, StringComparison.Ordinal))
587-
path = "~" + path.Substring(prefixLen);
587+
path = $"~{path.AsSpan().Slice(prefixLen)}";
588588
}
589589

590590
Title = $"[{workspace}] {name} ({path})";

src/Views/Histories.axaml.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,7 @@ private void OnCommitListKeyDown(object sender, KeyEventArgs e)
205205
foreach (var item in selected)
206206
{
207207
if (item is Models.Commit commit)
208-
builder.AppendLine($"{commit.SHA.Substring(0, 10)} - {commit.Subject}");
208+
builder.AppendLine($"{commit.SHA.AsSpan().Slice(0, 10)} - {commit.Subject}");
209209
}
210210

211211
App.CopyText(builder.ToString());

src/Views/TextDiffView.axaml.cs

Lines changed: 15 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -126,7 +126,7 @@ public override void Render(DrawingContext context)
126126
typeface,
127127
presenter.FontSize,
128128
presenter.Foreground);
129-
context.DrawText(txt, new Point(Bounds.Width - txt.Width, y - txt.Height * 0.5));
129+
context.DrawText(txt, new Point(Bounds.Width - txt.Width, y - (txt.Height * 0.5)));
130130
}
131131
}
132132
}
@@ -212,7 +212,7 @@ public override void Render(DrawingContext context)
212212
}
213213

214214
if (indicator != null)
215-
context.DrawText(indicator, new Point(0, y - indicator.Height * 0.5));
215+
context.DrawText(indicator, new Point(0, y - (indicator.Height * 0.5)));
216216
}
217217
}
218218
}
@@ -1047,7 +1047,8 @@ private void CopyWithoutIndicators()
10471047
// The first selected line (partial selection)
10481048
if (i == startIdx && startPosition.Column > 1)
10491049
{
1050-
builder.AppendLine(line.Content.Substring(startPosition.Column - 1));
1050+
builder.Append(line.Content.AsSpan().Slice(startPosition.Column - 1));
1051+
builder.Append(Environment.NewLine);
10511052
continue;
10521053
}
10531054

@@ -1061,7 +1062,14 @@ private void CopyWithoutIndicators()
10611062
// For the last line (selection range is within original source)
10621063
if (i == endIdx)
10631064
{
1064-
builder.Append(endPosition.Column - 1 < line.Content.Length ? line.Content.Substring(0, endPosition.Column - 1) : line.Content);
1065+
if (endPosition.Column - 1 < line.Content.Length)
1066+
{
1067+
builder.Append(line.Content.AsSpan().Slice(0, endPosition.Column - 1));
1068+
}
1069+
else
1070+
{
1071+
builder.Append(line.Content);
1072+
}
10651073
break;
10661074
}
10671075

@@ -1246,12 +1254,12 @@ protected override void OnDataContextChanged(EventArgs e)
12461254
var textDiff = DataContext as Models.TextDiff;
12471255
if (textDiff != null)
12481256
{
1249-
var builder = new StringBuilder();
1257+
var builder = new StringBuilder(512);
12501258
foreach (var line in textDiff.Lines)
12511259
{
12521260
if (line.Content.Length > 10000)
12531261
{
1254-
builder.Append(line.Content.Substring(0, 1000));
1262+
builder.Append(line.Content.AsSpan().Slice(0, 1000));
12551263
builder.Append($"...({line.Content.Length - 1000} character trimmed)");
12561264
}
12571265
else
@@ -1741,7 +1749,7 @@ static TextDiffView()
17411749
}
17421750

17431751
var top = chunk.Y + (chunk.Height >= 36 ? 16 : 4);
1744-
var right = (chunk.Combined || !chunk.IsOldSide) ? 16 : v.Bounds.Width * 0.5f + 16;
1752+
var right = (chunk.Combined || !chunk.IsOldSide) ? 16 : (v.Bounds.Width * 0.5f) + 16;
17451753
v.Popup.Margin = new Thickness(0, top, right, 0);
17461754
v.Popup.IsVisible = true;
17471755
});

0 commit comments

Comments
 (0)