Copying 15k string takes 9 secs on an M2

I have a TextArea control and I am storing the StyledText.RTFData into an SQLite database.

The RTFData is 15k - loading it from the database into the TextArea is pretty quick - no noticeable lag at all.

But saving the RTFData back to the database is taking 9 secs - an eternity when you are using an app.

I thought that maybe it was the database slowing it down… but if I take the database out of the equation and simply copy RTFData to a new string, it STILL takes 9 secs to make the copy.

Now, if I read the string from the database - into an actual String - and then copy that String, it is instantaneous.

So what is the deal with getting the RTFData ??? why is it taking so long ? Is there any advice on a different approach that might be quicker ?

Thanks for any advice - TJ

Show us your saving code, pelase, and be sure to select it once pasted into your message and then press </>.

Also: Which OS/version, which version of the IDE.

1 Like

Running MacOS 14.3.1… IDE is 2023 Release 4…

Here is my code saving it to the database…

Dim psql As String = "UPDATE Journals SET Entry = ?, Project = ?, LastUpdated = ?, EditWindow = ? WHERE ID = ?;"
Dim pstmt As SQLitePreparedStatement = Journal_DB.Prepare(psql)

pstmt.BindType(0, SQLitePreparedStatement.SQLITE_TEXT)
pstmt.Bind(0, text_edit_area.StyledText.RTFData)

pstmt.BindType(1, SQLitePreparedStatement.SQLITE_INTEGER)
pstmt.Bind(1, selected_proj_ID)

pstmt.BindType(2, SQLitePreparedStatement.SQLITE_TEXT)
pstmt.Bind(2, update_time)

pstmt.BindType(3, SQLitePreparedStatement.SQLITE_TEXT)
pstmt.Bind(3, Str(Self.Width) + "x" + Str(Self.Height))

pstmt.BindType(4, SQLitePreparedStatement.SQLITE_INTEGER)
pstmt.Bind(4, curr_entry_ID) 'ID of entry

pstmt.SQLExecute

If Journal_DB.Error Then
  MsgBox "Error updating entry text: " + Journal_DB.ErrorMessage
End If
Journal_DB.Commit

From what you describe, it isn’t saving to the database that is taking ages.

It is the actual call to TextArea.StyledText.RTFData

3 Likes

Yes, definitely ! It is the call to RTFData that is taking so long… looking for any suggestions or advice on making that quicker… at least from the end user’s perspective.

I looked into using a Thread to do the save… but that is a no go since RTFData is buried in the UI.

Can you show that code? Maybe something can be done to that.

The above could be condensed to:

Dim psql As String = "UPDATE Journals SET Entry = ?1, Project = ?2, LastUpdated = ?3, EditWindow = ?4 WHERE ID = ?5"

Journal_DB.ExecuteSQL (psql, text_edit_area.StyledText.RTFData, selected_proj_ID, update_time, Self.Width.ToString + "x" + Self.Height.ToString, curr_entry_ID)

although this is unlikely to solve your main issue.

RTFData has a long history of terrible performance in Xojo, and many of us users eventually gave up on it. Here’s one from 9 years ago which appears to have been closed w/o fixing? https://tracker.xojo.com/xojoinc/xojo/-/issues/33587

2 Likes

If you have MBS use
RTFDataMBS
and
WinRTFDataMBS
against the DesktopTextArea to load in a lot faster.

very long time …
does this control maybe a redraw again and again at reading the content ?
does it take the same time if it is invisible?

hmmm… good thought… I will try that and see…

In the mean time, I am creating some functions to turn all of the styleruns into an array of strings… and then writing that to the database… the big question is whether it will be slower.

Maybe internally RTFData() uses thousands low speed functions as largeString.indexOf(currentPoint, token), etc

And slow string concatenation in loops as

Var outString As String
Do Until EndedParsing()
  outString = outString + GetFewParsedChars() // causes large releases + more allocs, thousand times
Loop
// Xojo performs better when you do

Var outArray() As String
Var outString As String

Do Until EndedParsing()
  outArray.Add GetFewParsedChars() // allocs, thousand times
Loop

outString = String.FromArray(outArray, "") // fast alloc
outArray.RemoveAll() // fast release

My (limited) experience with RTFData and TextArea is that the slowness is only an issue under MacOS - it’s fine for me under Windows but unusably slow under MacOS. To me that would seem to point to TextArea rather than RTFData, but I haven’t looked deeply.

Both have a symbiotic relationship. And the problem arises from USING RTFData()

I was thinking that Xojo could be using an internal format and translating it. And as RTF is a Microsoft format invented in the 80’s and supported in the OS, I thought that Xojo was using system APIs to get it working ok in that platform and suffering to make the same for macOS.

But I was observing that in both Windows and macOS, there are “TextView” APIs, and both have the ability of getting RTF data out from the native component.

If not, due to old legacy code, maybe it should? rtf(from:) | Apple Developer Documentation

Please be aware that unless Xojo have improved the quality of their RTF code, it doesn’t handle Unicode characters correctly and corrupts the result.

Can you provide a sample code showing the issue?

Here is some stuff from the forum talking about various Unicode related issues:

15K Text String <> when converted to RTF.

What RTFData do ?

It tooks Styled Text and convert that to RTF. Probably written using REALbasic… Thus its slowliness.

Maybe doing it by yourself (using StyleRun) in both WriteToDB and ReadFromDB will speed up the process?

For less than 100MB Memory should be better suitable.

1 Like

On Windows at least, putting styled text into a TextArea has always been excruciatingly slow. Pulling it back out is fast I would bet it’s the TextArea that has the problem, since it has to convert the RTF to styled text, which is slow even if you put the text in raw and then style it.