开发者

How severe, if at all, the performance penalty for using the AsReference qualifier?

I have to decide whether I want to shave off extr开发者_开发技巧a 5K out of 550K total by qualifying a property with AsReference. After all, 5K is only a fraction of the total - less than 1%. Still, if the performance penalty is miniscule - why not?

Thanks.

Clarification

Using AsReference really reduces the size if there are actually shared references. My question is about the performance or bluntly put - the speed.


It will depend on the model obviously, and serialization and deserialization will be different here. For moderately sized models the performance overhead will be minimal, except of course it will typically have less actual serialization to do (assuming there is a reasonable amount of repeated object instances marked AsReference; if there are none at all then the overhead, though minmimal, is wasted). And if the reference means we avoid re-serializing a large branch of data (maybe a sub-collection etc) then we can get some very nice savings for both CPU and bandwidth.

Any cost here is felt purely by serialization, since the problematic part is checking whether we have seen the object before. During deserialization it is just pluckign items from a list by index, so very fast.

Also note that I'm assuming DynamicType is disabled here, as that is a separate concern (again, impact is minimsed).

Re storage; currently a flat list is maintained, and checked for referential equality. I would like to have used a hashtable/dictionary lookup, but I have concerns about types that override GetHashCode()/Equals, and sadly it is not possible to access the original object.GetHashCode() instance method. This means that for very large numbers of members marked AsReference (and here I mean many many thousands of objects in the graph) it may slowly degrade (the lookup will be O(N) for a growing list of length N). Changing this to a hash lookup would make it O(1) at each lookup.

Thinking aloud, we could possibly do something when we can prove that the type doesn't override (although this involves more reflection, which is itself a pain), or we could just trust the user not to make a mess of GetHashCode() etc - and use their definition of equality to mean equality in the graph. I'm open to thoughts here, but currently referential equality is used as the safest and simplest option.

For actual numbers: it depends a lot on your model and size; since you have a handy model and know the size with/without AsReference, you are presumably in a good position to wrap that in a Stopwatch or similar (preferably to something like MemoryStream so you aren't including disk/IO costs in the timing).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜