navid/veins / commit
389b4ed583e5
Navid M <navidmomtahen@gmail.com> · 2026-11-26
parents: ffdd48d871af
Browse files at this commit · Download source
--- /dev/null+++ b/src/src/cli/bench_cmd.sx@@ -0,0 +1,244 @@+/// Benchmark-only lazy import probe using the git CLI backend.++val str = use("std/string")+val ostr = use("std/string/owned")+val stdio = use("std/stdio")+val os = use("std/os")+val time = use("std/time")+val bytes = use("../core/bytes.sx")+val buf = use("../core/buf.sx")+val idmap = use("../store/idmap.sx")+val files = use("../io/files.sx")++fn find_byte(data: bytes.Bytes, start: usize, target: u8) usize = trust {+ val i: mut usize = start+ for i < data.len && bytes.Bytes.at(data, i) != target {+ i+++ }+ return i+}++fn parse_size(data: bytes.Bytes, start: usize, end: usize) usize = {+ val n: mut usize = 0+ val i: mut usize = start+ for i < end {+ val c: u8 = bytes.Bytes.at(data, i)+ if c < 48 || c > 57 {+ break+ }+ n = n * 10 + (c - 48) as usize+ i+++ }+ return n+}++fn exec_ok(cmd: ref u8) option[bytes.Bytes] = trust {+ val r = os.exec(cmd)+ match r {+ ok s => {+ val n: i64 = ostr.len(s)+ val p: ref u8 = ostr.as_cstr(s)+ val data: bytes.Bytes = bytes.Bytes.dup(bytes.bytes(p, n as usize))+ ostr.free(s)+ return some data+ }+ err _ => {+ return none+ }+ }+}++pub fn run(args: list[ref u8], start: usize) i32 = trust {+ if start >= @len(args) {+ stdio.eprint("vn: bench-import requires a git directory\n", {})+ return 1+ }+ val repo_arg = @get(args, start)+ val repo: mut str.StringView = str.from_cstr(c".")+ if repo_arg is some r {+ repo = str.from_cstr(r)+ }+ val repo_view: str.StringView = repo+ val t0: i64 = time.now_ns()++ val list_cmd: ref u8 = @fmtalloc("git --git-dir={s} rev-list --objects --all", {repo_view})+ val listing_opt = exec_ok(list_cmd)+ @free(list_cmd as ref void)+ val listing: mut bytes.Bytes = bytes.empty()+ match listing_opt {+ some d => {+ listing = d+ }+ none => {+ stdio.eprint("vn: cannot run git rev-list\n", {})+ return 1+ }+ }++ val all_buf: mut buf.Buf = buf.new(1048576)+ val pos: mut usize = 0+ for pos < listing.len {+ val eol: usize = find_byte(listing, pos, 10)+ val end: usize = find_byte(listing, pos, 32)+ val stop: mut usize = end+ if stop > eol {+ stop = eol+ }+ if stop > pos {+ buf.Buf.put_raw(all_buf, @ptradd(listing.ptr, pos as i64), stop - pos)+ buf.Buf.put_u8(all_buf, 10)+ }+ pos = eol + 1+ }+ files.free_buffer(listing)++ val all_path: ref u8 = @fmtalloc("/tmp/vein-bench-all-{d}.oids", {time.now_ns()})+ val all_view: str.StringView = str.from_cstr(all_path)+ val all_written: bool = files.write_all(all_view, buf.Buf.view(all_buf))+ buf.Buf.free(all_buf)+ if !all_written {+ @free(all_path as ref void)+ stdio.eprint("vn: cannot write temporary object list\n", {})+ return 1+ }++ val check_cmd: ref u8 = @fmtalloc(+ "git --git-dir={s} cat-file --batch-check < {s}",+ {repo_view, all_view}+ )+ val check_opt = exec_ok(check_cmd)+ @free(check_cmd as ref void)+ val check: mut bytes.Bytes = bytes.empty()+ match check_opt {+ some d => {+ check = d+ }+ none => {+ files.delete(all_view)+ @free(all_path as ref void)+ stdio.eprint("vn: cannot run git cat-file --batch-check\n", {})+ return 1+ }+ }++ val meta_buf: mut buf.Buf = buf.new(65536)+ val cp: mut usize = 0+ for cp < check.len {+ val eol2: usize = find_byte(check, cp, 10)+ val sp1: usize = find_byte(check, cp, 32)+ if sp1 < eol2 {+ val sp2: usize = find_byte(check, sp1 + 1, 32)+ if sp2 < eol2 {+ val kind: bytes.Bytes = bytes.Bytes.slice(check, sp1 + 1, sp2)+ if bytes.Bytes.eql(kind, bytes.bytes(c"commit", 6))+ || bytes.Bytes.eql(kind, bytes.bytes(c"tree", 4)) {+ buf.Buf.put_raw(+ meta_buf,+ @ptradd(check.ptr, cp as i64),+ sp1 - cp+ )+ buf.Buf.put_u8(meta_buf, 10)+ }+ }+ }+ cp = eol2 + 1+ }+ files.free_buffer(check)++ val meta_path: ref u8 = @fmtalloc("/tmp/vein-bench-meta-{d}.oids", {time.now_ns()})+ val meta_view: str.StringView = str.from_cstr(meta_path)+ val meta_written: bool = files.write_all(meta_view, buf.Buf.view(meta_buf))+ buf.Buf.free(meta_buf)+ if !meta_written {+ files.delete(all_view)+ @free(all_path as ref void)+ @free(meta_path as ref void)+ stdio.eprint("vn: cannot write temporary commit/tree list\n", {})+ return 1+ }++ val batch_cmd: ref u8 = @fmtalloc(+ "git --git-dir={s} cat-file --batch < {s}",+ {repo_view, meta_view}+ )+ val batch_opt = exec_ok(batch_cmd)+ @free(batch_cmd as ref void)+ val data: mut bytes.Bytes = bytes.empty()+ match batch_opt {+ some d => {+ data = d+ }+ none => {+ files.delete(all_view)+ files.delete(meta_view)+ @free(all_path as ref void)+ @free(meta_path as ref void)+ stdio.eprint("vn: cannot run git cat-file --batch\n", {})+ return 1+ }+ }++ val prefix: bytes.Bytes = bytes.bytes(c"vein-git-oid", 12)+ val key: ref u8 = @alloc(32) as ref u8+ @memcpy(key as ref void, prefix.ptr as ref void, 12)+ val lazy: mut idmap.IdSet = idmap.new(16384)+ val commits: mut usize = 0+ val trees: mut usize = 0+ val entries: mut usize = 0+ val lazy_count: mut usize = 0++ val p: mut usize = 0+ for p < data.len {+ val nl: usize = find_byte(data, p, 10)+ val sp1: usize = find_byte(data, p, 32)+ val sp2: usize = find_byte(data, sp1 + 1, 32)+ val size: usize = parse_size(data, sp2 + 1, nl)+ val body_start: usize = nl + 1+ val body_end: usize = body_start + size+ val kind: bytes.Bytes = bytes.Bytes.slice(data, sp1 + 1, sp2)+ if bytes.Bytes.eql(kind, bytes.bytes(c"commit", 6)) {+ commits+++ } elif bytes.Bytes.eql(kind, bytes.bytes(c"tree", 4)) {+ trees+++ val ep: mut usize = body_start+ for ep < body_end {+ val space: usize = find_byte(data, ep, 32)+ val nul: usize = find_byte(data, space + 1, 0)+ val mode: bytes.Bytes = bytes.Bytes.slice(data, ep, space)+ val oid_start: usize = nul + 1+ if oid_start + 20 > body_end {+ break+ }+ val is_blob: bool = bytes.Bytes.starts_with(mode, bytes.bytes(c"100", 3))+ || bytes.Bytes.eql(mode, bytes.bytes(c"120000", 6))+ if is_blob {+ @memcpy(+ @ptradd(key, 12) as ref void,+ @ptradd(data.ptr, oid_start as i64) as ref void,+ 20+ )+ if idmap.put(lazy, bytes.bytes(key, 32)) {+ lazy_count+++ }+ }+ entries+++ ep = oid_start + 20+ }+ }+ p = body_end + 1+ }+ idmap.free(lazy)+ @free(key as ref void)+ files.free_buffer(data)+ files.delete(all_view)+ files.delete(meta_view)+ @free(all_path as ref void)+ @free(meta_path as ref void)++ val ms: i64 = (time.now_ns() - t0) / 1000000+ stdio.print(+ "commits={d} trees={d} tree_entries={d} lazy_blobs={d} total={d}ms\n",+ {commits, trees, entries, lazy_count, ms}+ )+ return 0+}--- a/src/src/cli/help_cmd.sx+++ b/src/src/cli/help_cmd.sx@@ -11,13 +11,14 @@ stdio.puts("vein is a tool for managing source code") stdio.puts("") stdio.puts("commands:")- stdio.puts(" init create a repository")- stdio.puts(" git import from git")- stdio.puts(" checkout write a revision into the working directory")- stdio.puts(" fsck verify repository integrity")- stdio.puts(" self-test run the format self-tests")- stdio.puts(" version print version information")- stdio.puts(" help show help for a command")+ stdio.puts(" init create a repository")+ stdio.puts(" git import from git")+ stdio.puts(" checkout write a revision into the working directory")+ stdio.puts(" fsck verify repository integrity")+ stdio.puts(" self-test run the format self-tests")+ stdio.puts(" version print version information")+ stdio.puts(" bench-import compare lazy import metadata using the git CLI")+ stdio.puts(" help show help for a command") stdio.puts("") stdio.puts("run 'vn help <command>' for details") }@@ -81,6 +82,13 @@ stdio.puts("usage: vn self-test") stdio.puts("") stdio.puts("run the format self-tests")+}++fn show_bench_import() void = trust {+ stdio.puts("usage: vn bench-import <gitdir>")+ stdio.puts("")+ stdio.puts("run the lazy-import metadata pass using the git CLI backend")+ stdio.puts("and print commit, tree, entry, and lazy-blob counts") } fn show_version() void = trust {@@ -148,6 +156,10 @@ } if cmd_is(cmd, c"version", 7) { show_version()+ return 0+ }+ if cmd_is(cmd, c"bench-import", 12) {+ show_bench_import() return 0 } if cmd_is(cmd, c"help", 4) {--- a/src/src/git/object.sx+++ b/src/src/git/object.sx@@ -550,7 +550,10 @@ idx = (idx + 1) & mask if idx == start { if tomb >= 0 {- val tslot: ref u32 = @ptradd(pack.cache_map as ref void, ((tomb as usize) * 4) as i64) as ref u32+ val tslot: ref u32 = @ptradd(+ pack.cache_map as ref void,+ ((tomb as usize) * 4) as i64+ ) as ref u32 @store32(tslot as ref void, (entry + 1) as i32) } return--- a/src/src/main.sx+++ b/src/src/main.sx@@ -10,6 +10,7 @@ val git_cmd = use("cli/git_cmd.sx") val checkout_cmd = use("cli/checkout_cmd.sx") val help_cmd = use("cli/help_cmd.sx")+val bench_cmd = use("cli/bench_cmd.sx") fn cmd_is(a: ref u8, text: ref u8, len: usize) bool = { if a == none {@@ -102,6 +103,9 @@ if cmd_is(cmd, c"version", 7) || cmd_is(cmd, c"--version", 9) { stdio.puts("vn 0.0.1 (vein-store 1)") return 0+ }+ if cmd_is(cmd, c"bench-import", 12) {+ return bench_cmd.run(args, 2) } if cmd_is(cmd, c"help", 4) || cmd_is(cmd, c"--help", 6) || cmd_is(cmd, c"-h", 2) { return help_cmd.run(args, 2)--- a/src/src/store/writer.sx+++ b/src/src/store/writer.sx@@ -4,22 +4,22 @@ /// the header count is patched, the pack trailer is hashed by streaming over /// the file, and the index is written from the collected record metadata. -val str = use("std/string")-val time = use("std/time")+val str = use("std/string")+val time = use("std/time") val allocators = use("std/allocators")-val thread = use("std/thread")-val bytes = use("../core/bytes.sx")-val buf = use("../core/buf.sx")-val lists = use("../core/lists.sx")-val objid = use("../object/id.sx")-val records = use("records.sx")-val idmap = use("idmap.sx")-val index = use("index.sx")-val zstd = use("zstd.sx")-val layout = use("../store/layout.sx")-val files = use("../io/files.sx")-val native = use("../io/native.sx")-val b3 = use("../crypto/blake3.sx")+val thread = use("std/thread")+val bytes = use("../core/bytes.sx")+val buf = use("../core/buf.sx")+val lists = use("../core/lists.sx")+val objid = use("../object/id.sx")+val records = use("records.sx")+val idmap = use("idmap.sx")+val index = use("index.sx")+val zstd = use("zstd.sx")+val layout = use("../store/layout.sx")+val files = use("../io/files.sx")+val native = use("../io/native.sx")+val b3 = use("../crypto/blake3.sx") extern (C) fn sysconf(name: i32) i64 = "sysconf" --- a/src/src/worktree/checkout.sx+++ b/src/src/worktree/checkout.sx@@ -465,7 +465,14 @@ some t => { val success: mut bool = false if task.kind == tree.KIND_SYMLINK {- success = write_symlink(job.handles, t, task.target, job.git, job.git_lock, job.has_git)+ 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@@ -476,7 +483,15 @@ } val fd: i32 = mut_fd if fd >= 0 {- success = write_blob_data(job.handles, fd, task.target, 0, job.git, job.git_lock, job.has_git)+ 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 {