TRVStyle.SaveToINI become very very slow for document content pasted from the web

General TRichView support forum. Please post your questions here
Post Reply
edwinyzh
Posts: 104
Joined: Sun Jun 05, 2022 2:22 pm

TRVStyle.SaveToINI become very very slow for document content pasted from the web

Post by edwinyzh »

TRVStyle.SaveToINI become very very slow for document content pasted from the web. It took over 30 seconds to save the styles.

Steps to reproduce the issue.

- Implement TrvHtmlViewImporter for TRichViewEdit, as instructed here.

- Copy and paste web page content from https://stackoverflow.com/questions/270 ... kage-error

- Now save the styles, using TRVStyle.SaveToINI. I found that slowness happens in the follow lines:

Code: Select all

TRVStyle.SaveToINI
begin
  ...
  ParaStyles.SaveToINI(ini, Section);
  TextStyles.SaveToINI(ini, Section);
  ListStyles.SaveToINI(ini, Section);
  StyleTemplates.SaveToINI(ini, Section);
Any advise?
edwinyzh
Posts: 104
Joined: Sun Jun 05, 2022 2:22 pm

Re: TRVStyle.SaveToINI become very very slow for document content pasted from the web

Post by edwinyzh »

More finding 1:
The saved ini file is 310+ kb, with 10k+ lines in it...
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: TRVStyle.SaveToINI become very very slow for document content pasted from the web

Post by Sergey Tkachenko »

1. Use TMemIniFile instead of TIniFile, it is faster.

2. Instead of Ini files, you can use Delphi streaming features.
You can create TFileStream, and use its WriteComponent and ReadComponent methods for storing TRVStyle component.
It is much faster than ini files, and much smaller.

3. The main problem is unused text, paragraph, and list styles
(especially unused list styles that may appear after reading RTF or DocX files)
If TRVStyle is used by a single TRichView, you can use RichView.DeleteUnusedStyles(True, True, True).
If TRVStyle is used in multiple documents, see https://www.trichview.com/help/idh_clas ... sdata.html
edwinyzh
Posts: 104
Joined: Sun Jun 05, 2022 2:22 pm

Re: TRVStyle.SaveToINI become very very slow for document content pasted from the web

Post by edwinyzh »

Sergey Tkachenko wrote: Thu Jun 30, 2022 10:14 am 1. Use TMemIniFile instead of TIniFile, it is faster.
I'm already using TMemIniFile.
Sergey Tkachenko wrote: Thu Jun 30, 2022 10:14 am 2. Instead of Ini files, you can use Delphi streaming features.
You can create TFileStream, and use its WriteComponent and ReadComponent methods for storing TRVStyle component.
It is much faster than ini files, and much smaller.
Is it human-readable? Like that I've chosen xml instead of rvf as the document file format, human-readable is crucial.

3. The main problem is unused text, paragraph, and list styles
(especially unused list styles that may appear after reading RTF or DocX files)
But the html content is quite simple, it's not reasonable to cause the saving that takes over 30 seconds! Something must be wrong...

And during the import process, does it reuse existing styles?
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: TRVStyle.SaveToINI become very very slow for document content pasted from the web

Post by Sergey Tkachenko »

No, it's not human readable.
It can be converted to human-readable form using ObjectBinaryToText/ObjectTextToBinary functions (from Classes unit).
Saving: Save to a memory stream using Stream.WriteComponent, then convert to a file stream using ObjectBinaryToText.
Loading: Convert from file stream to a memory stream using ObjectTextToBinary, then load from a memory stream using ObjectTextToBinary.

I believe the resulting size will be less than INI, but not significatly.
I am not sure about speed, it need to be tested. I believe it will be much faster.

---

Yes, import re-uses existing styles.
What HTML file do you load, and how?
edwinyzh
Posts: 104
Joined: Sun Jun 05, 2022 2:22 pm

Re: TRVStyle.SaveToINI become very very slow for document content pasted from the web

Post by edwinyzh »

Sergey Tkachenko wrote: Thu Jun 30, 2022 2:37 pm I believe the resulting size will be less than INI, but not significatly.
I am not sure about speed, it need to be tested. I believe it will be much faster.

---

Yes, import re-uses existing styles.
What HTML file do you load, and how?
I stated in the question - just copy and paste some content from a stackoverflow.com page.
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: TRVStyle.SaveToINI become very very slow for document content pasted from the web

Post by Sergey Tkachenko »

I tried pasting from StackOverflow question.
As a result, 23 new text styles, 37 paragraph styles, 9 list styles.
DeleteUnusedStyles reduced the count of paragraph styles, that means some of them are not used in document.
Subsequent pastes in the same document increased the number of list styles, but they are returned to 9 after DeleteUnusedStyles.

Conclusions:
1. Count of styles added by HTML importing with THTMLViewer is not too large.
2. Still, unused styles should be deleted.
edwinyzh
Posts: 104
Joined: Sun Jun 05, 2022 2:22 pm

Re: TRVStyle.SaveToINI become very very slow for document content pasted from the web

Post by edwinyzh »

Thanks for the info. After more thoughts, even there are 10k items in INI file, it shouldn't take over 30 seconds to save. There must be something else. I'll debug into it later. I'll keep you updated in this thread.
Sergey Tkachenko
Site Admin
Posts: 17557
Joined: Sat Aug 27, 2005 10:28 am
Contact:

Re: TRVStyle.SaveToINI become very very slow for document content pasted from the web

Post by Sergey Tkachenko »

In addition to INI and ReadComponent/WriteComponent, I can suggest the third way of saving.
Simply save an empty document with RichViewXML, turning on saving styles.
edwinyzh
Posts: 104
Joined: Sun Jun 05, 2022 2:22 pm

Re: TRVStyle.SaveToINI become very very slow for document content pasted from the web

Post by edwinyzh »

Sergey Tkachenko wrote: Fri Jul 01, 2022 7:36 pm In addition to INI and ReadComponent/WriteComponent, I can suggest the third way of saving.
Simply save an empty document with RichViewXML, turning on saving styles.
Good idea, I'll try this too, when I come back to this issue, in case after debugging into source and still cannot find why saving to INI takes over 30 seconds. I'm having other priorities now.
Post Reply