navid/veins / commit
ffdd48d871af
Navid M <navidmomtahen@gmail.com> · 2026-11-25
parents: 04991c7c989c
Browse files at this commit · Download source
--- a/docs/SPECIFICATION_V1.adoc+++ b/docs/SPECIFICATION_V1.adoc@@ -667,7 +667,7 @@ | commits | vein commit ID <-> git commit SHA-1 | trees | vein tree ID <-> git tree SHA-1-| blobs | vein blob ID <-> git blob SHA-1 (cached, because computing it requires hashing the full content)+| blobs | vein blob ID <-> git blob SHA-1. Lazy IDs embed the SHA-1, but the mapping is still recorded for materialization and export. | raw | vein ID -> original git object bytes, for commits and trees that cannot be reconstructed canonically (see below) |=== @@ -685,6 +685,20 @@ . Importing is idempotent. A commit whose SHA-1 is in the map is not converted again. The initial import of a long history pays git's delta resolution cost once, for reading the source. This cost is not paid again on any later clone.++=== Lazy git-backed import (M1)++To keep import time independent of blob count and blob size, the M1 importer uses lazy blob IDs. It converts commits, trees, refs, the view, and the operation log, but it does not read, chunk, hash, or recompress blob bodies. A blob referenced by an imported tree is represented by a 32-byte lazy ID:++ `"vein-git-oid"` (12 bytes) followed by the 20-byte git SHA-1++The body remains in the `.vein/git` mirror. Tree entries keep their normal file, executable, or symlink kind, but the target is a lazy blob ID instead of a canonical vein blob ID.++Reading a lazy blob reads the corresponding git object from the mirror. `vn checkout` uses this path, so an imported repository checks out to the same bytes as git. `vn fsck` treats lazy blob references as promised objects; they are not errors merely because they are absent from `.vein/store`.++Lazy IDs are deterministic for a given git blob. The `blobs` mapping table records them so that a later materialization pass can replace lazy references with canonical chunked vein blobs. Materialization is a separate operation and will rewrite the affected tree, commit, and view IDs. Deleting `.vein/git` while lazy references remain makes those blobs unavailable.++Lazy import is an importer fast path. Repositories created by a native vein clone use canonical chunked blobs and do not depend on a git mirror. === Export (vein to git) --- a/src/src/git/import.sx+++ b/src/src/git/import.sx@@ -14,7 +14,6 @@ val objid = use("../object/id.sx") val types = use("../object/types.sx") val tree = use("../object/tree.sx")-val blob = use("../object/blob.sx") val commit = use("../object/commit.sx") val operation = use("../object/operation.sx") val view = use("../object/view.sx")@@ -24,6 +23,7 @@ val state = use("../store/state.sx") val files = use("../io/files.sx") val object = use("object.sx")+val lazy = use("lazy.sx") val format = use("format.sx") val refs = use("refs.sx") val oidmap = use("oidmap.sx")@@ -62,9 +62,6 @@ new_trees: mut usize new_blobs: mut usize writer: mut ref writer.Writer- time_read: mut i64- time_chunk: mut i64- time_write: mut i64 } fn put_usize(map: mut oidmap.OidMap, oid: bytes.Bytes, value: usize) void = trust {@@ -184,53 +181,11 @@ val existing = get_id(ctx.blobs_map, oid) if existing is some id { return some dup_id(id)- }- val blob_t0: i64 = time.now_ns()- val obj_opt = object.read_fast(ctx.git, oid)- val blob_t1: i64 = time.now_ns()- ctx.time_read = ctx.time_read + (blob_t1 - blob_t0)- val obj: mut object.Object = object.Object{kind: 0, body: bytes.empty(), alloc: bytes.empty()}- match obj_opt {- ok o => {- obj = o- }- err _ => {- return none- }- }- if obj.kind != object.KIND_BLOB {- object.free_object(obj)- return none- }- val built: ref blob.BlobBuild = blob.build(obj.body.ptr, obj.body.len)- val blob_t2: i64 = time.now_ns()- ctx.time_chunk = ctx.time_chunk + (blob_t2 - blob_t1)- val root: objid.Id = dup_id(blob.root_id(built))- for ro in built.objects {- if ro.id != none {- val rid: objid.Id = objid.id(ro.id)- val _ = writer.add_known(ctx.writer, ro.type_byte, rid, ro.body)- val rip: mut ref void = ro.id as ref void- @free(rip)- } else {- val rid2: objid.Id = writer.add(ctx.writer, ro.type_byte, ro.body)- val rp2: mut ref void = rid2.ptr as ref void- @free(rp2)- }- val rp: mut ref void = ro as ref void- @free(rp) }- @listfree(built.objects)- val blob_t3: i64 = time.now_ns()- ctx.time_write = ctx.time_write + (blob_t3 - blob_t2)- put_id(ctx.blobs_map, oid, root)+ val id: objid.Id = lazy.blob_id(oid)+ put_id(ctx.blobs_map, oid, id) ctx.new_blobs++- val out: objid.Id = dup_id(root)- val rp3: mut ref void = root.ptr as ref void- @free(rp3)- @free(built as ref void)- object.free_object(obj)- return some out+ return some dup_id(id) } fn import_tree(ctx: mut ImportCtx, oid: bytes.Bytes) option[objid.Id] = trust {@@ -651,7 +606,6 @@ } val refset: refs.RefSet = refs.read(git.git_dir)- val timing_start: i64 = time.now_ns() val writer_opt = writer.open(repo) val wref: mut ref writer.Writer = none match writer_opt {@@ -677,10 +631,7 @@ new_commits: 0, new_trees: 0, new_blobs: 0,- writer: wref,- time_read: 0,- time_chunk: 0,- time_write: 0+ writer: wref } val branches: mut list[ref ImportNamedTarget] = lists.new[ref ImportNamedTarget]()@@ -777,8 +728,6 @@ } @listfree(stack) oidmap.free(seen)- val timing_collect: i64 = time.now_ns()- stdio.eprint("timing: collect {d} ms\n", {(timing_collect - timing_start) / 1000000}) val n: usize = @len(records) val indegree: ref u32 = @alloc(n * 4 + 1) as ref u32@@ -888,8 +837,6 @@ return 1 } - val timing_topo: i64 = time.now_ns()- stdio.eprint("timing: topo {d} ms\n", {(timing_topo - timing_collect) / 1000000}) val build_failed: mut bool = false // Import trees and blobs in topological order so git delta bases are read // before the objects that depend on them.@@ -914,8 +861,6 @@ } ci++ }- val timing_trees: i64 = time.now_ns()- stdio.eprint("timing: trees {d} ms\n", {(timing_trees - timing_topo) / 1000000}) val oi: mut usize = 0 for oi < @len(order) {@@ -998,8 +943,6 @@ } oi++ }- val timing_commits: i64 = time.now_ns()- stdio.eprint("timing: commits {d} ms\n", {(timing_commits - timing_trees) / 1000000}) @listfree(order) for cl4 in children { @listfree(cl4)@@ -1180,10 +1123,7 @@ @free(up) } - stdio.eprint("timing: blob read {d} ms, chunk {d} ms, write {d} ms\n", {ctx.time_read / 1000000, ctx.time_chunk / 1000000, ctx.time_write / 1000000})- val timing_before_finish: i64 = time.now_ns() val wrote: bool = writer.finish(ctx.writer)- stdio.eprint("timing: state {d} ms, finish {d} ms\n", {(timing_before_finish - timing_commits) / 1000000, (time.now_ns() - timing_before_finish) / 1000000}) if !wrote { stdio.eprint("vn: failed to write object pack\n", {}) if old_encoded.ptr != none {--- /dev/null+++ b/src/src/git/lazy.sx@@ -0,0 +1,53 @@+/// Git-backed blob identifiers used by lazy imports.+///+/// A lazy blob ID is the 12-byte prefix "vein-git-oid" followed by the+/// 20-byte git object ID. Blob bodies stay in the `.vein/git` mirror until+/// they are materialized.++val bytes = use("../core/bytes.sx")+val objid = use("../object/id.sx")++pub val PREFIX_LEN: usize = 12+pub val GIT_OID_LEN: usize = 20+pub val ID_LEN: usize = 32++fn prefix() bytes.Bytes = {+ return bytes.bytes(c"vein-git-oid", 12)+}++/// Build the lazy vein ID for a git blob.+pub fn blob_id(oid: bytes.Bytes) objid.Id = trust {+ pre {+ oid_len: oid.len == GIT_OID_LEN+ }+ val out: ref u8 = @alloc(ID_LEN) as ref u8+ @memcpy(out as ref void, prefix().ptr as ref void, PREFIX_LEN)+ @memcpy(+ @ptradd(out, PREFIX_LEN as i64) as ref void,+ oid.ptr as ref void,+ GIT_OID_LEN+ )+ return objid.id(out)+}++/// True when `id` names a git-backed blob rather than a stored vein blob.+pub fn is_lazy(id: objid.Id) bool = trust {+ if id.ptr == none {+ return false+ }+ return bytes.Bytes.eql(+ bytes.bytes(id.ptr, PREFIX_LEN),+ prefix()+ )+}++/// The git object ID carried by a lazy blob ID. The result is borrowed.+pub fn git_oid(id: objid.Id) bytes.Bytes = trust {+ pre {+ present: id.ptr != none+ }+ return bytes.bytes(+ @ptradd(id.ptr, PREFIX_LEN as i64),+ GIT_OID_LEN+ )+}--- a/src/src/git/object.sx+++ b/src/src/git/object.sx@@ -85,8 +85,8 @@ } val CACHE_MIN: usize = 0-val CACHE_MAX_BYTES: usize = 8589934592-val CACHE_MAX_ENTRIES: usize = 131072+val CACHE_MAX_BYTES: usize = 536870912+val CACHE_MAX_ENTRIES: usize = 32768 val CACHE_MAP_CAP: usize = 262144 pub type GitRepo = {--- a/src/src/store/fsck.sx+++ b/src/src/store/fsck.sx@@ -19,6 +19,7 @@ val store = use("store.sx") val layout = use("layout.sx") val files = use("../io/files.sx")+val lazy = use("../git/lazy.sx") pub type Ctx = { repo: layout.Repo@@ -106,6 +107,9 @@ if objid.Id.is_none(id) { report(ctx, path, "absent reference where a value is required") return false+ }+ if expected == types.BLOB && lazy.is_lazy(id) {+ return true } val loc: mut store.Loc = store.Loc{pack_index: 0, record_index: 0, kind: 0} val found: bool = store.find_handles(handles, id, loc)--- a/src/src/store/writer.sx+++ b/src/src/store/writer.sx@@ -5,7 +5,6 @@ /// the file, and the index is written from the collected record metadata. val str = use("std/string")-val stdio = use("std/stdio") val time = use("std/time") val allocators = use("std/allocators") val thread = use("std/thread")@@ -47,7 +46,7 @@ offset: mut u64 metas: mut list[ref records.RecordMeta] items: mut list[ref PendingRecord]- hasher: mut ref void+ pending_bytes: mut usize failed: mut bool seen: mut idmap.IdSet }@@ -217,9 +216,7 @@ w.offset = 16 w.metas = lists.new[ref records.RecordMeta]() w.items = lists.new[ref PendingRecord]()- w.hasher = @alloc(b3.hasher_state_size())- b3.hasher_init(w.hasher)- b3.hasher_update(w.hasher, buf.Buf.view(header).ptr, buf.Buf.view(header).len)+ w.pending_bytes = 0 buf.Buf.free(header) w.failed = false w.seen = idmap.new(1024)@@ -251,6 +248,12 @@ r.stored = bytes.empty() r.comp = 0 @append(w.items, r)+ w.pending_bytes = w.pending_bytes + body.len+ if w.pending_bytes >= 268435456 {+ if !write_pending(w) {+ w.failed = true+ }+ } return id } @@ -264,10 +267,6 @@ if w.fd >= 0 { native.close_fd(w.fd) w.fd = -1- }- if w.hasher != none {- @free(w.hasher as ref void)- w.hasher = none } free_meta_list(w.metas) if @len(w.items) > 0 {@@ -300,15 +299,11 @@ free_writer(w) } -/// Finish the pack and write its index. `w` is consumed.-pub fn finish(w: mut ref Writer) bool = trust {- if w.failed {- abort(w)- return false+fn write_pending(w: mut ref Writer) bool = trust {+ if @len(w.items) == 0 {+ return true }- val tf0: i64 = time.now_ns() compress_items(w.items)- stdio.eprint("writer: compress {d} ms\n", {(time.now_ns() - tf0) / 1000000}) val i: mut usize = 0 val out: mut buf.Buf = buf.new(8388608) val header: mut buf.Buf = buf.new(64)@@ -328,7 +323,6 @@ w.count++ w.offset = start + header.len as u64 + r.stored.len as u64 if out.len >= 8388608 {- b3.hasher_update(w.hasher, buf.Buf.view(out).ptr, out.len) if !native.write_fd_all(w.fd, buf.Buf.view(out)) { w.failed = true }@@ -338,7 +332,6 @@ i++ } if out.len > 0 {- b3.hasher_update(w.hasher, buf.Buf.view(out).ptr, out.len) if !native.write_fd_all(w.fd, buf.Buf.view(out)) { w.failed = true }@@ -346,31 +339,63 @@ } buf.Buf.free(out) buf.Buf.free(header)- stdio.eprint("writer: write {d} ms\n", {(time.now_ns() - tf0) / 1000000})+ if w.failed {+ return false+ }+ free_items(w.items)+ w.items = lists.new[ref PendingRecord]()+ w.pending_bytes = 0+ return true+}++/// Finish the pack and write its index. `w` is consumed.+pub fn finish(w: mut ref Writer) bool = trust { if w.failed { abort(w) return false }- if @len(w.items) > 0 {- free_items(w.items)- } else {- @listfree(w.items)+ if !write_pending(w) {+ abort(w)+ return false }- w.items = lists.new[ref PendingRecord]() - val tf1: i64 = time.now_ns() val count_buf: mut buf.Buf = buf.new(8) buf.Buf.put_u64_le(count_buf, w.count as u64) val patched: bool = native.pwrite_fd_all(w.fd, 8, buf.Buf.view(count_buf)) buf.Buf.free(count_buf) if !patched {+ abort(w)+ return false+ }++ if !native.seek_set(w.fd, 0) { abort(w) return false+ }+ val state: ref void = @alloc(b3.hasher_state_size())+ b3.hasher_init(state)+ val chunk: ref u8 = @alloc(1048576) as ref u8+ val hashed: mut bool = true+ for {+ val n: i64 = native.read_fd(w.fd, chunk, 1048576)+ if n < 0 {+ hashed = false+ break+ }+ if n == 0 {+ break+ }+ b3.hasher_update(state, chunk, n as usize) } val digest: ref u8 = @alloc(32) as ref u8- b3.hasher_final(w.hasher, digest)- stdio.eprint("writer: hash {d} ms\n", {(time.now_ns() - tf1) / 1000000})- val tf2: i64 = time.now_ns()+ b3.hasher_final(state, digest)+ @free(state)+ @free(chunk as ref void)+ if !hashed {+ @free(digest as ref void)+ abort(w)+ return false+ } val trailer: bytes.Bytes = bytes.bytes(digest, 32) val wrote_trailer: bool = native.pwrite_fd_all(w.fd, w.offset as i64, trailer) val synced: bool = native.fsync_fd(w.fd)@@ -416,7 +441,6 @@ w.packs_dir, index_bytes )- stdio.eprint("writer: index {d} ms\n", {(time.now_ns() - tf2) / 1000000}) val idx_buf: mut ref void = index_bytes.ptr as ref void @free(idx_buf) files.free_path(idx_tmp_name)--- a/src/src/worktree/checkout.sx+++ b/src/src/worktree/checkout.sx@@ -18,6 +18,8 @@ val state = use("../store/state.sx") val files = use("../io/files.sx") val native = use("../io/native.sx")+val object = use("../git/object.sx")+val lazy = use("../git/lazy.sx") extern (C) fn openat(dirfd: i32, path: ref u8, flags: i32, mode: i32) i32 = "openat" extern (C) fn mkdirat(dirfd: i32, path: ref u8, mode: i32) i32 = "mkdirat"@@ -123,6 +125,9 @@ step: mut usize failed: mut bool error: mut bytes.Bytes+ git: mut object.GitRepo+ git_lock: mut ref void+ has_git: mut bool } type CheckoutTarget = {@@ -281,6 +286,32 @@ } val np: mut ref void = t.name as ref void @free(np)+}++fn read_git_blob(+ git: object.GitRepo,+ lock: ref void,+ has_git: bool,+ blob_id: objid.Id+) option[bytes.Bytes] = trust {+ if !has_git || lock == none {+ return none+ }+ thread.Mutex.lock(lock)+ val read = object.read_fast(git, lazy.git_oid(blob_id))+ val out: mut option[bytes.Bytes] = none+ match read {+ ok o => {+ if o.kind == object.KIND_BLOB {+ out = some bytes.Bytes.dup(o.body)+ }+ object.free_object(o)+ }+ err _ => {+ }+ }+ thread.Mutex.unlock(lock)+ return out } fn write_blob_data(@@ -287,10 +318,26 @@ handles: list[ref store.Handle], fd: i32, blob_id: objid.Id,- depth: usize+ depth: usize,+ git: object.GitRepo,+ git_lock: ref void,+ has_git: bool ) bool = trust { if depth > 64 { return false+ }+ if lazy.is_lazy(blob_id) {+ val git_body = read_git_blob(git, git_lock, has_git, blob_id)+ match git_body {+ some b => {+ val wrote: bool = native.write_fd_all(fd, b)+ files.free_buffer(b)+ return wrote+ }+ none => {+ return false+ }+ } } val body_opt = state.read_body(handles, blob_id, types.BLOB) val body: mut bytes.Bytes = bytes.empty()@@ -336,7 +383,7 @@ } } else { for e in b.entries {- if !write_blob_data(handles, fd, e.id, depth + 1) {+ if !write_blob_data(handles, fd, e.id, depth + 1, git, git_lock, has_git) { success = false break }@@ -350,8 +397,31 @@ fn write_symlink( handles: list[ref store.Handle], t: CheckoutTarget,- blob_id: objid.Id+ blob_id: objid.Id,+ git: object.GitRepo,+ git_lock: ref void,+ has_git: bool ) bool = trust {+ if lazy.is_lazy(blob_id) {+ val git_body = read_git_blob(git, git_lock, has_git, blob_id)+ match git_body {+ some b => {+ val target: ref u8 = @alloc(b.len + 1) as ref u8+ if b.len > 0 {+ @memcpy(target as ref void, b.ptr as ref void, b.len)+ }+ @store8(@ptradd(target, b.len as i64), 0)+ val _ = unlinkat(t.fd, t.name, 0)+ val created: bool = symlinkat(target, t.fd, t.name) == 0+ @free(target as ref void)+ files.free_buffer(b)+ return created+ }+ none => {+ return false+ }+ }+ } val body_opt = state.read_body(handles, blob_id, types.BLOB) val body: mut bytes.Bytes = bytes.empty() match body_opt {@@ -395,7 +465,7 @@ some t => { val success: mut bool = false if task.kind == tree.KIND_SYMLINK {- success = write_symlink(job.handles, t, task.target)+ success = write_symlink(job.handles, t, task.target, job.git, job.git_lock, job.has_git) } else { val flags: i32 = o_wronly() | o_creat() | o_trunc() | o_nofollow() | o_cloexec() val mode: u32 = FILE_MODE@@ -406,7 +476,7 @@ } val fd: i32 = mut_fd if fd >= 0 {- success = write_blob_data(job.handles, fd, task.target, 0)+ success = write_blob_data(job.handles, fd, task.target, 0, job.git, job.git_lock, job.has_git) if success { val want: mut u32 = FILE_MODE if task.kind == tree.KIND_EXECUTABLE {@@ -706,6 +776,30 @@ } val ac: allocators.HeapAllocator = @zeroed+ val git_path: str.StringView = layout.git_dir(repo)+ val git_opt = object.open(git_path)+ val git_repo: mut object.GitRepo = @zeroed+ val git_open: mut bool = false+ match git_opt {+ ok g => {+ git_repo = g+ git_open = true+ }+ err _ => {+ files.free_path(git_path)+ }+ }+ val git_lock_opt = thread.Mutex.new[allocators.HeapAllocator](ac)+ val git_lock: mut ref void = none+ val git_usable: mut bool = git_open+ match git_lock_opt {+ some m => {+ git_lock = m+ }+ none => {+ git_usable = false+ }+ } val count: usize = cpu_count() val n: usize = @len(tasks) val workers: mut usize = count@@ -730,6 +824,9 @@ job.step = workers job.failed = false job.error = bytes.empty()+ job.git = git_repo+ job.git_lock = git_lock+ job.has_git = git_usable @append(jobs, job) val tr = thread.Thread.new[allocators.HeapAllocator](ac, checkout_worker, job as ref void) match tr {@@ -770,6 +867,12 @@ j++ } + if git_lock != none {+ thread.Mutex.free[allocators.HeapAllocator](ac, git_lock)+ }+ if git_open {+ object.free(git_repo)+ } close(root_fd) if had_failure { if failed_path.len > 0 {