Skip to content

Commit

Permalink
improve init-only fields handling, setup bench
Browse files Browse the repository at this point in the history
  • Loading branch information
lofcz committed Jan 9, 2025
1 parent 3bda763 commit c0ff64c
Show file tree
Hide file tree
Showing 14 changed files with 679 additions and 27 deletions.
25 changes: 25 additions & 0 deletions FastCloner.Benchmark/FastCloner.Benchmark.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>preview</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="BenchmarkDotNet" Version="0.14.0" />
<PackageReference Include="DeepCloner" Version="0.10.4" />
<PackageReference Include="DeepCopier" Version="1.0.4" />
<PackageReference Include="DeepCopy" Version="1.0.3" />
<PackageReference Include="DeepCopy.Expression" Version="1.4.2" />
<PackageReference Include="FastDeepCloner" Version="1.3.6" />
</ItemGroup>

<ItemGroup>
<ProjectReference Include="..\FastCloner.Contrib\FastCloner.Contrib.csproj" />
<ProjectReference Include="..\FastCloner\FastCloner.csproj" />
</ItemGroup>

</Project>
76 changes: 76 additions & 0 deletions FastCloner.Benchmark/Minimal.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,76 @@
using BenchmarkDotNet.Attributes;
using Force.DeepCloner;

namespace FastCloner.Benchmark;

[RankColumn]
[Orderer(BenchmarkDotNet.Order.SummaryOrderPolicy.FastestToSlowest)]
[MemoryDiagnoser]
public class Minimal
{
private TestObject testData;

[GlobalSetup]
public void Setup()
{
testData = new TestObject
{
Id = 1,
Name = "Test",
NestedObject = new NestedObject
{
Value = 42,
Description = "Nested test"
}
};
}

[Benchmark(Baseline = true)]
public object? FastCloner()
{
return global::FastCloner.FastCloner.DeepClone(testData);
}

[Benchmark]
public object? DeepCopier()
{
return global::DeepCopier.Copier.Copy(testData);
}

[Benchmark]
public object? DeepCopy()
{
return global::DeepCopy.DeepCopier.Copy(testData);
}

[Benchmark]
public object DeepCopyExpression()
{
return global::DeepCopy.ObjectCloner.Clone(testData);
}

[Benchmark]
public object? FastDeepCloner()
{
return global::FastDeepCloner.DeepCloner.Clone(testData);
}

[Benchmark]
public object? DeepCloner()
{
return testData.DeepClone();
}

public class TestObject
{
public int Id { get; set; }
public string Name { get; set; }
public NestedObject NestedObject { get; set; }
}

public class NestedObject
{
public int Value { get; set; }
public string Description { get; set; }
}
}
19 changes: 19 additions & 0 deletions FastCloner.Benchmark/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
using BenchmarkDotNet.Attributes;
using BenchmarkDotNet.Configs;
using BenchmarkDotNet.Reports;
using BenchmarkDotNet.Running;

namespace FastCloner.Benchmark;

class Program
{
static void Main(string[] args)
{
// this is used only for package "FastDeepCloner" which is not properly packed; todo: fork it and pack for a fair bench
ManualConfig config = DefaultConfig.Instance
.WithOptions(ConfigOptions.DisableOptimizationsValidator);

Summary summary = BenchmarkRunner.Run<Minimal>(config);
Console.WriteLine(summary);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<Project Sdk="Microsoft.NET.Sdk">

<PropertyGroup>
<OutputType>Exe</OutputType>
<TargetFramework>net8.0</TargetFramework>
<LangVersion>preview</LangVersion>
<ImplicitUsings>enable</ImplicitUsings>
<Nullable>enable</Nullable>
</PropertyGroup>

<ItemGroup>
<ProjectReference Include="..\FastCloner.SourceGenerator\FastCloner.SourceGenerator.csproj"
OutputItemType="Analyzer"
ReferenceOutputAssembly="false" />
</ItemGroup>

</Project>
29 changes: 29 additions & 0 deletions FastCloner.SourceGenerator.Console/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
namespace FastCloner.SourceGenerator.Console;
using System;

class Program
{

public class Person
{
public string Name { get; set; }
public int Age { get; set; }
public List<string> Hobbies { get; set; }
}


static void Main(string[] args)
{
Person person = new Person
{
Name = "John",
Age = 30,
Hobbies = new List<string> { "Reading", "Gaming" }
};

/*var clone = PersonClone.Clone(person);
Console.WriteLine($"Original: {person.Name}, {person.Age}");
Console.WriteLine($"Clone: {clone.Name}, {clone.Age}");*/
}
}
Loading

0 comments on commit c0ff64c

Please sign in to comment.