Take the 2-minute tour ×
Code Review Stack Exchange is a question and answer site for peer programmer code reviews. It's 100% free, no registration required.

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);
         }
share|improve this question

migrated from stackoverflow.com Jul 14 at 10:19

This question came from our site for professional and enthusiast programmers.

3  
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. –  Ben Voigt Jul 12 at 0:35
    
propertyName, propertyValue, propertyType, propertyTypeName & cacheStatus are assigned to but never used; get rid of them and you'll get some performance gain. –  JohnLBevan Jul 12 at 0:52
    
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. –  Josh Smeaton Jul 12 at 2:05
    
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? –  bingles Jul 12 at 2:14
    
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. –  Johnathon Sullinger Jul 12 at 2:18

1 Answer 1

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

share|improve this answer
    
I want to save object in cache memory ,in EntityFramework if I have a Test class with a property type List<SomeType> when is save Test class object ,EF to automatic save all of object in List<SomeType>, EF is SQL,My scenario is cache memory , How i do that for cache? –  Amir hossein gholzam Aug 22 at 6:18

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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