Victor Stinner wrote:
====================== 8< ========================================
def _get_width( o ):
    global widths
    if o == 0xe or o == 0xf:
        return 0
    for num, wid in widths:
        if o <= num:
            return wid
    return 1
def get_width( o ):
    """Return the screen column width for unicode ordinal o."""
    global _get_width_cache
    if o not in _get_width_cache:
        _get_width_cache[o] = _get_width(o)
    return _get_width_cache[o]
====================== 8< ========================================

In my program, the cache has 71 entries. That's small ;-)

That's madness! :-) Instead of storing the entire string we could store either a hash of the string or use weakrefs to the strings. But I suspect that none of these solutions will be faster than just calculating the width in C.

Also, this patch might help for your situation:
https://excess.org/urwid/changeset/48

It assumes length==width when all the characters in the string are "simple" (as defined by a regular expression)

For calc_width():
 * avoid it? (use it only when it's needed)

some avoidance is done in https://excess.org/urwid/changeset/48

 * use cache? (sound complex)
 * only accept unicode

Manipulate UTF-8 is complex. In general, it would be a better idea to only store Unicode string, and convert to UTF-8 when text is written in the terminal.

Yes, that is the recommended approach when writing Python programs. I chose to be more accepting, since I'm writing a library for other programmers to use.

In Urwid I accept either locale-dependent strings (to support EUC-*, GBK* etc.) or unicode strings. I don't want to make a special case that says "this program must only use unicode strings if the detected encoding is UTF-8".

But I just proposed "micro-optimization". To really improve urwid, we have to implement:
  http://excess.org/urwid/ticket/1
  Partial screen updates

Indeed. This one will have the biggest impact, but it will also hide some bits of code that are really slow. I'll have to include an option to turn it off for profiling Urwid.

And also:
  http://excess.org/urwid/ticket/2
  C code for performance critical sections

I believe that Rebecca Breu has done some work on this. I hope she finds some time to post her work, I'd like to try it out.

Ian


_______________________________________________
Urwid mailing list
[email protected]
http://lists.excess.org/mailman/listinfo/urwid

Reply via email to