... or When "So Long" Means Goodbye.
During the last couple of weeks I've been struggling with VSTO and my code, trying to find an answer to the long duration it was taking for Word to load with my addin, which wasn't such slow and clumsy.
After testing my smart document from almost every possible angle, a colleague of mine suggested to check the automatically generated code that VSTO adds to every new smart document you create.
So I did. This automatically generated code lies within the ThisDocument.designer.cs file, which you'll be able to see only after clicking the "Show All Files" button in Visual Studio.
In this file, there is the Initialize method which does all the background initializing for you. I'd put timers there and found out that the reason for the long loading time was the InitializeCachedData method.
Yupe, it's the deserializtion of the document's cached items that was taking so long.
WATCH OUT FROM SERIALIZATION!!!
This is pretty much the message I'd like you to absorb... In my smart document I have about 10 cached items, including a list of longs, a list of strings and a list of typed datasets. The InitializeCachedData took on my machine 5-6 seconds and on some other machines it took up to 30 seconds! What I did eventually, was changing all the list cached items to strings and managing the serialization and deserialization of my cached items by myself. This reduces the execution time of InitializeCachedData in about 66%!
In conclusion,
- Variables you don't need to cache, don't cache.
- Keep your cached items as small in size as you can.
- If possible, make all your cached items the same type and if possible, make it a value type.
Remember, "so long" means goodbye. And in terms of your product, this is bad, very bad.
Take care,
Shay.