Skip to content

Commit c0ff64c

Browse files
committedJan 9, 2025·
improve init-only fields handling, setup bench
1 parent 3bda763 commit c0ff64c

14 files changed

+679
-27
lines changed
 
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<LangVersion>preview</LangVersion>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
13+
<PackageReference Include="DeepCloner" Version="0.10.4" />
14+
<PackageReference Include="DeepCopier" Version="1.0.4" />
15+
<PackageReference Include="DeepCopy" Version="1.0.3" />
16+
<PackageReference Include="DeepCopy.Expression" Version="1.4.2" />
17+
<PackageReference Include="FastDeepCloner" Version="1.3.6" />
18+
</ItemGroup>
19+
20+
<ItemGroup>
21+
<ProjectReference Include="..\FastCloner.Contrib\FastCloner.Contrib.csproj" />
22+
<ProjectReference Include="..\FastCloner\FastCloner.csproj" />
23+
</ItemGroup>
24+
25+
</Project>

‎FastCloner.Benchmark/Minimal.cs

+76
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,76 @@
1+
using BenchmarkDotNet.Attributes;
2+
using Force.DeepCloner;
3+
4+
namespace FastCloner.Benchmark;
5+
6+
[RankColumn]
7+
[Orderer(BenchmarkDotNet.Order.SummaryOrderPolicy.FastestToSlowest)]
8+
[MemoryDiagnoser]
9+
public class Minimal
10+
{
11+
private TestObject testData;
12+
13+
[GlobalSetup]
14+
public void Setup()
15+
{
16+
testData = new TestObject
17+
{
18+
Id = 1,
19+
Name = "Test",
20+
NestedObject = new NestedObject
21+
{
22+
Value = 42,
23+
Description = "Nested test"
24+
}
25+
};
26+
}
27+
28+
[Benchmark(Baseline = true)]
29+
public object? FastCloner()
30+
{
31+
return global::FastCloner.FastCloner.DeepClone(testData);
32+
}
33+
34+
[Benchmark]
35+
public object? DeepCopier()
36+
{
37+
return global::DeepCopier.Copier.Copy(testData);
38+
}
39+
40+
[Benchmark]
41+
public object? DeepCopy()
42+
{
43+
return global::DeepCopy.DeepCopier.Copy(testData);
44+
}
45+
46+
[Benchmark]
47+
public object DeepCopyExpression()
48+
{
49+
return global::DeepCopy.ObjectCloner.Clone(testData);
50+
}
51+
52+
[Benchmark]
53+
public object? FastDeepCloner()
54+
{
55+
return global::FastDeepCloner.DeepCloner.Clone(testData);
56+
}
57+
58+
[Benchmark]
59+
public object? DeepCloner()
60+
{
61+
return testData.DeepClone();
62+
}
63+
64+
public class TestObject
65+
{
66+
public int Id { get; set; }
67+
public string Name { get; set; }
68+
public NestedObject NestedObject { get; set; }
69+
}
70+
71+
public class NestedObject
72+
{
73+
public int Value { get; set; }
74+
public string Description { get; set; }
75+
}
76+
}

‎FastCloner.Benchmark/Program.cs

+19
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
using BenchmarkDotNet.Attributes;
2+
using BenchmarkDotNet.Configs;
3+
using BenchmarkDotNet.Reports;
4+
using BenchmarkDotNet.Running;
5+
6+
namespace FastCloner.Benchmark;
7+
8+
class Program
9+
{
10+
static void Main(string[] args)
11+
{
12+
// this is used only for package "FastDeepCloner" which is not properly packed; todo: fork it and pack for a fair bench
13+
ManualConfig config = DefaultConfig.Instance
14+
.WithOptions(ConfigOptions.DisableOptimizationsValidator);
15+
16+
Summary summary = BenchmarkRunner.Run<Minimal>(config);
17+
Console.WriteLine(summary);
18+
}
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,17 @@
1+
<Project Sdk="Microsoft.NET.Sdk">
2+
3+
<PropertyGroup>
4+
<OutputType>Exe</OutputType>
5+
<TargetFramework>net8.0</TargetFramework>
6+
<LangVersion>preview</LangVersion>
7+
<ImplicitUsings>enable</ImplicitUsings>
8+
<Nullable>enable</Nullable>
9+
</PropertyGroup>
10+
11+
<ItemGroup>
12+
<ProjectReference Include="..\FastCloner.SourceGenerator\FastCloner.SourceGenerator.csproj"
13+
OutputItemType="Analyzer"
14+
ReferenceOutputAssembly="false" />
15+
</ItemGroup>
16+
17+
</Project>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,29 @@
1+
namespace FastCloner.SourceGenerator.Console;
2+
using System;
3+
4+
class Program
5+
{
6+
7+
public class Person
8+
{
9+
public string Name { get; set; }
10+
public int Age { get; set; }
11+
public List<string> Hobbies { get; set; }
12+
}
13+
14+
15+
static void Main(string[] args)
16+
{
17+
Person person = new Person
18+
{
19+
Name = "John",
20+
Age = 30,
21+
Hobbies = new List<string> { "Reading", "Gaming" }
22+
};
23+
24+
/*var clone = PersonClone.Clone(person);
25+
26+
Console.WriteLine($"Original: {person.Name}, {person.Age}");
27+
Console.WriteLine($"Clone: {clone.Name}, {clone.Age}");*/
28+
}
29+
}

0 commit comments

Comments
 (0)
Please sign in to comment.