A head-slapping moment: the so-called high-level interfaced throughout 
Leo's core to interact with Leo's text panes lacks a way of getting the 
length of text *without* making a copy of the text.

This is more than a slight oversight: I imagine there are several place 
were the code does s = w.getAllText() merely so it can use len(s)! This is 
a huge strain on the GC for large p.b.

Worse, the absence of w.getTextLength() has lead to the most unfortunate 
LeoQTextEditWidget.lengthHelper, which calculated the length of the text 
using QTextCursor methods!  This is catastrophically slow for large text.  
This method was doubling node load times for large nodes.

In other words, w.getTextLength() is an essential part of fixing git bug 28:
https://github.com/leo-editor/leo-editor/issues/28

But nooooooo... w.getTextLength() is far from easy to do.  There is no 
QTextEdit or QTextDocument method that delivers the length of the text!  
This is a *huge* hole in the QTextEdit and QTextDocument api's, but there 
is nothing we can do about that.

The simplest thing that could possibly work would be::

    def getTextLength(self):
        w = self.widget
        return len(w.getAllText())

But this would be a step backwards because it hides the fact that it is 
expensive.

It might be possible to cache the value of w.getAllText in an "text 
changed" event handler in LeoQTextBrowser:(QTextBrowser).  But I would like 
to avoid this approach--any bug would lead to data loss or corruption. I 
may play around with this, but for now it's not likely to happen.  See 
below for a workaround.

By analogy with w.getTextLength(), we can imagine a p.b_length method that 
would simply return len(p.v._bodyString).  This, at least, is worth doing.

===== Summary

w.getTextLength() isn't trivial.  (Sounds of gnashing teeth.)

Caching the value returned by w.getAllText() seems too dangerous.

As a work-around, the crucial node-selection logic will attempt to minimize 
calls to w.getAllText.  There, and *only* there, it should be possible to 
create a *single* copy of the text and pass that copy around to all code 
that needs it.  It would be a complication, but a completely safe one.

Otoh, p/v.b_length are trivial and useful.  They will be added asap.

Edward

-- 
You received this message because you are subscribed to the Google Groups 
"leo-editor" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
To post to this group, send email to [email protected].
Visit this group at http://groups.google.com/group/leo-editor.
For more options, visit https://groups.google.com/d/optout.

Reply via email to