Skip to content

Commit

Permalink
Improve performance of resolving of direct references
Browse files Browse the repository at this point in the history
  • Loading branch information
nulltoken committed May 21, 2011
1 parent b283190 commit 4b46919
Show file tree
Hide file tree
Showing 3 changed files with 27 additions and 15 deletions.
29 changes: 24 additions & 5 deletions LibGit2Sharp/DirectReference.cs
@@ -1,18 +1,37 @@
namespace LibGit2Sharp
using System;

namespace LibGit2Sharp
{
/// <summary>
/// A DirectReference points directly to a <see cref = "GitObject" />
/// </summary>
public class DirectReference : Reference
{
private readonly Func<GitObject> targetResolver;
private bool resolved;
private GitObject target;

internal DirectReference(Func<GitObject> targetResolver)
{
this.targetResolver = targetResolver;
}

/// <summary>
/// Gets the target of this <see cref = "DirectReference" />
/// </summary>
public GitObject Target { get; internal set; }

protected override object ProvideAdditionalEqualityComponent()
public GitObject Target
{
return Target;
get
{
if (resolved)
{
return target;
}

target = targetResolver();
resolved = true;
return target;
}
}

/// <summary>
Expand Down
8 changes: 3 additions & 5 deletions LibGit2Sharp/Reference.cs
Expand Up @@ -11,7 +11,7 @@ namespace LibGit2Sharp
public abstract class Reference : IEquatable<Reference>
{
private static readonly LambdaEqualityHelper<Reference> equalityHelper =
new LambdaEqualityHelper<Reference>(new Func<Reference, object>[] { x => x.CanonicalName, x => x.ProvideAdditionalEqualityComponent() });
new LambdaEqualityHelper<Reference>(new Func<Reference, object>[] { x => x.CanonicalName, x => x.TargetIdentifier });

/// <summary>
/// Gets the full name of this reference.
Expand Down Expand Up @@ -57,8 +57,8 @@ public abstract class Reference : IEquatable<Reference>
var targetId = new ObjectId(oid);
targetIdentifier = targetId.Sha;

var target = repo.Lookup(targetId);
reference = new DirectReference { CanonicalName = name, Target = target, TargetIdentifier = targetIdentifier};
var targetResolver = new Func<GitObject>(() => repo.Lookup(targetId));
reference = new DirectReference(targetResolver) { CanonicalName = name, TargetIdentifier = targetIdentifier};
break;

default:
Expand Down Expand Up @@ -88,8 +88,6 @@ public abstract class Reference : IEquatable<Reference>
Enum.GetName(typeof (GitReferenceType), type)));
}

protected abstract object ProvideAdditionalEqualityComponent();

/// <summary>
/// Recursively peels the target of the reference until a direct reference is encountered.
/// </summary>
Expand Down
5 changes: 0 additions & 5 deletions LibGit2Sharp/SymbolicReference.cs
Expand Up @@ -10,11 +10,6 @@ public class SymbolicReference : Reference
/// </summary>
public Reference Target { get; internal set; }

protected override object ProvideAdditionalEqualityComponent()
{
return Target;
}

/// <summary>
/// Recursively peels the target of the reference until a direct reference is encountered.
/// </summary>
Expand Down

0 comments on commit 4b46919

Please sign in to comment.