Skip to content

Commit 2f0e481

Browse files
committed
benchamrk: don't duplicate the final version's definition
1 parent 4803fd0 commit 2f0e481

File tree

2 files changed

+4
-34
lines changed

2 files changed

+4
-34
lines changed

benchmarks/LinkBuilder/LinkBuilder_ GetNamespaceFromPath_Benchmarks.cs

Lines changed: 3 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,5 @@
11
using System;
2-
using System.Diagnostics;
32
using System.Text;
4-
using System.Threading;
53
using BenchmarkDotNet.Attributes;
64
using BenchmarkDotNet.Attributes.Exporters;
75
using BenchmarkDotNet.Attributes.Jobs;
@@ -22,7 +20,7 @@ public class LinkBuilder_GetNamespaceFromPath_Benchmarks
2220
public void UsingSpanWithStringBuilder() => GetNamespaceFromPath_Using_Span_With_StringBuilder(PATH, ENTITY_NAME);
2321

2422
[Benchmark]
25-
public void UsingSpanWithNoAlloc() => GetNamespaceFromPath_Using_Span_No_Alloc(PATH, ENTITY_NAME);
23+
public void Current() => GetNameSpaceFromPath_Current(PATH, ENTITY_NAME);
2624

2725
public static string GetNamespaceFromPath_BySplitting(string path, string entityName)
2826
{
@@ -40,36 +38,8 @@ public static string GetNamespaceFromPath_BySplitting(string path, string entity
4038
return nSpace;
4139
}
4240

43-
public static string GetNamespaceFromPath_Using_Span_No_Alloc(string path, string entityName)
44-
{
45-
var entityNameSpan = entityName.AsSpan();
46-
var pathSpan = path.AsSpan();
47-
const char delimiter = '/';
48-
for (var i = 0; i < pathSpan.Length; i++)
49-
{
50-
if(pathSpan[i].Equals(delimiter))
51-
{
52-
var nextPosition = i+1;
53-
if(pathSpan.Length > i + entityNameSpan.Length)
54-
{
55-
var possiblePathSegment = pathSpan.Slice(nextPosition, entityNameSpan.Length);
56-
if (entityNameSpan.SequenceEqual(possiblePathSegment))
57-
{
58-
// check to see if it's the last position in the string
59-
// or if the next character is a /
60-
var lastCharacterPosition = nextPosition + entityNameSpan.Length;
61-
62-
if(lastCharacterPosition == pathSpan.Length || pathSpan.Length >= lastCharacterPosition + 2 && pathSpan[lastCharacterPosition + 1].Equals(delimiter))
63-
{
64-
return pathSpan.Slice(0, i).ToString();
65-
}
66-
}
67-
}
68-
}
69-
}
70-
71-
return string.Empty;
72-
}
41+
public static string GetNameSpaceFromPath_Current(string path, string entityName)
42+
=> JsonApiDotNetCore.Builders.LinkBuilder.GetNamespaceFromPath(path, entityName);
7343

7444
public static string GetNamespaceFromPath_Using_Span_With_StringBuilder(string path, string entityName)
7545
{

src/JsonApiDotNetCore/Builders/LinkBuilder.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,7 +21,7 @@ public string GetBasePath(HttpContext context, string entityName)
2121
: $"{r.Scheme}://{r.Host}{GetNamespaceFromPath(r.Path, entityName)}";
2222
}
2323

24-
private static string GetNamespaceFromPath(string path, string entityName)
24+
internal static string GetNamespaceFromPath(string path, string entityName)
2525
{
2626
var entityNameSpan = entityName.AsSpan();
2727
var pathSpan = path.AsSpan();

0 commit comments

Comments
 (0)