From 5584b323de5475eb30a818593cff5a2eaba6627f Mon Sep 17 00:00:00 2001 From: leo Date: Tue, 17 Nov 2020 19:31:39 +0800 Subject: [PATCH] optimize: using StringBuilder instead of TextBox.AppendText --- src/UI/Blame.xaml.cs | 9 +++++---- src/UI/DiffViewer.xaml.cs | 26 ++++++++++++++++---------- 2 files changed, 21 insertions(+), 14 deletions(-) diff --git a/src/UI/Blame.xaml.cs b/src/UI/Blame.xaml.cs index 3aa88969..2d3e04ad 100644 --- a/src/UI/Blame.xaml.cs +++ b/src/UI/Blame.xaml.cs @@ -1,6 +1,7 @@ using System; using System.Collections.Generic; using System.Globalization; +using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; @@ -55,7 +56,7 @@ namespace SourceGit.UI { content.Document.Blocks.Clear(); if (blame.IsBinary) { - lineNumber.Text = "0"; + lineNumber.Text = ""; Paragraph p = new Paragraph(new Run("BINARY FILE BLAME NOT SUPPORTED!!!")); p.Margin = new Thickness(0); @@ -67,9 +68,9 @@ namespace SourceGit.UI { content.Document.Blocks.Add(p); } else { - List numbers = new List(); - for (int i = 0; i < blame.LineCount; i++) numbers.Add(i.ToString()); - lineNumber.Text = string.Join("\n", numbers); + var numbers = new StringBuilder(); + for (int i = 0; i < blame.LineCount; i++) numbers.AppendLine(i.ToString()); + lineNumber.Text = numbers.ToString(); numbers.Clear(); for (int i = 0; i < blame.Blocks.Count; i++) { diff --git a/src/UI/DiffViewer.xaml.cs b/src/UI/DiffViewer.xaml.cs index 868138eb..603284d0 100644 --- a/src/UI/DiffViewer.xaml.cs +++ b/src/UI/DiffViewer.xaml.cs @@ -1,5 +1,6 @@ using System; using System.Globalization; +using System.Text; using System.Threading.Tasks; using System.Windows; using System.Windows.Controls; @@ -119,10 +120,15 @@ namespace SourceGit.UI { leftText.Document.Blocks.Clear(); rightText.Document.Blocks.Clear(); - foreach (var b in rs.Blocks) ShowBlock(b); + var leftLineNumberBuilder = new StringBuilder(); + var rightLineNumberBuilder = new StringBuilder(); + + foreach (var b in rs.Blocks) ShowBlock(b, leftLineNumberBuilder, rightLineNumberBuilder); leftText.Document.PageWidth = minWidth + 16; rightText.Document.PageWidth = minWidth + 16; + leftLineNumber.Text = leftLineNumberBuilder.ToString(); + rightLineNumber.Text = rightLineNumberBuilder.ToString(); leftText.ScrollToHome(); }); } @@ -175,7 +181,7 @@ namespace SourceGit.UI { /// Make paragraph. /// /// - private void ShowBlock(Git.Diff.Block b) { + private void ShowBlock(Git.Diff.Block b, StringBuilder leftNumber, StringBuilder rightNumber) { var content = b.Builder.ToString(); Paragraph p = new Paragraph(new Run(content)); @@ -220,15 +226,15 @@ namespace SourceGit.UI { case Git.Diff.Side.Left: leftText.Document.Blocks.Add(p); for (int i = 0; i < b.Count; i++) { - if (b.CanShowNumber) leftLineNumber.AppendText($"{i + b.LeftStart}\n"); - else leftLineNumber.AppendText("\n"); + if (b.CanShowNumber) leftNumber.AppendLine($"{i + b.LeftStart}"); + else leftNumber.AppendLine(); } break; case Git.Diff.Side.Right: rightText.Document.Blocks.Add(p); for (int i = 0; i < b.Count; i++) { - if (b.CanShowNumber) rightLineNumber.AppendText($"{i + b.RightStart}\n"); - else rightLineNumber.AppendText("\n"); + if (b.CanShowNumber) rightNumber.AppendLine($"{i + b.RightStart}"); + else rightNumber.AppendLine(); } break; default: @@ -246,11 +252,11 @@ namespace SourceGit.UI { for (int i = 0; i < b.Count; i++) { if (b.Mode != Git.Diff.LineMode.Indicator) { - leftLineNumber.AppendText($"{i + b.LeftStart}\n"); - rightLineNumber.AppendText($"{i + b.RightStart}\n"); + leftNumber.AppendLine($"{i + b.LeftStart}"); + rightNumber.AppendLine($"{i + b.RightStart}"); } else { - leftLineNumber.AppendText("\n"); - rightLineNumber.AppendText("\n"); + leftNumber.AppendLine(); + rightNumber.AppendLine(); } } break;