davidbalbert / Watt

A high performance text editor for macOS.
MIT License
57 stars 2 forks source link

Audit and test COW BTree construction #66

Open davidbalbert opened 1 year ago

davidbalbert commented 1 year ago

There are a bunch of places where I have the following pattern:

struct Foo: BTree
    mutating func hmm() {
        // ...
        var b = BTreeBuilder<Tree>()
        b.push(&root, slicedBy: firstRange)
        b.push(&newTree.root)
        b.push(&root, slicedBy: secondRange)
        self = b.build()
    }
}

I believe there are cases where using &root directly, rather than doing var r = root; b.push(&r, slicedBy: range) might modify root after the first push so that the second push(_:slicedBy:) won't work correctly.

But I'm inconsistent (sometimes I create a second reference, sometimes I don't), and I'm not sure which is correct.

I need to audit BTreeBuilder, as well as all the call sites to push(:slicedBy:) and push(:) to make sure I understand what's going on. I also need to write tests to verify the correct behavior.

This pattern is common enough that it might be beneficial to define a method to do this in an extension to BTree.