I would be surprised if the serialization used by the AppFabric cache used nothing but a binary writer from WCF, so if that is the case (it looks like this), the difference is due to the processing of arrays in binary format. Arrays for certain primitive types have a special type node , which allows them to be effectively stored in binary format. int (and byte) are some of these types. Guid is not (I donβt know that the team decided to decide what type of array it would be or not). Therefore, when serializing an array of integers in binary format is very efficient, serializing an array of Guid is not.
As suggested by m0sa, you can convert it to byte [], and you are likely to see a significant improvement. I found out that LINQ syntax, although much cleaner, does not give you the big performance improvements you can get with a more traditional loop. I executed the code below to compare serialization speeds (which I think will be displayed in the AF cache) and serialization as a byte [] (even with conversion between Guid [] to byte []) is even faster than one of int [].
public class StackOverflow_6346646 { static void SerializeGuid() { Console.WriteLine("Serializing Guid[]"); var guids = new Guid[20000]; Random rndGen = new Random(); for (int i = 0; i < guids.Length; i++) { guids[i] = Guid.NewGuid(); } MemoryStream ms = new MemoryStream(); Stopwatch watch = new Stopwatch(); DataContractSerializer dcs = new DataContractSerializer(guids.GetType()); XmlDictionaryWriter binaryWriter = XmlDictionaryWriter.CreateBinaryWriter(ms); watch.Start(); dcs.WriteObject(binaryWriter, guids); binaryWriter.Flush(); watch.Stop(); Console.WriteLine("Serialized in {0}ms, total size = {1} bytes", watch.ElapsedMilliseconds, ms.Position); } static void SerializeInt() { Console.WriteLine("Serializing int[]"); var guids = new int[80000];
source share