Skip to content

Commit 8309c06

Browse files
committed
reduce to fastcloner vs deepcloner
internals rebase use generated fastcloner code group benchmarks, simplify position tracking work on perf remove experiment return original DeepCloner into source code until we sort this out for reproducible benchmarks remove behaviors, add licenses
1 parent 40e243b commit 8309c06

28 files changed

+1841
-1812
lines changed

benchmarks/DeepCloneBenchmarks.cs

Lines changed: 92 additions & 131 deletions
Large diffs are not rendered by default.

benchmarks/Foundatio.Benchmarks.csproj

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,8 @@
1111
<Optimize>true</Optimize>
1212
<Configuration>Release</Configuration>
1313
<IsPackable>false</IsPackable>
14+
<SignAssembly>true</SignAssembly>
15+
<AssemblyOriginatorKeyFile>..\build\Foundatio.snk</AssemblyOriginatorKeyFile>
1416
</PropertyGroup>
1517
<ItemGroup>
1618
<PackageReference Include="BenchmarkDotNet" Version="0.15.8" />

src/Foundatio/DeepCloner/DeepClonerExtensions.cs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
1-
#define NETCORE
1+
#define NETCORE
22
using System;
33
using System.Security;
4-
4+
using Foundatio.DeepCloner.Helpers;
55
using Foundatio.Force.DeepCloner.Helpers;
66

77
namespace Foundatio.Force.DeepCloner
@@ -71,7 +71,7 @@ private static bool PermissionCheck()
7171
{
7272
return false;
7373
}
74-
74+
7575
return true;
7676
}
7777
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
#define NETCORE
2+
using System;
3+
using Foundatio.Force.DeepCloner.Helpers;
4+
5+
namespace Foundatio.DeepCloner.Helpers;
6+
7+
internal static class ShallowClonerGenerator
8+
{
9+
public static T CloneObject<T>(T obj)
10+
{
11+
// this is faster than typeof(T).IsValueType
12+
if (obj is ValueType)
13+
{
14+
if (typeof(T) == obj.GetType())
15+
return obj;
16+
17+
// we're here so, we clone value type obj as object type T
18+
// so, we need to copy it, bcs we have a reference, not real object.
19+
return (T)ShallowObjectCloner.CloneObject(obj);
20+
}
21+
22+
if (ReferenceEquals(obj, null))
23+
return (T)(object)null;
24+
25+
if (DeepClonerSafeTypes.CanReturnSameObject(obj.GetType()))
26+
return obj;
27+
28+
return (T)ShallowObjectCloner.CloneObject(obj);
29+
}
30+
}

src/Foundatio/DeepCloner/LICENSE

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
The MIT License (MIT)
2+
3+
Copyright (c) 2016 force
4+
5+
Permission is hereby granted, free of charge, to any person obtaining a copy
6+
of this software and associated documentation files (the "Software"), to deal
7+
in the Software without restriction, including without limitation the rights
8+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9+
copies of the Software, and to permit persons to whom the Software is
10+
furnished to do so, subject to the following conditions:
11+
12+
The above copyright notice and this permission notice shall be included in all
13+
copies or substantial portions of the Software.
14+
15+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21+
SOFTWARE.
Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
using System.Diagnostics.CodeAnalysis;
2-
using Foundatio.FastCloner.Code;
32

43
namespace Foundatio.Utility;
54

@@ -8,6 +7,6 @@ public static class ObjectExtensions
87
[return: NotNullIfNotNull(nameof(original))]
98
public static T? DeepClone<T>(this T? original)
109
{
11-
return FastClonerGenerator.CloneObject(original);
10+
return Foundatio.FastCloner.Code.FastClonerGenerator.CloneObject(original);
1211
}
1312
}

src/Foundatio/FastCloner/Code/AhoCorasick.cs

Lines changed: 9 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,10 @@
1-
#nullable enable
2-
using System;
1+
/* This code was generated by FastCloner.Internalization.Builder, do not edit manually
2+
FastCloner is licensed under the MIT licence. https://github.com/lofcz/FastCloner
3+
*/
4+
35
using System.Collections.Generic;
4-
using System.Collections.Concurrent;
5-
using System.Linq;
6-
using System.Linq.Expressions;
7-
using System.Net.Http;
8-
using System.Reflection;
9-
using System.Runtime.CompilerServices;
10-
using System.Threading;
11-
namespace Foundatio.FastCloner.Code;
126

7+
namespace Foundatio.FastCloner.Code;
138
internal class AhoCorasick
149
{
1510
private class Node
@@ -21,7 +16,6 @@ private class Node
2116

2217
private readonly Node root = new Node();
2318
private readonly string[] patterns;
24-
2519
public AhoCorasick(string[] patterns)
2620
{
2721
this.patterns = patterns;
@@ -41,17 +35,17 @@ private void BuildTrie()
4135
value = new Node();
4236
current.Children[c] = value;
4337
}
38+
4439
current = value;
4540
}
46-
41+
4742
current.IsEndOfPattern = true;
4843
}
4944
}
5045

5146
private void BuildFailureLinks()
5247
{
5348
Queue<Node> queue = new Queue<Node>();
54-
5549
foreach (Node node in root.Children.Values)
5650
{
5751
node.Failure = root;
@@ -61,13 +55,10 @@ private void BuildFailureLinks()
6155
while (queue.Count > 0)
6256
{
6357
Node current = queue.Dequeue();
64-
6558
foreach (KeyValuePair<char, Node> kvp in current.Children)
6659
{
6760
queue.Enqueue(kvp.Value);
68-
6961
Node? failure = current.Failure;
70-
7162
while (failure != null && !failure.Children.ContainsKey(kvp.Key))
7263
{
7364
failure = failure.Failure;
@@ -81,21 +72,20 @@ private void BuildFailureLinks()
8172
public bool ContainsAnyPattern(string text)
8273
{
8374
Node? current = root;
84-
8575
foreach (char c in text)
8676
{
8777
while (current != null && !current.Children.ContainsKey(c))
8878
{
8979
current = current.Failure;
9080
}
91-
92-
current = current?.Children.GetValueOrDefault(c) ?? root;
9381

82+
current = current?.Children.GetValueOrDefault(c) ?? root;
9483
if (current.IsEndOfPattern)
9584
{
9685
return true;
9786
}
9887
}
88+
9989
return false;
10090
}
10191
}

0 commit comments

Comments
 (0)