More efficient p.makeCacheList() (and saving leo doc as json)

0 views
Skip to first unread message

Ville M. Vainio

unread,
Nov 10, 2009, 4:53:21 AM11/10/09
to leo-editor
Currently p.makeCacheList() is somewhat inefficient, as it goes
through positions unnecessarily. Using vnodes directly yields better
performance, as no intermediate iterators need to be instantiated, or
"self" lookups done. I didn't measure this, but this is "obviously"
faster with equivalent (lower?) complexity.

A better implementation is below (tree_at_vnode). Incidentally, the
script below also shows how to save the leo document in json format,
for use from javascript :-).

QQQ

import json

def tree_at_vnode(v):

'''Create a recursive list describing a tree
for use by v.createOutlineFromCacheList.
'''

return [v.h,v.b,v.gnx, [tree_at_vnode(v2) for v2 in v.children]]

tree = tree_at_vnode(c.hiddenRootNode)
f = open("/tmp/leodoc.json","w")
json.dump(tree, f)

QQQ


--
Ville M. Vainio
http://tinyurl.com/vainio

Edward K. Ream

unread,
Nov 11, 2009, 7:04:55 AM11/11/09
to leo-e...@googlegroups.com
On Tue, Nov 10, 2009 at 3:53 AM, Ville M. Vainio <viva...@gmail.com> wrote:

Currently p.makeCacheList() is somewhat inefficient, as it goes
through positions unnecessarily.

The old code is::

def makeCacheList(self):
    p = self
    return [p.h,p.b,p.gnx, [p2.makeCacheList() for p2 in p.children()]]
 
The proposed new code is::
 
def tree_at_vnode(v):

   return [v.h,v.b,v.gnx, [tree_at_vnode(v2) for v2 in v.children]]

My guess is that for most outlines, the difference would amount to a few milliseconds, at most.

Do have any data to show that this method is a problem?

Edward

Ville M. Vainio

unread,
Nov 11, 2009, 11:54:08 AM11/11/09
to leo-e...@googlegroups.com
On Wed, Nov 11, 2009 at 2:04 PM, Edward K. Ream <edre...@gmail.com> wrote:

>> def tree_at_vnode(v):
>>    return [v.h,v.b,v.gnx, [tree_at_vnode(v2) for v2 in v.children]]
>
> My guess is that for most outlines, the difference would amount to a few
> milliseconds, at most.
>
> Do have any data to show that this method is a problem?

Nope, profiling is most useful when trying to find slow areas of the
code. This kind of "obviously correct" optimization is just something
you casually do here and there when you see inefficient code.

Reply all
Reply to author
Forward
0 new messages