Giter Site home page Giter Site logo

clutch's Introduction

Clutch

Idea

This is an attempt to implement AOP (Aspect Oriented Programming) approches on data structures. The final target of the project is yet unknown. One of the outcomes of the approach is high performance json serializer that outperforms the one from System.Text.Json in many scenarious.

So, it is supposed that the data is presented as a set of interfaces like this

interface IRoot
{
    int IntProperty { get; set; } 
    IChild Child { get; set; }
}

interface IChild
{
    string StrProperty {get;set;}
    IList<string> { get; }
    int? NullableInt { get; set; }
}

We also support classes with virtual properties but approach with the interfaces seems to be more elegant. So, idea of the project is that the Clutch framework will generate implementation of the interfaces under the hood. The implementation is done by using System.Reflection.Emit namespace. Implemented objects may receive different aspects depending on configuration. Configuration may look like:

var context = ClutchContext.CreateContext(c =>
{
    c.UseNotifyPropertyChanged(NotifyPropertyChangedBehavior.ImplementOnlyOnEntitiesWithEnabledProperties);

    c.Entity<IChild>(c =>
    {
        c.Property(e => e.StrProperty).
            HasDefaultValue("").
            UsePropertySetterMode(PropertySetterMode.CompareAndSet).
            EnableNotifyPropertyChanged(true);
        c.Property(e => e.NullableInt).
            HasDefaultValue(null).
            UsePropertySetterMode(PropertySetterMode.Set).
            EnableNotifyPropertyChanged(true);
    });
    c.Entity<IRoot>().AnyProperty().EnableNotifyPropertyChanged(true);

}, out var issueList);

Based on the configuration, the implementation of interfaces will be generated. Then you can do like:

bool raised = false;
var entity = context.Create<IRoot>();
((INotifyPropertyChanged)entity).PropertyChanged += (e, s) => raised = true;

entity.IntProperty = 42;
Assert.IsTrue(raised);

Performance

Credits to Benchmark.Net project, the following setup was used (some details omitted for clarity):

    public interface IEntity
    {
        int Test1 { get; set; }
        int Test2 { get; set; }
        int Test3 { get; set; }
        string String1 { get; set; }
        string String2 { get; set; }
        string String3 { get; set; }
    }

    public class Serialization
    {
        private IEntity[] _array;
        private ClutchContext _context;

        [GlobalSetup]
        public void Setup()
        {
            _context = ClutchContext.CreateContext(b => { b.Entity<IEntity>(); }, out _);
            _array = InitArray(10000);
        }

        private static ObjectMaterializationClass[] InitArray(int amount) {...}


        [Benchmark]
        public void Clutch_SerializeDeserialize()
        {
            var str = _context.Serialize(_contextArray);
            _ = _context.Deserialize<IEntity>(str);
        }
    }

Here is performance benchmark on serializing a list of 10000 objects with 3 string and 3 integer properties set:

Method Toolchain Mean
NewtonsoftJsonNet_Serialize .NET 6.0 14.907 ms
NewtonsoftJsonNet_SerializeAndDeserialize .NET 6.0 132.012 ms
SystemTextJson_Serialize .NET 6.0 3.622 ms
SystemTextJson_SerializeDeserialize .NET 6.0 11.184 ms
Clutch_Serialize .NET 6.0 2.228 ms
Clutch_SerializeDeserialize .NET 6.0 9.798 ms
Clutch_SerializeDeserializeWithBuildingContext .NET 6.0 25.934 ms
----------------------------------------------- --------------------- -----------:
NewtonsoftJsonNet_Serialize .NET Framework 4.7.2 19.872 ms
NewtonsoftJsonNet_SerializeAndDeserialize .NET Framework 4.7.2 150.681 ms
SystemTextJson_Serialize .NET Framework 4.7.2 8.639 ms
SystemTextJson_SerializeDeserialize .NET Framework 4.7.2 24.103 ms
Clutch_Serialize .NET Framework 4.7.2 5.683 ms
Clutch_SerializeDeserialize .NET Framework 4.7.2 24.167 ms
Clutch_SerializeDeserializeWithBuildingContext .NET Framework 4.7.2 33.751 ms

clutch's People

Contributors

kapainguild avatar

Watchers

 avatar

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.