Quantcast
Channel: C1 CMS Foundation - Open Source on .NET
Viewing all articles
Browse latest Browse all 2540

New Post: Why is DataConnection.New() slow

$
0
0
Its this method call which is slow due to the reflection needed to resolve your interface to a concrete class and instantiate it.
var user = DataConnection.New<RegisteredUsers>();
A way to optimize it, is to only do this call once, and then emit IL code to instantiate the type. Something like this (psuodo code)
using (DataConnection connection = new DataConnection())
            {
                var userTemplate = DataConnection.New<RegisteredUsers>();
                var type = userTemplate.GetType();


                for (int i = 0; i < (100000); i++)
                {
                   // optimize this creation by emitting IL code which calls the constructor of type
                    var user = (RegisteredUsers)Activator.CreateInstance(type); 

                    user.Id = Guid.NewGuid();
                    user.Name = "John";
                    user.LastName = "Doe";
                    user.Email = "john.doe@ay.com";
                    user.Password = "rawr";
                    userList.Add(user);
                }

                connection.Add<RegisteredUsers>(userList);
            }

Viewing all articles
Browse latest Browse all 2540

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>