|
| 1 | +using System; |
| 2 | +using System.Collections.Generic; |
| 3 | +using System.Diagnostics; |
| 4 | +using System.Text; |
| 5 | +using System.Threading.Tasks; |
| 6 | + |
| 7 | +namespace SourceGit.Commands |
| 8 | +{ |
| 9 | + /// <summary> |
| 10 | + /// Optimized QueryCommits implementation with batching, memory optimization, and progress feedback |
| 11 | + /// Designed for handling large repositories (1000+ commits) without UI freezing |
| 12 | + /// </summary> |
| 13 | + public class QueryCommitsOptimized : Command |
| 14 | + { |
| 15 | + private const int BATCH_SIZE = 50; // Process commits in batches |
| 16 | + private const int YIELD_FREQUENCY = 100; // Yield control every N commits |
| 17 | + |
| 18 | + public QueryCommitsOptimized(string repo, string limits, bool needFindHead = true) |
| 19 | + { |
| 20 | + WorkingDirectory = repo; |
| 21 | + Context = repo; |
| 22 | + Args = $"log --no-show-signature --decorate=full --format=%H%x00%P%x00%D%x00%aN±%aE%x00%at%x00%cN±%cE%x00%ct%x00%s {limits}"; |
| 23 | + _findFirstMerged = needFindHead; |
| 24 | + } |
| 25 | + |
| 26 | + public QueryCommitsOptimized(string repo, string filter, Models.CommitSearchMethod method, bool onlyCurrentBranch) |
| 27 | + { |
| 28 | + string search = onlyCurrentBranch ? string.Empty : "--branches --remotes "; |
| 29 | + |
| 30 | + if (method == Models.CommitSearchMethod.ByAuthor) |
| 31 | + { |
| 32 | + search += $"-i --author={filter.Quoted()}"; |
| 33 | + } |
| 34 | + else if (method == Models.CommitSearchMethod.ByCommitter) |
| 35 | + { |
| 36 | + search += $"-i --committer={filter.Quoted()}"; |
| 37 | + } |
| 38 | + else if (method == Models.CommitSearchMethod.ByMessage) |
| 39 | + { |
| 40 | + var argsBuilder = new StringBuilder(); |
| 41 | + argsBuilder.Append(search); |
| 42 | + |
| 43 | + var words = filter.Split([' ', '\t', '\r'], StringSplitOptions.RemoveEmptyEntries); |
| 44 | + foreach (var word in words) |
| 45 | + argsBuilder.Append("--grep=").Append(word.Trim().Quoted()).Append(' '); |
| 46 | + argsBuilder.Append("--all-match -i"); |
| 47 | + |
| 48 | + search = argsBuilder.ToString(); |
| 49 | + } |
| 50 | + else if (method == Models.CommitSearchMethod.ByPath) |
| 51 | + { |
| 52 | + search += $"-- {filter.Quoted()}"; |
| 53 | + } |
| 54 | + else |
| 55 | + { |
| 56 | + search = $"-G{filter.Quoted()}"; |
| 57 | + } |
| 58 | + |
| 59 | + WorkingDirectory = repo; |
| 60 | + Context = repo; |
| 61 | + Args = $"log -1000 --date-order --no-show-signature --decorate=full --format=%H%x00%P%x00%D%x00%aN±%aE%x00%at%x00%cN±%cE%x00%ct%x00%s {search}"; |
| 62 | + _findFirstMerged = false; |
| 63 | + } |
| 64 | + |
| 65 | + public async Task<List<Models.Commit>> GetResultAsync() |
| 66 | + { |
| 67 | + // Use memory-optimized collection |
| 68 | + var commits = Models.MemoryOptimizer.RentCommitList(); |
| 69 | + var stopwatch = Stopwatch.StartNew(); |
| 70 | + |
| 71 | + try |
| 72 | + { |
| 73 | + using var proc = new Process(); |
| 74 | + proc.StartInfo = CreateGitStartInfo(true); |
| 75 | + proc.Start(); |
| 76 | + |
| 77 | + var rawLines = Models.MemoryOptimizer.RentStringList(); |
| 78 | + var lineCount = 0; |
| 79 | + |
| 80 | + // Read all lines first with batching |
| 81 | + while (await proc.StandardOutput.ReadLineAsync() is { } line) |
| 82 | + { |
| 83 | + rawLines.Add(line); |
| 84 | + lineCount++; |
| 85 | + |
| 86 | + // Process in batches to prevent memory buildup |
| 87 | + if (rawLines.Count >= BATCH_SIZE) |
| 88 | + { |
| 89 | + await ProcessCommitBatch(rawLines, commits); |
| 90 | + rawLines.Clear(); |
| 91 | + |
| 92 | + // Yield control periodically to keep UI responsive |
| 93 | + if (lineCount % YIELD_FREQUENCY == 0) |
| 94 | + { |
| 95 | + await Task.Delay(1); // Allow other tasks to run |
| 96 | + |
| 97 | + // Suggest GC every 500 commits to manage memory |
| 98 | + if (lineCount % 500 == 0) |
| 99 | + { |
| 100 | + Models.MemoryOptimizer.SuggestGarbageCollection(); |
| 101 | + } |
| 102 | + } |
| 103 | + } |
| 104 | + } |
| 105 | + |
| 106 | + // Process remaining commits |
| 107 | + if (rawLines.Count > 0) |
| 108 | + { |
| 109 | + await ProcessCommitBatch(rawLines, commits); |
| 110 | + } |
| 111 | + |
| 112 | + // Return string list to pool |
| 113 | + Models.MemoryOptimizer.ReturnStringList(rawLines); |
| 114 | + |
| 115 | + await proc.WaitForExitAsync().ConfigureAwait(false); |
| 116 | + |
| 117 | + if (_findFirstMerged && !_isHeadFound && commits.Count > 0) |
| 118 | + await MarkFirstMergedAsync(commits).ConfigureAwait(false); |
| 119 | + |
| 120 | + System.Diagnostics.Debug.WriteLine($"[PERF] QueryCommitsOptimized processed {commits.Count} commits in {stopwatch.ElapsedMilliseconds}ms"); |
| 121 | + |
| 122 | + // Create a new list to return (can't return pooled list) |
| 123 | + var result = new List<Models.Commit>(commits); |
| 124 | + Models.MemoryOptimizer.ReturnCommitList(commits); |
| 125 | + |
| 126 | + return result; |
| 127 | + } |
| 128 | + catch (Exception e) |
| 129 | + { |
| 130 | + // Make sure to return pooled resources on error |
| 131 | + Models.MemoryOptimizer.ReturnCommitList(commits); |
| 132 | + App.RaiseException(Context, $"Failed to query commits. Reason: {e.Message}"); |
| 133 | + return new List<Models.Commit>(); |
| 134 | + } |
| 135 | + } |
| 136 | + |
| 137 | + private static async Task ProcessCommitBatch(List<string> lines, List<Models.Commit> commits) |
| 138 | + { |
| 139 | + await Task.Run(() => |
| 140 | + { |
| 141 | + foreach (var line in lines) |
| 142 | + { |
| 143 | + var parts = line.Split('\0'); |
| 144 | + if (parts.Length != 8) |
| 145 | + continue; |
| 146 | + |
| 147 | + var commit = CreateCommitFromParts(parts); |
| 148 | + commits.Add(commit); |
| 149 | + } |
| 150 | + }); |
| 151 | + } |
| 152 | + |
| 153 | + private static Models.Commit CreateCommitFromParts(string[] parts) |
| 154 | + { |
| 155 | + var commit = new Models.Commit() { SHA = parts[0] }; |
| 156 | + |
| 157 | + // Optimize memory allocation by reusing existing User objects |
| 158 | + commit.ParseParents(parts[1]); |
| 159 | + commit.ParseDecorators(parts[2]); |
| 160 | + commit.Author = Models.User.FindOrAdd(parts[3]); |
| 161 | + commit.AuthorTime = ulong.Parse(parts[4]); |
| 162 | + commit.Committer = Models.User.FindOrAdd(parts[5]); |
| 163 | + commit.CommitterTime = ulong.Parse(parts[6]); |
| 164 | + commit.Subject = parts[7]; |
| 165 | + |
| 166 | + return commit; |
| 167 | + } |
| 168 | + |
| 169 | + private async Task MarkFirstMergedAsync(List<Models.Commit> commits) |
| 170 | + { |
| 171 | + Args = $"log --since={commits[^1].CommitterTimeStr.Quoted()} --format=\"%H\""; |
| 172 | + |
| 173 | + var rs = await ReadToEndAsync().ConfigureAwait(false); |
| 174 | + var shas = rs.StdOut.Split(['\r', '\n'], StringSplitOptions.RemoveEmptyEntries); |
| 175 | + if (shas.Length == 0) |
| 176 | + return; |
| 177 | + |
| 178 | + var set = new HashSet<string>(shas); |
| 179 | + |
| 180 | + foreach (var c in commits) |
| 181 | + { |
| 182 | + if (set.Contains(c.SHA)) |
| 183 | + { |
| 184 | + c.IsMerged = true; |
| 185 | + break; |
| 186 | + } |
| 187 | + } |
| 188 | + } |
| 189 | + |
| 190 | + private bool _findFirstMerged = false; |
| 191 | + private bool _isHeadFound = false; |
| 192 | + } |
| 193 | +} |
0 commit comments