ObjectIDGenerator implementation

I am trying to use ObjectIDGenerator in C # to generate a unique identifier during serialization, however this class is not available in XBox360 or Windows Phone 7.NET frameworks (they use the compact version of .NET). I implemented the version using the Object to Int64 dictionary and was able to get a fully functional version, but the performance is unsatisfactory. I am serializing about tens of thousands of objects, and this is currently the biggest hurdle to save / load. Using the actual .NET implementation on the PC, it takes about 0.3 seconds to serialize about 20,000 objects. Using my implementation, it takes about 6 seconds.

In profiling, I found that the heavy hitters were .TryGetValue and .Add in the dictionary (which makes sense because it indexes and adds a hash map). More importantly, the virtual equality operator was called instead of simply comparing the links, so I implemented IEqualityComparer, which only used ReferenceEquals (this led to an increase in speed).

Does anyone have an understanding of a better implementation of ObjectIDGenerator? Thank you for your help!

My implementation: http://pastebin.com/H1skZwNK

[Change] Another note: profiling results indicate that comparing objects / ReferenceEquals is still a bottleneck, the number of hits is 43,000,000. I wonder if there is a way to store data along this object without having to look for it in the hash map .. .

+1
source share
1 answer

Is it possible to use the Int32 Id / handle property for each object, and not Object ? This can help. It seems that in any case, you assign an identifier type number to each object, only then you look based on the Object link instead of Id. Can you save the object identifier (your Int64 ) inside each object and instead make your dictionary in Dictionary<Int64, Object> ?

You can also see if SortedDictionary<TKey, TValue> or SortedList<TKey, TValue> better or worse. But if your main bottleneck is in your IEqualityComparer , this may not help much.

UPDATE

ObjectIDGenerator looking at the API of the ObjectIDGenerator class, I can understand why you cannot do what I suggested first; you create identifiers!

ObjectIDGenerator seems to manually implement its own hash table (it selects object[] and parallel long[] and resizes them as objects are added). It also uses RuntimeHelpers.GetHashCode(Object) to calculate its hash, not IEqualityComparer , which can be a big incentive for your punch, as it always calls Object.GetHashCode() and does not make a virtual call to the derived type (or an interface call in your case with IEqualityComparer ).

You can see the source yourself through the Microsoft Shared Source Initiative :

+4
source

Source: https://habr.com/ru/post/1439829/


All Articles