ENB: Positions vs VNodes

31 views
Skip to first unread message

Edward K. Ream

unread,
Aug 9, 2023, 9:49:51 AM8/9/23
to leo-editor

This Engineering Notebook post discusses when to use Positions and when to use VNodes. The general rules:


1. Prefer VNodes to Positions if possible, but

2. Most outline operations must use Positions.


Examples


Outline commands such as insert-node, delete-node, and paste-node must use Positions because c.p affects what parent VNodes Leo must update.


When writing a .leo file, Leo must scan all nodes to find dirty @<file> nodes. Using c.all_nodes, an abbreviation for c.hiddenRootNode.self_and_subtree, will be slightly more efficient than c.all_unique_positions. But not enough to matter.


Otoh, v.setAllAncestorAtFileNodesDirty is the perfect place to use v.self_and_all_parents. There is no need to scan the entire outline!


Finally, Leo's outline-drawing code must use Positions because VNodes may appear in multiple places in the outline.


And that's it.


Edward

Edward K. Ream

unread,
Aug 10, 2023, 2:14:05 PM8/10/23
to leo-editor
On Wednesday, August 9, 2023 at 8:49:51 AM UTC-5 Edward K. Ream wrote:


> When writing a .leo file, Leo must scan all nodes to find dirty @<file> nodes.

> Using c.all_unique_nodes will be slightly more efficient than c.all_unique_positions. But not enough to matter.


The timeit script below shows that c.all_unique_nodes is about three times faster than c.all_unique_positions.  On my machine the output is something like this:

positions  0.012876
vnodes     0.004999

Imo this 3x speedup is no big deal. It's a gain of less than 0.01 seconds on a large outline like LeoPyRef.leo. Note that the time is linear on the number of positions in the outline.

Edward

P.S. Here is the timeit script:

import timeit

def positions():
    for p in c.all_unique_positions():
        if 0:
            print(p.v)

def vnodes():
    for v in c.all_unique_nodes():
        if 0:
            print(v)

for f in ('positions', 'vnodes'):
    s = timeit.timeit(f"{f}()", number=1, globals=globals())
    print(f"{f:10} {s:6.6}")

Edward

Edward K. Ream

unread,
Aug 10, 2023, 2:17:11 PM8/10/23
to leo-editor
On Thursday, August 10, 2023 at 1:14:05 PM UTC-5 Edward K. Ream wrote:

The timeit script below shows that c.all_unique_nodes is about three times faster than c.all_unique_positions.  On my machine the output is something like this:

positions  0.012876
vnodes     0.004999

Note: I ran this test in the ekr-3472-flat-json branch. The test probably won't work in devel because of missing methods.

Edward
Reply all
Reply to author
Forward
0 new messages