2
\$\begingroup\$

I'm trying to write a function which will cache objects in memory.

The function below works, but performance is unbearable. What would you recommend to improve performance?

 public void SerializeInsertCache(object item)
        {
            //Get type of item object item
            Type type = item.GetType();

            //Get all of attributes type of item object
            TypeAttributes attributesType = type.Attributes;

            //Get object type full name for cach key
            string rootName = type.Name;

            string name = type.Name;
            PropertyInfo[] properties = type.GetProperties();
            PropertyInfo idRoot = properties.Where(p => p.Name == "Id").SingleOrDefault();

            int rootId = (int)idRoot.GetValue(item);

            string rootKey = "";

            foreach (var property in properties)
            {

                string propertyName = property.Name;
                var propertyValue = property.GetValue(item);
                Type propertyType = property.PropertyType;
                string propertyTypeName = propertyType.Name;
                bool cacheStatus = true;
                SerializeInsertCache(property);//recursive function 

            }

           SaveInCache(item);
         }
\$\endgroup\$
8
  • 3
    \$\begingroup\$ Reflection is slow. Make sure you never use Reflection more than once for the same data. Use a Dictionary or something to store results, so you can do a lookup and avoid using reflection again. \$\endgroup\$
    – Ben Voigt
    Commented Jul 12, 2014 at 0:35
  • \$\begingroup\$ propertyName, propertyValue, propertyType, propertyTypeName & cacheStatus are assigned to but never used; get rid of them and you'll get some performance gain. \$\endgroup\$
    – JohnLBevan
    Commented Jul 12, 2014 at 0:52
  • \$\begingroup\$ I would rethink your strategy if possible. You don't really mention why you're caching objects, but I would imagine that if you needed to cache different Types, that the caller requesting an item from the cache would know what type it expects, and could cast appropriately. Save objects into a Dictionary<string, object>(), and retrieve with a (ExpectedType)cache.Get("key_name"). I may be way off the mark though which is why this is a comment. \$\endgroup\$
    – Josh Smeaton
    Commented Jul 12, 2014 at 2:05
  • \$\begingroup\$ As mentioned in the last comment, you seem to be setting a few variables that aren't being used for anything. Can you trim down your code sample to show what is actually important to what you are trying to accomplish? \$\endgroup\$
    – bingles
    Commented Jul 12, 2014 at 2:14
  • \$\begingroup\$ You really need to provide a more complete example. Your example provided shows that nothing is happening with the reflected data. You use reflection, loop through all of the properties, but do nothing with it. In the end, you just pass item in to the SaveInCache method, and discard everything you reflected. \$\endgroup\$
    – Johnathon Sullinger
    Commented Jul 12, 2014 at 2:18

1 Answer 1

6
\$\begingroup\$

Vast swaths of this code have no apparent use. Cutting out the code that does not appear to have an effect, you seem to be left with:

public void SerializeInsertCache(object item)
{
        Type type = item.GetType();
        PropertyInfo[] properties = type.GetProperties();

        foreach (var property in properties)
        {
            SerializeInsertCache(property);

        }

       SaveInCache(item);
}

Ben Voigt's point about not repeating reflection might look something like this:

private Dictionary<string,PropertyInfo[]> typeProperties = new Dictionary<string,PropertyInfo[]>();

public void SerializeInsertCache(object item)
{
    Type type = item.GetType();
    string name = type.Name;
    PropertyInfo[] properties;

    if (!typeProperties.TryGetValue(name, out properties))
    {
        properties = type.GetProperties();
        typeProperties.Add(name,properties);
    }

    foreach (var property in properties)
    {
        SerializeInsertCache(property,typeProperties);
    }

   SaveInCache(item);
 }

However, that said, I also think this is overcomplicating the problem. Why are you wanting to serialize these things in the first place? If they're being kept in memory, there is almost certainly a better approach (which we can't suggest until we know more about what problem you're solving).

\$\endgroup\$
0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.