Comments (3)
The test result:
Method | Mean | Error | StdDev | Gen 0 | Allocated |
---|---|---|---|---|---|
WithStaticFieldBuilder | 63.91 ns | 0.8752 ns | 0.8187 ns | 0.0075 | 24 B |
WithOutStaticFieldBuilder | 57.27 ns | 0.3871 ns | 0.3621 ns | 0.0076 | 24 B |
The code:
public class StaticFieldBuilderBenchmarks
{
public class SimpleClass
{
public SimpleClass(string input)
{
Value = input;
}
public string Value { get; set; }
}
private static readonly Func<SimpleClass> s_createSimpleClassWithStaticFieldBuilder = CreateInstance<SimpleClass>(true);
private static readonly Func<SimpleClass> s_createSimpleClassWithOutStaticFieldBuilder = CreateInstance<SimpleClass>(false);
[Benchmark]
public void WithStaticFieldBuilder()
{
s_createSimpleClassWithStaticFieldBuilder();
}
[Benchmark]
public void WithOutStaticFieldBuilder()
{
s_createSimpleClassWithOutStaticFieldBuilder();
}
private static Func<T> CreateInstance<T>(bool withStaticFieldBuilder)
{
var type = typeof(T);
var dynamicMethod = new DynamicMethod(
type.Name + "CreateInstanceWithStaticFieldBuilder",
typeof(T),
Type.EmptyTypes,
typeof(StaticFieldBuilderBenchmarks).Module,
true);
var il = dynamicMethod.GetILGenerator();
il.DeclareLocal(type);
if (withStaticFieldBuilder) {
var typeField = DeepCopier.FieldBuilder.GetOrCreateStaticField(type);
il.Emit(OpCodes.Ldsfld, typeField);
}
else {
il.Emit(OpCodes.Ldtoken, type);
il.Emit(OpCodes.Call, DeepCopier.MethodInfos.GetTypeFromHandle);
}
il.Emit(OpCodes.Call, DeepCopier.MethodInfos.GetUninitializedObject);
il.Emit(OpCodes.Castclass, type);
il.Emit(OpCodes.Stloc_0);
// Return the result.
il.Emit(OpCodes.Ldloc_0);
il.Emit(OpCodes.Ret);
return (Func<T>) dynamicMethod.CreateDelegate(typeof(Func<T>));
}
}
from deepcopy.
@tangdf true, it's not required. I haven't benchmarked with & without StaticFieldBuilder
- could you benchmark it and post a table showing the performance difference?
from deepcopy.
works for me, let's kill it. Do you want to submit a PR or should I?
from deepcopy.
Related Issues (12)
- A few enhancements HOT 8
- A problem of HashSet<> HOT 1
- Update immutable object HOT 2
- Type include interface field HOT 2
- Found a couple of issues ... HOT 2
- The issue of array copy
- crash on linux HOT 6
- Common Language Runtime detected an invalid program net6.0 HOT 1
- Clone of SimpleClass too slow HOT 13
- Not Support HashSet<>,Dictionary<> HOT 3
- ImmutabilityCheck for generic types is wrong HOT 4
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from deepcopy.