Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speed up buildconfig generation 6x (6m->1m) #20510

Draft
wants to merge 1 commit into
base: master
Choose a base branch
from
Draft
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
70 changes: 53 additions & 17 deletions BuildConfigGen/GitUtil.cs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ namespace BuildConfigGen
{
internal static class GitUtil
{
static (string? gitRoot, IEnumerable<string>? cache) NonIgnoredFileListCache;

public static void GetUntrackedFiles(string taskTarget, out IEnumerable<string> toAdd, out IEnumerable<string> toRemove)
{
Expand Down Expand Up @@ -122,40 +123,75 @@ internal static string GetGitRootPath(string currentDir)

internal static IEnumerable<string> GetNonIgnoredFileListFromPath(string gitRoot, string taskTarget)
{
string gitIgnorePathBak = Path.Combine(gitRoot, ".gitignore.bak");
string gitIgnore = Path.Combine(gitRoot, ".gitignore");
// the new version gets the non-ignored files frmo the root, caches and filters based on taskTarget. It's 6x faster
return GetNonIgnoredFileListFromPathInnerIncludingFromGeneratedLocalFilteredByTaskTarget(gitRoot, taskTarget);
}

bool needsGitIgnoreUpdate = taskTarget.Contains("/_generated_local/") || taskTarget.Contains(@"\_generated_local\");
private static IEnumerable<string> GetNonIgnoredFileListFromPathInnerIncludingFromGeneratedLocalFilteredByTaskTarget(string gitRoot, string taskTarget)
{
if (!taskTarget.StartsWith(gitRoot, StringComparison.OrdinalIgnoreCase))
{
throw new Exception($"expected taskTarget={taskTarget} to start with gitRoot={gitRoot}");
}

string? gitIgnoreContent = null;
string taskTargetSubpath = taskTarget.Substring(gitRoot.Length);

if (needsGitIgnoreUpdate)
if(NonIgnoredFileListCache.gitRoot is not null)
{
gitIgnoreContent = File.ReadAllText(gitIgnore);
const string genertedLocalPath = "_generated_local/";
NonIgnoredFileListCache.gitRoot = gitRoot;

if (!gitIgnoreContent.Contains(genertedLocalPath))
if(gitRoot!= NonIgnoredFileListCache.gitRoot)
{
throw new Exception("Expected " + genertedLocalPath + " in " + gitIgnore);
throw new Exception($"BUG: gitroot={gitRoot} expected to match {NonIgnoredFileListCache.gitRoot}=NonIgnoredFileListCache.gitRoot");
}
}

gitIgnoreContent = gitIgnoreContent.Replace(genertedLocalPath, "");

File.Copy(gitIgnore, gitIgnorePathBak, true);
if (NonIgnoredFileListCache.cache is null)
{
NonIgnoredFileListCache.cache = GetNonIgnoredFileListFromPathInnerIncludingFromGeneratedLocal(gitRoot);
}

try
return NonIgnoredFileListCache.cache.Where(x => x.StartsWith(taskTargetSubpath + Path.DirectorySeparatorChar, StringComparison.OrdinalIgnoreCase)).Select(x =>
{
if (needsGitIgnoreUpdate)
var y = x.Substring(taskTargetSubpath.Length);

if (y.StartsWith("/") || y.StartsWith(@"\"))
{
return y.Substring(1);
}
else
{
File.WriteAllText(gitIgnore, gitIgnoreContent);
throw new Exception($"BUG: expected y={y} to start with / or \\ ");
}
});
}

private static IEnumerable<string> GetNonIgnoredFileListFromPathInnerIncludingFromGeneratedLocal(string gitRoot)
{
string gitIgnorePathBak = Path.Combine(gitRoot, ".gitignore.bak");
string gitIgnore = Path.Combine(gitRoot, ".gitignore");

string? gitIgnoreContent = File.ReadAllText(gitIgnore);
const string genertedLocalPath = "_generated_local/";

if (!gitIgnoreContent.Contains(genertedLocalPath))
{
throw new Exception("Expected " + genertedLocalPath + " in " + gitIgnore);
}

gitIgnoreContent = gitIgnoreContent.Replace(genertedLocalPath, "");

File.Copy(gitIgnore, gitIgnorePathBak, true);

try
{
File.WriteAllText(gitIgnore, gitIgnoreContent);

return GetNonIgnoredFileListFromPathInner(taskTarget);
return GetNonIgnoredFileListFromPathInner(gitRoot);
}
finally
{
if (needsGitIgnoreUpdate)
if (File.Exists(gitIgnorePathBak))
{
File.Move(gitIgnorePathBak, gitIgnore, true);
}
Expand Down
5 changes: 4 additions & 1 deletion BuildConfigGen/Program.cs
Original file line number Diff line number Diff line change
Expand Up @@ -73,8 +73,11 @@ static void Main(string? task = null, string? configs = null, int? currentSprint
{
try
{
Stopwatch sw = new Stopwatch();
sw.Start();
ensureUpdateModeVerifier = new EnsureUpdateModeVerifier(!writeUpdates);
MainInner(task, configs, currentSprint, writeUpdates, allTasks, getTaskVersionTable, debugAgentDir, includeLocalPackagesBuildConfig);
Console.WriteLine("Elapsed=" + sw.ToString());
}
catch (Exception e2)
{
Expand Down Expand Up @@ -945,7 +948,7 @@ private static void CopyConfig(string gitRootPath, string taskTargetPathOrUnders
HashSet<string> pathsToRemoveFromOutput;

// In case if task was not generated yet, we don't need to get the list of files to remove, because taskOutput not exists yet
if (Directory.Exists(taskOutput) && !config.useAltGeneratedPath /* exclude alt which is .gitignore */)
if (Directory.Exists(taskOutput))
{
pathsToRemoveFromOutput = new HashSet<string>(GitUtil.GetNonIgnoredFileListFromPath(gitRootPath, taskOutput));
}
Expand Down