Skip to content

Commit c21289e

Browse files
Remove Uri.TryParse and replace with Span<char> parsing (#5039)
* `Span<char>`-ifying ActorPath and Address parsing Abstraction of ServiceProvider, Improving Akka.DependencyInjection (#4814) * Abstraction of ServiceProvider * introduced non-breaking Akka.DependencyInjection API changes * fixed unit tests / Props bug * fixed up DelegateInjectionSpecs * Added type checking for `Props(Type type, params object[] args)` * fixed non-generic `Props()` method Co-authored-by: Aaron Stannard <[email protected]> completed work on ActorPath parsing * restore Parse performance to ActorPath.Parse * fixed `ActorPath.ToString()`
1 parent 98ef574 commit c21289e

File tree

12 files changed

+318
-71
lines changed

12 files changed

+318
-71
lines changed

src/benchmark/Akka.Benchmarks/Actor/ActorPathBenchmarks.cs

Lines changed: 19 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -16,12 +16,16 @@ public class ActorPathBenchmarks
1616
{
1717
private ActorPath x;
1818
private ActorPath y;
19+
private ActorPath _childPath;
20+
private Address _sysAdr = new Address("akka.tcp", "system", "127.0.0.1", 1337);
21+
private Address _otherAdr = new Address("akka.tcp", "system", "127.0.0.1", 1338);
1922

2023
[GlobalSetup]
2124
public void Setup()
2225
{
23-
x = new RootActorPath(new Address("akka.tcp", "system", "127.0.0.1", 1337), "user");
24-
y = new RootActorPath(new Address("akka.tcp", "system", "127.0.0.1", 1337), "system");
26+
x = new RootActorPath(_sysAdr, "user");
27+
y = new RootActorPath(_sysAdr, "system");
28+
_childPath = x / "parent" / "child";
2529
}
2630

2731
[Benchmark]
@@ -45,7 +49,19 @@ public bool ActorPath_Equals()
4549
[Benchmark]
4650
public string ActorPath_ToString()
4751
{
48-
return x.ToString();
52+
return _childPath.ToString();
53+
}
54+
55+
[Benchmark]
56+
public string ActorPath_ToSerializationFormat()
57+
{
58+
return _childPath.ToSerializationFormat();
59+
}
60+
61+
[Benchmark]
62+
public string ActorPath_ToSerializationFormatWithAddress()
63+
{
64+
return _childPath.ToSerializationFormatWithAddress(_otherAdr);
4965
}
5066
}
5167
}
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
//-----------------------------------------------------------------------
2+
// <copyright file="NameAndUidBenchmarks.cs" company="Akka.NET Project">
3+
// Copyright (C) 2009-2021 Lightbend Inc. <http://www.lightbend.com>
4+
// Copyright (C) 2013-2021 .NET Foundation <https://github.com/akkadotnet/akka.net>
5+
// </copyright>
6+
//-----------------------------------------------------------------------
7+
8+
using System;
9+
using System.Collections.Generic;
10+
using System.Text;
11+
using Akka.Actor;
12+
using Akka.Benchmarks.Configurations;
13+
using BenchmarkDotNet.Attributes;
14+
15+
namespace Akka.Benchmarks.Actor
16+
{
17+
[Config(typeof(MicroBenchmarkConfig))]
18+
public class NameAndUidBenchmarks
19+
{
20+
public const string ActorPath = "foo#11241311";
21+
22+
[Benchmark]
23+
public NameAndUid ActorCell_SplitNameAndUid()
24+
{
25+
return ActorCell.SplitNameAndUid(ActorPath);
26+
}
27+
28+
[Benchmark]
29+
public (string name, int uid) ActorCell_GetNameAndUid()
30+
{
31+
return ActorCell.GetNameAndUid(ActorPath);
32+
}
33+
}
34+
}

src/core/Akka.API.Tests/CoreAPISpec.ApproveCore.approved.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,6 +115,7 @@ namespace Akka.Actor
115115
protected bool SetChildrenTerminationReason(Akka.Actor.Internal.SuspendReason reason) { }
116116
public void SetReceiveTimeout(System.Nullable<System.TimeSpan> timeout = null) { }
117117
protected void SetTerminated() { }
118+
[System.ObsoleteAttribute("Not used. Will be removed in Akka.NET v1.5.")]
118119
public static Akka.Actor.NameAndUid SplitNameAndUid(string name) { }
119120
public virtual void Start() { }
120121
protected void Stash(Akka.Dispatch.SysMsg.SystemMessage msg) { }
@@ -1325,6 +1326,7 @@ namespace Akka.Actor
13251326
public override void Suspend() { }
13261327
protected override void TellInternal(object message, Akka.Actor.IActorRef sender) { }
13271328
}
1329+
[System.ObsoleteAttribute("Not used. Will be removed in Akka.NET v1.5.")]
13281330
public class NameAndUid
13291331
{
13301332
public NameAndUid(string name, int uid) { }

src/core/Akka.Remote/RemoteSystemDaemon.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -308,10 +308,10 @@ public override IActorRef GetChild(IEnumerable<string> name)
308308
var n = 0;
309309
while (true)
310310
{
311-
var nameAndUid = ActorCell.SplitNameAndUid(path);
312-
if (TryGetChild(nameAndUid.Name, out var child))
311+
var (s, uid) = ActorCell.GetNameAndUid(path);
312+
if (TryGetChild(s, out var child))
313313
{
314-
if (nameAndUid.Uid != ActorCell.UndefinedUid && nameAndUid.Uid != child.Path.Uid)
314+
if (uid != ActorCell.UndefinedUid && uid != child.Path.Uid)
315315
return Nobody.Instance;
316316
return n == 0 ? child : child.GetChild(name.TakeRight(n));
317317
}

src/core/Akka.Remote/Serialization/LruBoundedCache.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,7 @@ internal static class FastHash
2525
/// <returns>A 32-bit pseudo-random hash value.</returns>
2626
public static int OfString(string s)
2727
{
28-
var chars = s.ToCharArray();
28+
var chars = s.AsSpan();
2929
var s0 = 391408L; // seed value 1, DON'T CHANGE
3030
var s1 = 601258L; // seed value 2, DON'T CHANGE
3131
unchecked

src/core/Akka.Tests/Actor/ActorSelectionSpec.cs

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -76,12 +76,10 @@ private IActorRef AskNode(IActorRef node, IQuery query)
7676
{
7777
var result = node.Ask(query).Result;
7878

79-
var actorRef = result as IActorRef;
80-
if (actorRef != null)
79+
if (result is IActorRef actorRef)
8180
return actorRef;
8281

83-
var selection = result as ActorSelection;
84-
return selection != null ? Identify(selection) : null;
82+
return result is ActorSelection selection ? Identify(selection) : null;
8583
}
8684

8785
[Fact]

src/core/Akka/Actor/ActorCell.Children.cs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -387,17 +387,16 @@ public bool TryGetSingleChild(string name, out IInternalActorRef child)
387387
}
388388
else
389389
{
390-
var nameAndUid = SplitNameAndUid(name);
391-
if (TryGetChildRestartStatsByName(nameAndUid.Name, out var stats))
390+
var (s, uid) = GetNameAndUid(name);
391+
if (TryGetChildRestartStatsByName(s, out var stats))
392392
{
393-
var uid = nameAndUid.Uid;
394393
if (uid == ActorCell.UndefinedUid || uid == stats.Uid)
395394
{
396395
child = stats.Child;
397396
return true;
398397
}
399398
}
400-
else if (TryGetFunctionRef(nameAndUid.Name, nameAndUid.Uid, out var functionRef))
399+
else if (TryGetFunctionRef(s, uid, out var functionRef))
401400
{
402401
child = functionRef;
403402
return true;

src/core/Akka/Actor/ActorCell.cs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -479,10 +479,11 @@ protected void SetActorFields(ActorBase actor)
479479
actor?.Unclear();
480480
}
481481
/// <summary>
482-
/// TBD
482+
/// INTERNAL API
483483
/// </summary>
484484
/// <param name="name">TBD</param>
485485
/// <returns>TBD</returns>
486+
[Obsolete("Not used. Will be removed in Akka.NET v1.5.")]
486487
public static NameAndUid SplitNameAndUid(string name)
487488
{
488489
var i = name.IndexOf('#');
@@ -491,6 +492,19 @@ public static NameAndUid SplitNameAndUid(string name)
491492
: new NameAndUid(name.Substring(0, i), Int32.Parse(name.Substring(i + 1)));
492493
}
493494

495+
/// <summary>
496+
/// INTERNAL API
497+
/// </summary>
498+
/// <param name="name">The full name of the actor, including the UID if known</param>
499+
/// <returns>A new (string name, int uid) instance.</returns>
500+
internal static (string name, int uid) GetNameAndUid(string name)
501+
{
502+
var i = name.IndexOf('#');
503+
return i < 0
504+
? (name, UndefinedUid)
505+
: (name.Substring(0, i), SpanHacks.Parse(name.AsSpan(i + 1)));
506+
}
507+
494508
/// <summary>
495509
/// TBD
496510
/// </summary>

0 commit comments

Comments
 (0)