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

Optimize some memory allocations from ActorPath. #4351

Merged
merged 1 commit into from
Mar 25, 2020
Merged
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
Original file line number Diff line number Diff line change
Expand Up @@ -483,6 +483,7 @@ namespace Akka.Actor
public override Akka.Actor.ActorPath Parent { get; }
public override Akka.Actor.ActorPath Root { get; }
public override int CompareTo(Akka.Actor.ActorPath other) { }
public override int GetHashCode() { }
public override Akka.Actor.ActorPath WithUid(long uid) { }
}
public sealed class CoordinatedShutdown : Akka.Actor.IExtension
Expand Down
38 changes: 33 additions & 5 deletions src/core/Akka/Actor/ActorPath.cs
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ public static bool IsValidPathElement(string s)
{
return false;
}
return !s.StartsWith("$") && Validate(s.ToCharArray(), s.Length);
return !s.StartsWith("$") && Validate(s);
}

private static bool IsValidChar(char c) => (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z') ||
Expand All @@ -130,8 +130,9 @@ private static bool IsValidChar(char c) => (c >= 'a' && c <= 'z') || (c >= 'A' &
private static bool IsHexChar(char c) => (c >= 'a' && c <= 'f') || (c >= 'A' && c <= 'F') ||
(c >= '0' && c <= '9');

private static bool Validate(IReadOnlyList<char> chars, int len)
private static bool Validate(string chars)
{
int len = chars.Length;
var pos = 0;
while (pos < len)
{
Expand Down Expand Up @@ -182,8 +183,6 @@ protected ActorPath(ActorPath parentPath, string name, long uid)
public long Uid { get; }

internal static readonly string[] EmptyElements = { };
internal static readonly string[] SystemElements = { "system" };
internal static readonly string[] UserElements = { "user" };

/// <summary>
/// Gets the elements.
Expand Down Expand Up @@ -239,7 +238,23 @@ public bool Equals(ActorPath other)
if (other == null)
return false;

return Address.Equals(other.Address) && Elements.SequenceEqual(other.Elements);
if (!Address.Equals(other.Address))
return false;

ActorPath a = this;
ActorPath b = other;
for (;;)
{
if (ReferenceEquals(a, b))
return true;
else if (a == null || b == null)
return false;
else if (a.Name != b.Name)
return false;

a = a.Parent;
b = b.Parent;
}
}

/// <inheritdoc/>
Expand Down Expand Up @@ -654,6 +669,19 @@ public override ActorPath WithUid(long uid)
return new ChildActorPath(_parent, _name, uid);
}

/// <inheritdoc/>
public override int GetHashCode()
{
unchecked
{
var hash = 17;
hash = (hash * 23) ^ Address.GetHashCode();
for (ActorPath p = this; p != null; p = p.Parent)
hash = (hash * 23) ^ p.Name.GetHashCode();
return hash;
}
}

/// <inheritdoc/>
public override int CompareTo(ActorPath other)
{
Expand Down