Skip to content

Commit 54b1ab1

Browse files
committed
optimise linear search by checking most recent commit first
1 parent 8e336d4 commit 54b1ab1

1 file changed

Lines changed: 29 additions & 25 deletions

File tree

GitContentSearch/GitContentSearcher.cs

Lines changed: 29 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -39,16 +39,19 @@ public void SearchContent(string filePath, string searchString, string earliestC
3939
return;
4040
}
4141

42-
int firstMatchIndex = FindFirstMatchIndex(commits, filePath, searchString);
43-
int lastMatchIndex = FindLastMatchIndex(commits, filePath, searchString, firstMatchIndex);
42+
// Search the most recent match first with FindLastMatchIndex
43+
int lastMatchIndex = FindLastMatchIndex(commits, filePath, searchString, 0);
44+
45+
// Pass lastMatchIndex to FindFirstMatchIndex to optimize the search range
46+
int firstMatchIndex = FindFirstMatchIndex(commits, filePath, searchString, lastMatchIndex);
4447

4548
LogResults(firstMatchIndex, lastMatchIndex, commits, searchString);
4649
}
4750

48-
private int FindFirstMatchIndex(string[] commits, string filePath, string searchString)
51+
private int FindFirstMatchIndex(string[] commits, string filePath, string searchString, int lastMatchIndex)
4952
{
5053
int left = 0;
51-
int right = commits.Length - 1;
54+
int right = lastMatchIndex; // Use lastMatchIndex as the upper bound
5255
int? firstMatchIndex = null;
5356

5457
while (left <= right)
@@ -81,18 +84,7 @@ private int FindFirstMatchIndex(string[] commits, string filePath, string search
8184
}
8285
else
8386
{
84-
if (!_disableLinearSearch)
85-
{
86-
// Use the linear search helper
87-
int? linearSearchResult = PerformLinearSearch(commits, filePath, searchString, left, mid - 1);
88-
if (linearSearchResult.HasValue)
89-
{
90-
firstMatchIndex = linearSearchResult;
91-
break;
92-
}
93-
}
94-
95-
left = mid + 1;
87+
left = mid + 1; // Search to the right
9688
}
9789

9890
_fileManager.DeleteTempFile(tempFileName);
@@ -137,7 +129,18 @@ private int FindLastMatchIndex(string[] commits, string filePath, string searchS
137129
}
138130
else
139131
{
140-
right = mid - 1;
132+
// If not found and linear search is enabled, check remaining commits with linear search
133+
if (!_disableLinearSearch)
134+
{
135+
int? linearSearchResult = PerformLinearSearch(commits, filePath, searchString, mid + 1, right, reverse: true);
136+
if (linearSearchResult.HasValue)
137+
{
138+
lastMatchIndex = linearSearchResult;
139+
break;
140+
}
141+
}
142+
143+
right = mid - 1; // Continue searching to the left
141144
}
142145

143146
_fileManager.DeleteTempFile(tempFileName);
@@ -146,11 +149,13 @@ private int FindLastMatchIndex(string[] commits, string filePath, string searchS
146149
return lastMatchIndex ?? -1;
147150
}
148151

149-
private int? PerformLinearSearch(string[] commits, string filePath, string searchString, int left, int right)
152+
private int? PerformLinearSearch(string[] commits, string filePath, string searchString, int left, int right, bool reverse = false)
150153
{
151-
int? matchIndex = null;
154+
int step = reverse ? -1 : 1; // Use step to control direction of iteration
155+
int start = reverse ? right : left;
156+
int end = reverse ? left : right;
152157

153-
for (int i = left; i <= right; i++)
158+
for (int i = start; reverse ? i >= end : i <= end; i += step)
154159
{
155160
string commit = commits[i];
156161
string tempFileName = _fileManager.GenerateTempFileName(commit, filePath);
@@ -172,16 +177,15 @@ private int FindLastMatchIndex(string[] commits, string filePath, string searchS
172177
_logWriter.WriteLine($"Linear search commit: {commit} at {commitTime}, found: {found}");
173178
_logWriter.Flush();
174179

180+
_fileManager.DeleteTempFile(tempFileName); // Always clean up the temp file
181+
175182
if (found)
176183
{
177-
matchIndex = i; // Update matchIndex when a match is found
178-
break; // Stop search on first match (can be modified for last match)
184+
return i; // Return the index as soon as a match is found
179185
}
180-
181-
_fileManager.DeleteTempFile(tempFileName);
182186
}
183187

184-
return matchIndex;
188+
return null; // Return null if no match is found
185189
}
186190

187191
private string GetCommitTime(string commit)

0 commit comments

Comments
 (0)