optimize<User>: reduce memory used by commit's author/committer data

This commit is contained in:
leo 2023-10-10 11:25:57 +08:00
parent d9afb798db
commit 766f24f4b0
14 changed files with 91 additions and 41 deletions

View file

@ -8,6 +8,8 @@ namespace SourceGit.Commands {
/// </summary>
public class Blame : Command {
private static readonly Regex REG_FORMAT = new Regex(@"^\^?([0-9a-f]+)\s+.*\((.*)\s+(\d+)\s+[\-\+]?\d+\s+\d+\) (.*)");
private static readonly DateTime UTC_START = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime();
private Data data = new Data();
private bool needUnifyCommitSHA = false;
private int minSHALen = 0;
@ -53,7 +55,7 @@ namespace SourceGit.Commands {
var author = match.Groups[2].Value;
var timestamp = int.Parse(match.Groups[3].Value);
var content = match.Groups[4].Value;
var when = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(timestamp).ToLocalTime().ToString("yyyy/MM/dd");
var when = UTC_START.AddSeconds(timestamp).ToString("yyyy/MM/dd");
var blameLine = new Models.BlameLine() {
LineNumber = $"{data.Lines.Count + 1}",

View file

@ -61,9 +61,9 @@ namespace SourceGit.Commands {
}
private string ParseTrackStatus(string data) {
if (string.IsNullOrEmpty(data)) return "";
if (string.IsNullOrEmpty(data)) return string.Empty;
string track = "";
string track = string.Empty;
var ahead = REG_AHEAD.Match(data);
if (ahead.Success) {

View file

@ -145,7 +145,7 @@ namespace SourceGit.Commands {
proc.Start();
} catch (Exception e) {
return new ReadToEndResult() {
Output = "",
Output = string.Empty,
Error = e.Message,
IsSuccess = false,
};

View file

@ -75,9 +75,17 @@ namespace SourceGit.Commands {
} else if (line.StartsWith("parent ", StringComparison.Ordinal)) {
current.Parents.Add(line.Substring("parent ".Length));
} else if (line.StartsWith("author ", StringComparison.Ordinal)) {
current.Author.Parse(line);
Models.User user = Models.User.Invalid;
ulong time = 0;
Models.Commit.ParseUserAndTime(line, ref user, ref time);
current.Author = user;
current.AuthorTime = time;
} else if (line.StartsWith("committer ", StringComparison.Ordinal)) {
current.Committer.Parse(line);
Models.User user = Models.User.Invalid;
ulong time = 0;
Models.Commit.ParseUserAndTime(line, ref user, ref time);
current.Committer = user;
current.CommitterTime = time;
} else if (string.IsNullOrEmpty(current.Subject)) {
current.Subject = line.Trim();
} else {
@ -129,7 +137,7 @@ namespace SourceGit.Commands {
}
private void MarkFirstMerged() {
Args = $"log --since=\"{commits.Last().Committer.Time}\" --format=\"%H\"";
Args = $"log --since=\"{commits.Last().CommitterTimeStr}\" --format=\"%H\"";
var rs = ReadToEnd();
var shas = rs.Output.Split(new char[] { '\n' }, StringSplitOptions.RemoveEmptyEntries);

View file

@ -37,7 +37,11 @@ namespace SourceGit.Commands {
} else if (line.StartsWith("Reflog message: ", StringComparison.Ordinal)) {
current.Message = line.Substring(16);
} else if (line.StartsWith("author ", StringComparison.Ordinal)) {
current.Author.Parse(line);
Models.User user = Models.User.Invalid;
ulong time = 0;
Models.Commit.ParseUserAndTime(line, ref user, ref time);
current.Author = user;
current.Time = time;
}
}
}

View file

@ -1,4 +1,6 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows;
namespace SourceGit.Models {
@ -6,16 +8,32 @@ namespace SourceGit.Models {
/// 提交记录
/// </summary>
public class Commit {
public string SHA { get; set; } = "";
private static readonly Regex REG_USER_FORMAT = new Regex(@"\w+ (.*) <(.*)> (\d{10}) [\+\-]\d+");
private static readonly DateTime UTC_START = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime();
public string SHA { get; set; } = string.Empty;
public string ShortSHA => SHA.Substring(0, 8);
public User Author { get; set; } = new User();
public User Committer { get; set; } = new User();
public string Subject { get; set; } = "";
public string Message { get; set; } = "";
public User Author { get; set; } = User.Invalid;
public ulong AuthorTime { get; set; } = 0;
public User Committer { get; set; } = User.Invalid;
public ulong CommitterTime { get; set; } = 0;
public string Subject { get; set; } = string.Empty;
public string Message { get; set; } = string.Empty;
public List<string> Parents { get; set; } = new List<string>();
public List<Decorator> Decorators { get; set; } = new List<Decorator>();
public bool HasDecorators => Decorators.Count > 0;
public bool IsMerged { get; set; } = false;
public Thickness Margin { get; set; } = new Thickness(0);
public string AuthorTimeStr => UTC_START.AddSeconds(AuthorTime).ToString("yyyy-MM-dd HH:mm:ss");
public string CommitterTimeStr => UTC_START.AddSeconds(CommitterTime).ToString("yyyy-MM-dd HH:mm:ss");
public static void ParseUserAndTime(string data, ref User user, ref ulong time) {
var match = REG_USER_FORMAT.Match(data);
if (!match.Success) return;
user = User.FindOrAdd(match.Groups[1].Value, match.Groups[2].Value);
time = ulong.Parse(match.Groups[3].Value);
}
}
}

View file

@ -1,11 +1,18 @@
using System;
namespace SourceGit.Models {
/// <summary>
/// 贮藏
/// </summary>
public class Stash {
private static readonly DateTime UTC_START = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime();
public string Name { get; set; } = "";
public string SHA { get; set; } = "";
public User Author { get; set; } = new User();
public User Author { get; set; } = User.Invalid;
public ulong Time { get; set; } = 0;
public string Message { get; set; } = "";
public string TimeStr => UTC_START.AddSeconds(Time).ToString("yyyy-MM-dd HH:mm:ss");
}
}

View file

@ -1,26 +1,36 @@
using System;
using System.Text.RegularExpressions;
using System.Collections.Generic;
namespace SourceGit.Models {
/// <summary>
/// Git用户
/// </summary>
public class User {
private static readonly Regex REG_FORMAT = new Regex(@"\w+ (.*) <(.*)> (\d{10}) [\+\-]\d+");
public static User Invalid = new User();
public static Dictionary<string, User> Caches = new Dictionary<string, User>();
public string Name { get; set; } = "";
public string Email { get; set; } = "";
public string Time { get; set; } = "";
public string Name { get; set; } = string.Empty;
public string Email { get; set; } = string.Empty;
public void Parse(string data) {
var match = REG_FORMAT.Match(data);
if (!match.Success) return;
public override bool Equals(object obj) {
if (obj == null || !(obj is User)) return false;
var time = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(int.Parse(match.Groups[3].Value));
var other = obj as User;
return Name == other.Name && Email == other.Email;
}
Name = match.Groups[1].Value;
Email = match.Groups[2].Value;
Time = time.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss");
public override int GetHashCode() {
return base.GetHashCode();
}
public static User FindOrAdd(string name, string email) {
string key = $"{name}#&#{email}";
if (Caches.ContainsKey(key)) {
return Caches[key];
} else {
User user = new User() { Name = name, Email = email };
Caches.Add(key, user);
return user;
}
}
}
}

View file

@ -105,7 +105,7 @@
<TextBlock
Grid.Column="1"
Text="{Binding Author.Time}"
Text="{Binding AuthorTimeStr}"
Foreground="{DynamicResource Brush.FG2}"
Margin="4,0,0,0"
HorizontalAlignment="Right"/>

View file

@ -58,6 +58,7 @@ namespace SourceGit.Views {
var weekStart = today.AddSeconds(-(int)today.DayOfWeek * 3600 * 24 - today.Hour * 3600 - today.Minute * 60 - today.Second);
var weekEnd = weekStart.AddDays(7);
var month = today.Month;
var utcStart = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).ToLocalTime();
var limits = $"--branches --remotes --since=\"{today.ToString("yyyy-01-01 00:00:00")}\"";
var commits = new Commands.Commits(repo, limits).Result();
@ -65,7 +66,7 @@ namespace SourceGit.Views {
var totalCommitsMonth = 0;
var totalCommitsYear = commits.Count;
foreach (var c in commits) {
var commitTime = DateTime.Parse(c.Committer.Time);
var commitTime = utcStart.AddSeconds(c.CommitterTime);
if (commitTime.CompareTo(weekStart) >= 0 && commitTime.CompareTo(weekEnd) < 0) {
mapsWeek[(int)commitTime.DayOfWeek].Count++;
totalCommitsWeek++;

View file

@ -49,20 +49,20 @@ namespace SourceGit.Views.Widgets {
avatarAuthor.FallbackLabel = commit.Author.Name;
txtAuthorName.Text = commit.Author.Name;
txtAuthorEmail.Text = commit.Author.Email;
txtAuthorTime.Text = commit.Author.Time;
txtAuthorTime.Text = commit.AuthorTimeStr;
avatarCommitter.Email = commit.Committer.Email;
avatarCommitter.FallbackLabel = commit.Committer.Name;
txtCommitterName.Text = commit.Committer.Name;
txtCommitterEmail.Text = commit.Committer.Email;
txtCommitterTime.Text = commit.Committer.Time;
if (commit.Committer.Email == commit.Author.Email && commit.Committer.Time == commit.Author.Time) {
if (commit.Committer.Equals(commit.Author) && commit.CommitterTime == commit.AuthorTime) {
avatarCommitter.Visibility = Visibility.Hidden;
committerInfoPanel.Visibility = Visibility.Hidden;
} else {
avatarCommitter.Visibility = Visibility.Visible;
committerInfoPanel.Visibility = Visibility.Visible;
avatarCommitter.Email = commit.Committer.Email;
avatarCommitter.FallbackLabel = commit.Committer.Name;
txtCommitterName.Text = commit.Committer.Name;
txtCommitterEmail.Text = commit.Committer.Email;
txtCommitterTime.Text = commit.CommitterTimeStr;
}
if (commit.Parents.Count == 0) {

View file

@ -147,7 +147,7 @@
<DataGridTemplateColumn Width="140" IsReadOnly="True">
<DataGridTemplateColumn.CellTemplate>
<DataTemplate>
<TextBlock Text="{Binding Committer.Time}" HorizontalAlignment="Right" Margin="0,0,4,0"/>
<TextBlock Text="{Binding CommitterTimeStr}" HorizontalAlignment="Right" Margin="0,0,4,0"/>
</DataTemplate>
</DataGridTemplateColumn.CellTemplate>
</DataGridTemplateColumn>

View file

@ -404,7 +404,7 @@ namespace SourceGit.Views.Widgets {
copyInfo.Click += (o, e) => {
Clipboard.SetDataObject(string.Format(
"SHA: {0}\nTITLE: {1}\nAUTHOR: {2} <{3}>\nTIME: {4}",
commit.SHA, commit.Subject, commit.Committer.Name, commit.Committer.Email, commit.Committer.Time), true);
commit.SHA, commit.Subject, commit.Committer.Name, commit.Committer.Email, commit.CommitterTime), true);
};
menu.Items.Add(copyInfo);

View file

@ -17,14 +17,14 @@ namespace SourceGit.Views.Widgets {
avatarStart.FallbackLabel = start.Committer.Name;
avatarStart.ToolTip = start.Committer.Name;
txtStartSHA.Text = start.ShortSHA;
txtStartTime.Text = start.Committer.Time;
txtStartTime.Text = start.CommitterTimeStr;
txtStartSubject.Text = start.Subject;
avatarEnd.Email = end.Committer.Email;
avatarEnd.FallbackLabel = end.Committer.Name;
avatarEnd.ToolTip = end.Committer.Name;
txtEndSHA.Text = end.ShortSHA;
txtEndTime.Text = end.Committer.Time;
txtEndTime.Text = end.CommitterTimeStr;
txtEndSubject.Text = end.Subject;
Task.Run(() => {