Giter VIP home page Giter VIP logo

Comments (3)

tangdf avatar tangdf commented on June 11, 2024 2

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.

ReubenBond avatar ReubenBond commented on June 11, 2024

@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.

ReubenBond avatar ReubenBond commented on June 11, 2024

works for me, let's kill it. Do you want to submit a PR or should I?

from deepcopy.

Related Issues (12)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.