views:

455

answers:

2

Hello, I am currently trying to tackle a speed issue involving loading and saving richtext. Here are the details. In my application I have a Question class which needs to be able to store two richtexts. At the moment these two richtexts are being stored in the Question class as TextRanges. As far as saving/loading goes I am using TextRange.Load(Stream, DataFormats) and TextRange.Save(Stream, DataFormats). Everything works...

My problem is that I need to have 1000+ of these questions save/load from a file. When I do this saving/loading a file that contains these questions takes 20~ seconds. And on top of that, I would like to be able to load several of these files at once...which would leave the user waiting for possibly several minutes. I'm sure my problem is clear now.

So I started to investigate why it was taking so long to save/load a file and from a profiler I found that 80% of the time spent saving/loading a file is in the TextRange.Load and TextRange.Save methods.

So my question is...Does anyone have any suggestions or pointers to speeding up my file save/load?

I was trying to think of another way to store the richtext in both the Question class and in the file. I ran out of ideas fairly quickly but wanted to come here before I conceded. Any help is greatly appreciated. Also let me know if I need to clarify anything. Thank you!

A: 

You might look into threading. This way you could load your questions without locking up the UI. Load the first 100 and when they run out, load another 100 or so.

Lucas McCoy
I was playing around with this idea earlier. I wouldn't do exactly this since I need to load the entire file for the user to be able to do anything with the questions; however, I could use the threading so that the user could do stuff with the files that have already loaded.
Jasson
+1  A: 

How much overhead is there in each call to Load and Save, is there any advantage to calling Load once for a big range versus many times for small ranges? How many times do you call Load when you load +1000 Questions? Is each act of calling Load updating a RichTextBox or a Flow Document somewhere? If not what are you doing with the TextRanges once you have them loaded?

I noticed this related thread and thought I would offer a suggestion: What if you save the byte arrays (or memory streams) in the Question class instead of the TextRange and only create the TextRange when you actually need to load your data in the RichTextBox (when the window for the question opens)?

MichaC
Now that you mention it there is a decent amount of overhead in the TextRange.Save and Load methods. I am actually calling Load twice for each question. So for 1000 questions it is called 2000 times. There is a FlowDocument used for a second for the loading of the richtext however that isn't using up much CPU according to the profiler. I allow the user to open these questions and that is when a window opens with RichTextBoxes that these TextRanges fill up.
Jasson
I think I may try looking into combining the TextRanges into one large one for the Save and Load and then parse it. That I'm sure will speed it up at least a some. Thanks for the idea. ^^
Jasson
Brilliant idea! I didn't think it'd be good to have a few thousand streams open nor did I want to mess with making sure they were at the beginning everytime I wanted to read them. So I went with changing the TextRanges to byte[]. I had to change a bunch of stuff around but once I finished I gave it a run and my code ran about 10 times faster lol, but it still took a while to load a file. Using a profiler I was able to speed it up some more. Now it loads a file in about half a second! Thanks! :D
Jasson