snarkVM icon indicating copy to clipboard operation
snarkVM copied to clipboard

feat: introduce atomic batch writes

Open howardwu opened this issue 3 years ago • 3 comments

Motivation

feat: introduce atomic batch writes

howardwu avatar Aug 15 '22 05:08 howardwu

@ljedrz Can we add a unit test for MemoryMap with the atomic op?

Also clippy may have some lint requests.

howardwu avatar Aug 15 '22 05:08 howardwu

I have one request and two questions.

Request: can we add an abort_atomic method as well?

Question 1: what happens if a method return Err(..) in between start_atomic and finish_atomic? Is it possible to rollback/abandon the unsaved changes when leaving the scope? I presume we may need an object with some drop logic to perform this.

Question 2: is it possible to continue 1 atomic scope from one method into another? For example:

fn foo() {
    start_atomic();
    bar();
    end_atomic();
}
fn bar() {
    // do stuff atomically still
}

There is a legitimate use case for this if it is possible.

howardwu avatar Aug 15 '22 05:08 howardwu

Rebased, fixed the related clippy lint, added Map::abort_atomic and 2 unit tests.

Question 1: what happens if a method return Err(..) in between start_atomic and finish_atomic? Is it possible to rollback/abandon the unsaved changes when leaving the scope? I presume we may need an object with some drop logic to perform this.

It could be done via Drop, but a new object would be required, and it could accidentally be misused (e.g. when bound to _ or when done in some extra helper scope). The best way to handle this would IMO be to have a wrapper function/method, so that the following could be performed:

impl Ledger {
    (...)

    fn some_method(...) {
        self.start_atomic();

        if let Err(e) = self.some_method_apply_storage_changes(...) {
            self.abort_atomic();
            return Err(e);
        }

        self.finish_atomic();
    }
}

Question 2: is it possible to continue 1 atomic scope from one method into another? For example:

Yes; right now Map::start_atomic persists until Map::{abort_atomic, finish_atomic} is called, regardless of what else happens in the meantime.

ljedrz avatar Aug 15 '22 08:08 ljedrz