Page 1 of 1

TRichView.LoadRTF takes a lot of time to execute

Posted: Tue Jul 01, 2025 6:55 am
by Lars Boshold
We are facing a situation where calling TRichView.LoadRTF takes a lot of time (several minutes), this does not occur with every rtf-file but only with certain ones, these files are not especially large.
When debugging you can see the two routines TCustomRVData.MassSimpleConcate and TCustomRVData.SimpleConcateSubitems are called a massive lot of times, several orders of magnitude more often than with files which do not show this behaviour. (>1 billion vs. 2-3 digit number of times)

This effect can be reproduced with demo "TRichView\Demos\DelphiUnicode\OfficeConverters" and attached file "Problem_Demo.rtf".

Is the rtf file corrupt in any way or is there a flaw in the RichView-code?

Regards,
Lars

Re: TRichView.LoadRTF takes a lot of time to execute

Posted: Wed Jul 02, 2025 4:05 pm
by Sergey Tkachenko
This is a TRichView bug.
It appears that SimpleConcateSubitems processes each item in table cells twice (when calling MassSimpleConcate, and directly in SimpleConcateSubitems). As a result, the number of calls grows exponentially depending on the table nesting level.
When table nesting level is not high, it's not a problem. But in your document, the maximum count tables nested in each other is 28!

The problem was not found before because this level of nesting is very untypical for Word documents.

Quick fix.
In CRVData.pas, change procedure TCustomRVData.SimpleConcateSubitems to:

Code: Select all

procedure TCustomRVData.SimpleConcateSubitems(ItemNo: Integer);
var
  StoreSub:  TRVStoreSubRVData;
  SubRVData: TCustomRVData;
  item:      TCustomRVItemInfo;
begin
  item := GetItem(ItemNo);
  StoreSub := nil;
  SubRVData := TCustomRVData(item.GetSubRVData(StoreSub, rvdFirst));
  while SubRVData <> nil do
  begin
    SubRVData.MassSimpleConcate(0, SubRVData.ItemCount - 1);
    SubRVData := TCustomRVData(item.GetSubRVData(StoreSub, rvdNext));
  end;
  StoreSub.Free;
end;

Re: TRichView.LoadRTF takes a lot of time to execute

Posted: Thu Jul 03, 2025 5:56 am
by Lars Boshold
Thanks, your fix solved the problem