Taudris
Taudris

Reputation: 1443

Mercurial CLI is slow in C#?

I'm writing a utility in C# that will make managing multiple Mercurial repositories easier for the way my team is using it. However, it seems that there is always about a 300 to 400 millisecond delay before I get anything back from hg.exe. I'm using the code below to run hg.exe and hgtk.exe (TortoiseHg's GUI). The code currently includes a Stopwatch and some variables for timing purposes. The delay is roughly the same on multiple runs within the same session. I have also tried specifying the exact path of hg.exe, and got the same result.

static string RunCommand(string executable, string path, string arguments)
{
    var psi = new ProcessStartInfo()
    {
        FileName = executable,
        Arguments = arguments,
        WorkingDirectory = path,

        UseShellExecute = false,
        RedirectStandardError = true,
        RedirectStandardInput = true,
        RedirectStandardOutput = true,

        WindowStyle = ProcessWindowStyle.Maximized,
        CreateNoWindow = true
    };

    var sbOut = new StringBuilder();
    var sbErr = new StringBuilder();

    var sw = new Stopwatch();

    sw.Start();

    var process = Process.Start(psi);

    TimeSpan firstRead = TimeSpan.Zero;

    process.OutputDataReceived += (s, e) =>
    {
        if (firstRead == TimeSpan.Zero)
        {
            firstRead = sw.Elapsed;
        }

        sbOut.Append(e.Data);
    };
    process.ErrorDataReceived += (s, e) => sbErr.Append(e.Data);

    process.BeginOutputReadLine();
    process.BeginErrorReadLine();

    var eventsStarted = sw.Elapsed;

    process.WaitForExit();

    var processExited = sw.Elapsed;

    sw.Reset();

    if (process.ExitCode != 0 || sbErr.Length > 0)
    {
        Error.Mercurial(process.ExitCode, sbOut.ToString(), sbErr.ToString());
    }

    return sbOut.ToString();
}

Any ideas on how I can speed things up? As it is, I'm going to have to do a lot of caching in addition to threading to keep the UI snappy.

Upvotes: 1

Views: 315

Answers (1)

Lasse V. Karlsen
Lasse V. Karlsen

Reputation: 391724

Well, here's the deal.

The mercurial command line program contains overhead, that's about it.

This command:

hg --quiet version

Which doesn't look at any repository at all, takes 195ms on average on my machine to execute. You're not going to be able to minimize this a lot closer to zero when you start involving repositories and changesets.

Why do you have 26 repositories? Why do you need to know which branch they're on? Are you using named branches and multiple repositores at the same time?

Upvotes: 2

Related Questions