Yeah, one of the last releases has that.
Btw, and since I’m no reflection guru, I’m curious: what’s the most expensive part? Get the attributes or getting the value from the prop info?
---
Luis Abreu
Ok, ok…one of the latest commits.
That didn’t answer my question: is getting the value through relfection less expensive than getting the property info? If it is, then caching that info would be an option, right? (but only after that is confirmed)…
Simone, about this caching thing…
I’ve tried doing something like that with value objects where I’d cache the list of properties by type on a dictionary. I had a friend that run some tests and he told me that there really wasn’t much improvement with the caching of the properties. That’s why I asked you in a previous mail what was the most expensive part (getting the Propertyinfo or getting the value of the propertyinfo…)
---
Luis Abreu
From:
sharp-arc...@googlegroups.com
[mailto:sharp-arc...@googlegroups.com] On
Behalf Of Simone Busoli
Sent: quarta-feira, 7 de Janeiro
de 2009 22:25
To:
sharp-arc...@googlegroups.com
Subject: Re: Performance of
GetCustomAttributes (Reflection)
Actually, caching them in a dictionary static field could be a performance improvement. As they are now, they are cached for each instance, but different instances of the same type won't share the knowledge.
Ok.
I too don’t have the time to write such test now so I’ll see if my friend still has that demo code that he has written…
Actualy no locks are need it in this scenario. If you call SignatureProperies from multiple threads in exactly the same time what will happen is that each thread will execute this line:
GetType().GetProperties().Where(p => Attribute.IsDefined(p, typeof(DomainSignatureAttribute), true)).ToList();
and will put result in the dictionary under the same key overriding the previous value, because we know for sure what this result would be identical in each execution it not a problem. It's just a slight performance hit for few very first concurrent calls to this property. If you still think that it better to optimize for this few calls I can recommend following implementation:
private static readonly Object padlock = new Object();
public override IEnumerable<PropertyInfo> SignatureProperties {
get {
IEnumerable<PropertyInfo> properties;
if (signaturePropertiesDictionary.TryGetValue(GetType(), out properties))
return properties;
properties=GetType().GetProperties()
.Where(p => Attribute.IsDefined(p, typeof(DomainSignatureAttribute), true))
.ToList();
lock (padlock)
{
signaturePropertiesDictionary[GetType()] = properties;
}
return properties;
}
}
Hello again.
Am I wrong when I look at the code and say that you’re just getting the list of properties?
Anything wrong with the double lock strategy?
---
Luis Abreu
From:
sharp-arc...@googlegroups.com
[mailto:sharp-arc...@googlegroups.com] On
Behalf Of Michael Tsibelman
Sent: sexta-feira, 9 de Janeiro de
2009 06:53
To:
sharp-arc...@googlegroups.com
Subject: Re: Performance of
GetCustomAttributes (Reflection)
Actualy no locks are need it in this scenario. If you call SignatureProperies from multiple threads in exactly the same time what will happen is that each thread will execute this line:
I forgot that GetType() is not a static method :)
I know that. What I was trying to say was that the most expensive part of getting the value through reflection was getting the value from a propertyINfo object and not the act of getting the propertyinfo instance in the first place….
Is is that expensive to use a lock instead of using the threadstatic attribute? Just asking…
From: sharp-arc...@googlegroups.com
[mailto:sharp-arc...@googlegroups.com] On Behalf Of Simone Busoli
Sent: sábado, 10 de Janeiro de 2009 13:41
To: sharp-arc...@googlegroups.com
Subject: Re: Performance of GetCustomAttributes (Reflection)
Sure. ThreadStatic (1) is thread safe, as is storing the IEnumerable<PropertyInfo> (2) in an instance field. So why is 1 better than 2?
Seems good to me.
From: sharp-arc...@googlegroups.com
[mailto:sharp-arc...@googlegroups.com] On Behalf Of Simone Busoli
Sent: domingo, 11 de Janeiro de 2009 01:26
To: sharp-arc...@googlegroups.com
Subject: Re: Performance of GetCustomAttributes (Reflection)
Since no one is coming up with a clever idea, I'll throw mine:
Well, what I’m saying is that we’re going to so much trouble in optimizing the code in getting the props when the most expensive operation might be getting the values. That’s all I’m saying…