navid/veins / commit

b7434ae81b96

Navid M <navidmomtahen@gmail.com> · 2026-11-26

parents: e651e381ea29

Browse files at this commit · Download source

all: commit performance fixes
--- a/veins/src/cli/new_cmd.sx+++ b/veins/src/cli/new_cmd.sx@@ -102,13 +102,18 @@     return out } -fn describe_impl(repo: layout.Repo, args: list[bytes.Bytes], require_changes: bool) i32 = trust {-    val snap: i32 = snapshot.run(repo)+fn describe_impl(+    repo: layout.Repo,+    args: list[bytes.Bytes],+    require_changes: bool,+    st: mut store.Store+) i32 = trust {+    val snap: i32 = snapshot.run_with(repo, st)     if snap != 0 {         return snap     }     val message: bytes.Bytes = join_args(args)-    val tx_opt = txn.begin(repo)+    val tx_opt = txn.begin_with(repo, st)     val tx: mut ref txn.Txn = none     match tx_opt {         some t => {@@ -137,12 +142,11 @@         @free(mp2)         return 1     }-    val st: store.Store = store.open_all(repo)-    val lc_opt = graph.load_commit(st.handles, wc)+    val handles: list[ref store.Handle] = txn.handles(tx)+    val lc_opt = graph.load_commit(handles, wc)     if lc_opt is none {         msg.error_prefix()         stdio.eprint("cannot read working-copy commit\n", {})-        store.close(st)         txn.abort(tx)         val mp2: mut ref void = message.ptr as ref void         @free(mp2)@@ -152,7 +156,7 @@     if lc_opt is some x {         lc = x     }-    val changed: bool = graph.has_changes(st.handles, lc)+    val changed: bool = graph.has_changes(handles, lc)     val same_message: bool = bytes.Bytes.eql(message, lc.c.message)     if !changed && (require_changes || same_message) {         if require_changes {@@ -159,7 +163,6 @@             msg.info_prefix()             stdio.print("nothing to commit, working copy clean\n", {})         }-        store.close(st)         txn.abort(tx)         graph.free_loaded(lc)         val mp: mut ref void = message.ptr as ref void@@ -201,9 +204,9 @@     viewops.rebuild_heads(tx.view)     val status: i32 = txn.finish(tx, bytes.bytes(c"describe", 8))     if status == 0 {+        store.refresh(st, repo)         val _ = gitmap.mark_native(repo, new_id)     }-    store.close(st)     graph.free_loaded(lc)     @listfree(parents)     free_renames(renames)@@ -223,29 +226,43 @@ }  pub fn describe(repo: layout.Repo, args: list[bytes.Bytes]) i32 = trust {-    return describe_impl(repo, args, false)+    val st: mut store.Store = store.open_all(repo)+    val status: i32 = describe_impl(repo, args, false, st)+    store.close(st)+    return status }  pub fn commit_changes(repo: layout.Repo, args: list[bytes.Bytes]) i32 = trust {-    val d: i32 = describe_impl(repo, args, true)+    val st: mut store.Store = store.open_all(repo)+    val d: i32 = describe_impl(repo, args, true, st)     if d == NOTHING_TO_COMMIT {+        store.close(st)         return 0     }     if d != 0 {+        store.close(st)         return d     }     val empty: list[bytes.Bytes] = lists.new[bytes.Bytes]()-    val status: i32 = new_change(repo, empty)+    val status: i32 = new_change_impl(repo, empty, false, st)+    store.close(st)     @listfree(empty)     return status } -pub fn new_change(repo: layout.Repo, revs: list[bytes.Bytes]) i32 = trust {-    val snap: i32 = snapshot.run(repo)-    if snap != 0 {-        return snap+fn new_change_impl(+    repo: layout.Repo,+    revs: list[bytes.Bytes],+    do_snapshot: bool,+    st: mut store.Store+) i32 = trust {+    if do_snapshot {+        val snap: i32 = snapshot.run_with(repo, st)+        if snap != 0 {+            return snap+        }     }-    val tx_opt = txn.begin(repo)+    val tx_opt = txn.begin_with(repo, st)     val tx: mut ref txn.Txn = none     match tx_opt {         some t => {@@ -257,7 +274,7 @@             return 1         }     }-    val st: store.Store = store.open_all(repo)+    val handles: list[ref store.Handle] = txn.handles(tx)     val wc_opt = viewops.working_copy(tx.view)     val wc: mut objid.Id = objid.id_none()     val have_wc: mut bool = false@@ -267,7 +284,7 @@     }     val old_root: mut option[objid.Id] = none     if have_wc {-        val old_lc_opt = graph.load_commit(st.handles, wc)+        val old_lc_opt = graph.load_commit(handles, wc)         if old_lc_opt is some old_lc {             old_root = some(copy_id(old_lc.c.root_tree))             graph.free_loaded(old_lc)@@ -279,11 +296,10 @@         if !have_wc {             msg.error_prefix()             stdio.eprint("no working-copy commit\n", {})-            store.close(st)             txn.abort(tx)             return 1         }-        val lc_opt = graph.load_commit(st.handles, wc)+        val lc_opt = graph.load_commit(handles, wc)         match lc_opt {             some lc => {                 @append(parents, copy_id(wc))@@ -293,7 +309,6 @@             none => {                 msg.error_prefix()                 stdio.eprint("cannot read working-copy commit\n", {})-                store.close(st)                 txn.abort(tx)                 return 1             }@@ -307,12 +322,12 @@             if r is some v {                 rev = v             }-            val resolved = graph.resolve(st.handles, tx.view, rev)+            val resolved = graph.resolve(handles, tx.view, rev)             match resolved {                 some id => {                     @append(parents, copy_id(id))                     if i == 0 {-                        val lc_opt = graph.load_commit(st.handles, id)+                        val lc_opt = graph.load_commit(handles, id)                         match lc_opt {                             some lc => {                                 root_tree = copy_id(lc.c.root_tree)@@ -333,7 +348,6 @@         if mut_failed {             msg.error_prefix()             stdio.eprint("unknown revision\n", {})-            store.close(st)             txn.abort(tx)             graph.free_ids(parents)             return 1@@ -364,6 +378,7 @@     viewops.rebuild_heads(tx.view)     val status: mut i32 = txn.finish(tx, bytes.bytes(c"new", 3))     if status == 0 {+        store.refresh(st, repo)         val _ = gitmap.mark_native(repo, new_id)         if @len(revs) > 0 {             // The new commit was created from the requested revision, but the@@ -371,7 +386,6 @@             status = checkout.materialize_commit(repo, st.handles, old_root, root_tree)         }     }-    store.close(st)     @listfree(parents)     @listfree(renames)     @listfree(extra)@@ -391,5 +405,12 @@         }     }     identity.free_sig(sig)+    return status+}++pub fn new_change(repo: layout.Repo, revs: list[bytes.Bytes]) i32 = trust {+    val st: mut store.Store = store.open_all(repo)+    val status: i32 = new_change_impl(repo, revs, true, st)+    store.close(st)     return status }--- a/veins/src/store/store.sx+++ b/veins/src/store/store.sx@@ -11,11 +11,13 @@ val layout = use("layout.sx") val files  = use("../io/files.sx") val native = use("../io/native.sx")+val stat   = use("../io/stat.sx")  pub type Handle = {     path: mut str.StringView     pack: mut ref pack.Pack     idx: mut ref index.Index+    mtime_ns: mut u64 }  pub type Store = {@@ -42,6 +44,7 @@     h.path = path     h.pack = p     h.idx = idx+    h.mtime_ns = 0     @append(handles, h) } @@ -51,13 +54,96 @@  pub fn close_handles(handles: list[ref Handle]) void = {     for h in handles {-        pack.free_pack(h.pack)-        index.free_index(h.idx)+        if h.pack != none {+            pack.free_pack(h.pack)+        }+        if h.idx != none {+            index.free_index(h.idx)+        }         files.free_path(h.path)         val p: mut ref void = h as ref void         trust @free(p)     }     @listfree(handles)+}++/// Load a handle's index on first use, and its pack only when an object body is+/// actually needed. Most commands touch a handful of recent objects, so eagerly+/// decoding every pack makes each invocation cost time proportional to the+/// whole store.+pub fn ensure_index(h: mut ref Handle) bool = trust {+    if h.idx != none {+        return true+    }+    if h.path.len <= 6 {+        return false+    }+    val base_view: str.StringView = str.view(h.path.ptr, h.path.len - 6)+    val iview: str.StringView = files.with_suffix(base_view, str.from_cstr(c".vidx"))+    val idata_opt = files.read_all(iview)+    files.free_path(iview)+    val idata: mut bytes.Bytes = bytes.empty()+    match idata_opt {+        some d => {+            idata = d+        }+        none => {+            return false+        }+    }+    val decoded = index.decode(idata, bytes.empty())+    match decoded {+        ok idx => {+            val base: str.StringView = files.basename(h.path)+            if base.len <= 6 || !pack_name_matches_trailer(+                str.view(base.ptr, base.len - 6),+                idx.pack_trailer+            ) {+                index.free_index(idx)+                return false+            }+            h.idx = idx+            return true+        }+        err _ => {+            files.free_buffer(idata)+            return false+        }+    }+}++pub fn ensure_pack(h: mut ref Handle) bool = trust {+    if h.pack != none {+        return true+    }+    if !ensure_index(h) {+        return false+    }+    val data_opt = files.read_all(h.path)+    val data: mut bytes.Bytes = bytes.empty()+    match data_opt {+        some d => {+            data = d+        }+        none => {+            return false+        }+    }+    val decoded = pack.decode(data)+    match decoded {+        ok p => {+            if !bytes.Bytes.eql(p.trailer, h.idx.pack_trailer) {+                pack.free_pack(p)+                return false+            }+            h.pack = p+            return true+        }+        err _ => {+            files.free_buffer(data)+            return false+        }+    } }  pub fn close(store: Store) void = {@@ -80,13 +166,21 @@     handles: list[ref Handle],     id: objid.Id,     out: mut Loc-) bool = {+) bool = trust {     val i: mut usize = 0     for i < @len(handles) {-        val h = handle_list_at(handles, i)+        val h: mut ref Handle = handle_list_at(handles, i)+        if !ensure_index(h) {+            i+++            continue+        }         val found = index.Index.find(h.idx, id)         if found is some pos {             val off: u64 = index.Index.offset_at(h.idx, pos)+            if !ensure_pack(h) {+                i+++                continue+            }             val rec = pack.Pack.find_by_offset(h.pack, off)             if rec is some r {                 val pidx: usize = i@@ -104,7 +198,7 @@ }  /// Compatibility wrapper for callers that prefer an option.-pub fn lookup_handles(handles: list[ref Handle], id: objid.Id) option[Loc] = {+pub fn lookup_handles(handles: list[ref Handle], id: objid.Id) option[Loc] = trust {     val out: mut Loc = Loc{pack_index: 0, record_index: 0, kind: 0}     if find_handles(handles, id, out) {         return some out@@ -113,11 +207,11 @@ }  /// Find an object across all loaded packs.-pub fn lookup(store: Store, id: objid.Id) option[Loc] = {+pub fn lookup(store: Store, id: objid.Id) option[Loc] = trust {     return lookup_handles(store.handles, id) } -pub fn handle_list_at(handles: list[ref Handle], i: usize) ref Handle = {+pub fn handle_list_at(handles: list[ref Handle], i: usize) mut ref Handle = {     val o = @get(handles, i)     if o is some h {         return h@@ -129,118 +223,157 @@ pub fn get_handles(     handles: list[ref Handle],     loc: Loc-) result[bytes.Bytes, derr.DecodeError] = {+) result[bytes.Bytes, derr.DecodeError] = trust {     pre {         pack_in_bounds: loc.pack_index < @len(handles)     }-    val h = handle_list_at(handles, loc.pack_index)+    val h: mut ref Handle = handle_list_at(handles, loc.pack_index)+    if !ensure_pack(h) {+        return err derr.fail(derr.ErrorKind.Corrupt, 0)+    }     return pack.get(h.pack, loc.record_index) } -pub fn get(store: Store, loc: Loc) result[bytes.Bytes, derr.DecodeError] = {+pub fn get(store: Store, loc: Loc) result[bytes.Bytes, derr.DecodeError] = trust {     return get_handles(store.handles, loc) } -fn load_pair(-    packs_dir: str.StringView,-    name: str.StringView-) option[Handle] = {+fn pack_name_matches_trailer(name: str.StringView, trailer: bytes.Bytes) bool = trust {+    if name.len != 64 || trailer.len != 32 {+        return false+    }+    val hex: ref u8 = @alloc(65) as ref u8+    objid.hex_encode(trailer.ptr, 32, hex)+    @store8(@ptradd(hex, 64), 0)+    val same: bool = bytes.Bytes.eql(bytes.bytes(name.ptr, 64), bytes.from_cstr(hex))+    @free(hex as ref void)+    return same+}++fn lazy_handle(packs_dir: str.StringView, name: str.StringView) mut ref Handle = trust {     val pview: str.StringView = files.join(packs_dir, name)-    val pdata_opt = files.read_all(pview)-    if pdata_opt is none {-        files.free_path(pview)-        return none+    val h: mut ref Handle = @alloc(@sizeof(Handle)) as ref Handle+    h.path = pview+    h.pack = none+    h.idx = none+    h.mtime_ns = 0+    val info = stat.read(pview)+    if info is some si {+        h.mtime_ns = si.mtime_ns     }-    val pdata: mut bytes.Bytes = bytes.empty()-    match pdata_opt {-        some d => {-            pdata = d-        }-        none => {-            files.free_path(pview)-            return none-        }+    return h+}++fn handle_ref_at(xs: list[ref Handle], i: usize) ref Handle = trust {+    val o = @get(xs, i)+    if o is some h {+        return h     }-    val p = trust pack.decode(pdata)-    val pp: mut ref pack.Pack = none-    match p {-        ok x => {-            pp = x+    assert false+    return none+}++fn merge_sort_handles(+    xs: mut list[ref Handle],+    lo: usize,+    hi: usize,+    tmp: mut list[ref Handle]+) void = trust {+    if hi - lo <= 1 {+        return+    }+    val mid: usize = lo + (hi - lo) / 2+    merge_sort_handles(xs, lo, mid, tmp)+    merge_sort_handles(xs, mid, hi, tmp)+    val i: mut usize = lo+    val j: mut usize = mid+    val k: mut usize = lo+    for k < hi {+        val take_left: mut bool = false+        if i >= mid {+            take_left = false+        } elif j >= hi {+            take_left = true+        } else {+            take_left = handle_ref_at(xs, i).mtime_ns >= handle_ref_at(xs, j).mtime_ns         }-        err _ => {-            files.free_buffer(pdata)-            files.free_path(pview)-            return none+        if take_left {+            @set(tmp, k, handle_ref_at(xs, i))+            i+++        } else {+            @set(tmp, k, handle_ref_at(xs, j))+            j++         }-    }-    if name.len <= 6 {-        pack.free_pack(pp)-        files.free_path(pview)-        return none+        k++     }-    val base: str.StringView = str.view(name.ptr, name.len - 6)-    val iname: str.StringView = files.with_suffix(base, ".vidx")-    val iview: str.StringView = files.join(packs_dir, iname)-    files.free_path(iname)-    val idata_opt = files.read_all(iview)-    if idata_opt is none {-        pack.free_pack(pp)-        files.free_path(pview)-        files.free_path(iview)-        return none+    val m: mut usize = lo+    for m < hi {+        @set(xs, m, handle_ref_at(tmp, m))+        m++     }-    val idata: mut bytes.Bytes = bytes.empty()-    match idata_opt {-        some d => {-            idata = d-        }-        none => {-            pack.free_pack(pp)-            files.free_path(pview)-            files.free_path(iview)-            return none-        }+}++/// Order packs newest first so lookups usually touch only the most recent one.+fn sort_handles(xs: mut list[ref Handle]) void = trust {+    val n: usize = @len(xs)+    if n <= 1 {+        return     }-    val idxr = index.decode(idata, pp.trailer)-    val idx: mut ref index.Index = none-    match idxr {-        ok x => {-            idx = x-        }-        err _ => {-            files.free_buffer(idata)-            pack.free_pack(pp)-            files.free_path(pview)-            files.free_path(iview)-            return none-        }+    val tmp: mut list[ref Handle] = lists.new[ref Handle]()+    val i: mut usize = 0+    for i < n {+        @append(tmp, none)+        i++     }-    files.free_path(iview)-    val h: mut ref Handle = @alloc(@sizeof(Handle)) as ref Handle-    h.path = pview-    h.pack = pp-    h.idx = idx-    return some h+    merge_sort_handles(xs, 0, n, tmp)+    @listfree(tmp) }  /// Load every valid pack/index pair. Invalid pairs are skipped; `fsck` reports /// them in detail.-pub fn open_all(repo: layout.Repo) Store = {+pub fn open_all(repo: layout.Repo) Store = trust {     val store: mut Store = new_store()     val dir: str.StringView = layout.packs_dir(repo)     val names: list[ref u8] = files.list_dir(dir)     for n in names {         val nv: str.StringView = str.from_cstr(n)-        if files.name_ends_with(n, c".vpack") {-            val h = load_pair(dir, nv)-            if h is some handle {-                @append(store.handles, handle)-            }+        if files.name_ends_with(n, c".vpack") && nv.len > 6 {+            @append(store.handles, lazy_handle(dir, nv))         }     }     files.free_names(names)     files.free_path(dir)+    sort_handles(store.handles)     return store+}++fn has_name(handles: list[ref Handle], name: ref u8) bool = trust {+    for h in handles {+        if files.name_ends_with(h.path.ptr, name) {+            return true+        }+    }+    return false+}++/// Add any packs written since the store was opened. Commands that perform+/// several transactions can keep one store instead of rescanning from scratch.+pub fn refresh(store: mut Store, repo: layout.Repo) void = trust {+    val dir: str.StringView = layout.packs_dir(repo)+    if !files.exists_dir(dir) {+        files.free_path(dir)+        return+    }+    val names: list[ref u8] = files.list_dir(dir)+    for n in names {+        val nv: str.StringView = str.from_cstr(n)+        if files.name_ends_with(n, c".vpack") && nv.len > 6 && !has_name(store.handles, n) {+            @append(store.handles, lazy_handle(dir, nv))+        }+    }+    files.free_names(names)+    files.free_path(dir)+    sort_handles(store.handles) }  /// Write one pack and its index durably.--- a/veins/src/store/txn.sx+++ b/veins/src/store/txn.sx@@ -21,6 +21,8 @@     repo: mut layout.Repo     writer: mut ref writer.Writer     view: mut ref view.View+    store: mut store.Store+    owns_store: mut bool     parents: mut list[objid.Id]     start_ms: mut i64 }@@ -290,8 +292,18 @@ }  pub fn begin(repo: layout.Repo) option[ref Txn] = trust {-    val heads: list[objid.Id] = state.heads(repo)     val st: store.Store = store.open_all(repo)+    return begin_owned(repo, st, true)+}++/// Start a transaction on an already open store. The caller keeps ownership of+/// the store and must close it after the transaction finishes.+pub fn begin_with(repo: layout.Repo, st: store.Store) option[ref Txn] = trust {+    return begin_owned(repo, st, false)+}++fn begin_owned(repo: layout.Repo, st: store.Store, owns: bool) option[ref Txn] = trust {+    val heads: list[objid.Id] = state.heads(repo)     val v: mut ref view.View = none     if @len(heads) == 0 {         v = state.empty_view()@@ -307,7 +319,9 @@                 v = x             }             none => {-                store.close(st)+                if owns {+                    store.close(st)+                }                 free_ids(heads)                 return none             }@@ -325,7 +339,9 @@                 base_view = x             }             none => {-                store.close(st)+                if owns {+                    store.close(st)+                }                 free_ids(heads)                 return none             }@@ -347,7 +363,6 @@         add_unique(base_view.heads, first_id)         v = base_view     }-    store.close(st)     sort_bookmarks(v.bookmarks)     sort_tags(v.tags)     sort_workspaces(v.working_copies)@@ -358,6 +373,9 @@             w = x         }         none => {+            if owns {+                store.close(st)+            }             view.free_view(v)             free_ids(heads)             return none@@ -367,9 +385,17 @@     tx.repo = repo     tx.writer = w     tx.view = v+    tx.store = st+    tx.owns_store = owns     tx.parents = heads     tx.start_ms = time.unix_now_millis()     return some tx+}++/// Handles for the store snapshot the transaction loaded. Valid until the+/// transaction is finished or aborted.+pub fn handles(tx: ref Txn) list[ref store.Handle] = {+    return tx.store.handles }  pub fn add_object(tx: mut ref Txn, kind: u8, body: bytes.Bytes) objid.Id = trust {@@ -415,6 +441,9 @@ fn free_txn(tx: mut ref Txn) void = trust {     if tx.view != none {         view.free_view(tx.view)+    }+    if tx.owns_store {+        store.close(tx.store)     }     free_ids(tx.parents)     @free(tx as ref void)--- a/veins/src/worktree/snapshot.sx+++ b/veins/src/worktree/snapshot.sx@@ -830,7 +830,16 @@  /// Snapshot the working tree into `@`. Returns 0 on success. pub fn run(repo: layout.Repo) i32 = trust {-    val tx_opt = txn.begin(repo)+    val st: mut store.Store = store.open_all(repo)+    val status: i32 = run_with(repo, st)+    store.close(st)+    return status+}++/// Snapshot using an existing store. The store is refreshed with any pack the+/// snapshot wrote and remains owned by the caller.+pub fn run_with(repo: layout.Repo, st: mut store.Store) i32 = trust {+    val tx_opt = txn.begin_with(repo, st)     val tx: mut ref txn.Txn = none     match tx_opt {         some t => {@@ -843,9 +852,8 @@         }     } -    val st: store.Store = store.open_all(repo)-    val wc_opt = viewops.working_copy(tx.view)-    val wc: mut objid.Id = objid.id_none()+    val handles: list[ref store.Handle] = txn.handles(tx)+    val wc_opt = viewops.working_copy(tx.view)    val wc: mut objid.Id = objid.id_none()     val have_wc: mut bool = false     if wc_opt is some id {         wc = id@@ -856,7 +864,7 @@     val c: mut ref commit.Commit = none     val old_tree: mut option[objid.Id] = none     if have_wc {-        val body_opt = state.read_body(st.handles, wc, types.COMMIT)+        val body_opt = state.read_body(handles, wc, types.COMMIT)         match body_opt {             some b => {                 cbody = b@@ -869,7 +877,6 @@                     err _ => {                         msg.error_prefix()                         stdio.eprint("corrupt working-copy commit\n", {})-                        store.close(st)                         txn.abort(tx)                         if wc.ptr != none {                             val wp: mut ref void = wc.ptr as ref void@@ -882,7 +889,6 @@             none => {                 msg.error_prefix()                 stdio.eprint("missing working-copy commit\n", {})-                store.close(st)                 txn.abort(tx)                 if wc.ptr != none {                     val wp2: mut ref void = wc.ptr as ref void@@ -902,7 +908,7 @@     val built = build_dir(         tx,         root,-        st.handles,+        handles,         bytes.empty(),         old_tree,         rules,@@ -931,7 +937,6 @@         build_failed = true     }     if build_failed {-        store.close(st)         txn.abort(tx)         if c != none {             commit.free_commit(c)@@ -966,7 +971,6 @@     }      if unchanged {-        store.close(st)         txn.abort(tx)         if c != none {             commit.free_commit(c)@@ -1035,7 +1039,7 @@         if !objid.Id.eql(old_for_rename, new_root) {             val gitfake: mut object.GitRepo = @zeroed             val detected = renamemod.detect_tx(-                st.handles,+                handles,                 @addr(gitfake),                 false,                 some old_for_rename,@@ -1092,9 +1096,9 @@      val status: i32 = txn.finish(tx, bytes.bytes(c"snapshot", 8))     if status == 0 {+        store.refresh(st, repo)         val _ = gitmap.mark_native(repo, new_commit)     }-    store.close(st)     if c != none {         commit.free_commit(c)     }