[Urwid] Optimize urwid

Victor Stinner victor.stinner at haypocalc.com
Fri Dec 29 06:46:50 EST 2006


Hi,

I run a profiler on hachoir-urwid [1] and top 10 is:

 ncalls  tottime  percall  cumtime  percall filename:lineno(function)
  10989    9.742    0.001   18.329    0.002 util.py:563(calc_width)
1406531    8.568    0.000    8.568    0.000 utable.py:78(get_width)
     69    7.676    0.111    7.676    0.111 curses_display.py:247(_getch)
     61    0.450    0.007    0.827    0.014 curses_display.py:484(draw_screen)
   2553    0.328    0.000   18.757    0.007 canvas.py:37(__init__)
    300    0.267    0.001    1.532    0.005 listbox.py:110(calculate_visible)
   2309    0.203    0.000    2.080    0.001 canvas.py:354(apply_text_layout)
  11454    0.190    0.000    0.666    0.000 urwid_ui.py:252(_get)
  24025    0.182    0.000    0.182    0.000 util.py:871(rle_len)
  10996    0.174    0.000    0.379    0.000 widget.py:237(rows)

I'm using a UTF-8 terminal and calc_width() takes 10% of the time!

I read calc_width() and get_width() functions and they aren't optimized.

For get_width(), it's hard to optimized the algorithm. So I propose to use a 
cache:
====================== 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 ;-)

For calc_width():
 * avoid it? (use it only when it's needed)
 * 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.

-------

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

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

-------

References:
[1] hachoir-urwid: http://hachoir.org/wiki/hachoir-urwid

Victor Stinner aka haypo
http://hachoir.org/




More information about the Urwid mailing list