optimize<*>: reduce repository loading time

This commit is contained in:
leo 2023-10-12 12:02:41 +08:00
parent 738daddbc7
commit 918eb48663
8 changed files with 38 additions and 43 deletions

View file

@ -6,7 +6,6 @@ namespace SourceGit.Models {
public string Name { get; set; }
public string FullName { get; set; }
public string Head { get; set; }
public string HeadSubject { get; set; }
public bool IsLocal { get; set; }
public bool IsCurrent { get; set; }
public string Upstream { get; set; }

View file

@ -1,6 +1,5 @@
using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;
using System.Windows;
namespace SourceGit.Models {
@ -8,7 +7,6 @@ namespace SourceGit.Models {
/// 提交记录
/// </summary>
public class Commit {
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;
@ -31,11 +29,12 @@ namespace SourceGit.Models {
public string CommitterTimeShortStr => UTC_START.AddSeconds(CommitterTime).ToString("yyyy/MM/dd");
public static void ParseUserAndTime(string data, ref User user, ref ulong time) {
var match = REG_USER_FORMAT.Match(data);
if (!match.Success) return;
var userEndIdx = data.IndexOf('>');
if (userEndIdx < 0) return;
user = User.FindOrAdd(match.Groups[1].Value, match.Groups[2].Value);
time = ulong.Parse(match.Groups[3].Value);
var timeEndIdx = data.IndexOf(' ', userEndIdx + 2);
user = User.FindOrAdd(data.Substring(0, userEndIdx));
time = timeEndIdx < 0 ? 0 : ulong.Parse(data.Substring(userEndIdx + 2, timeEndIdx - userEndIdx - 2));
}
}
}

View file

@ -22,13 +22,16 @@ namespace SourceGit.Models {
return base.GetHashCode();
}
public static User FindOrAdd(string name, string email) {
string key = $"{name}#&#{email}";
if (Caches.ContainsKey(key)) {
return Caches[key];
public static User FindOrAdd(string data) {
if (Caches.ContainsKey(data)) {
return Caches[data];
} else {
var nameEndIdx = data.IndexOf('<');
var name = nameEndIdx >= 2 ? data.Substring(0, nameEndIdx - 1) : string.Empty;
var email = data.Substring(nameEndIdx + 1);
User user = new User() { Name = name, Email = email };
Caches.Add(key, user);
Caches.Add(data, user);
return user;
}
}