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

@ -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 other = obj as User;
return Name == other.Name && Email == other.Email;
}
var time = new DateTime(1970, 1, 1, 0, 0, 0, DateTimeKind.Utc).AddSeconds(int.Parse(match.Groups[3].Value));
public override int GetHashCode() {
return base.GetHashCode();
}
Name = match.Groups[1].Value;
Email = match.Groups[2].Value;
Time = time.ToLocalTime().ToString("yyyy-MM-dd HH:mm:ss");
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;
}
}
}
}