navid/veins / commit
4d24b0b41743
Navid M <navidmomtahen@gmail.com> · 2026-11-26
parents: 389b4ed583e5
Browse files at this commit · Download source
--- a/src/src/cli/bench_cmd.sx+++ b/src/src/cli/bench_cmd.sx@@ -1,9 +1,7 @@ /// 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")@@ -10,19 +8,31 @@ 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+++extern (C) fn popen(cmd: ref u8, mode: ref u8) ref void = "popen"+extern (C) fn pclose(stream: ref void) i32 = "pclose"+extern (C) fn fread(ptr: ref void, size: usize, n: usize, stream: ref void) usize = "fread"+extern (C) fn memchr(s: ref void, c: i32, n: usize) ref void = "memchr"++fn find_raw(p: ref u8, start: usize, end: usize, target: u8) usize = trust {+ if start >= end {+ return end }- return i+ val hit: ref void = memchr(+ @ptradd(p, start as i64) as ref void,+ target as i32,+ end - start+ )+ if hit == none {+ return end+ }+ return @bitcast(hit, usize) - @bitcast(p, usize) } -fn parse_size(data: bytes.Bytes, start: usize, end: usize) usize = {+fn parse_size_raw(p: ref u8, start: usize, end: usize) usize = trust { val n: mut usize = 0 val i: mut usize = start for i < end {- val c: u8 = bytes.Bytes.at(data, i)+ val c: u8 = @load8(@ptradd(p, i as i64)) if c < 48 || c > 57 { break }@@ -32,20 +42,33 @@ 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+fn eq_raw(p: ref u8, start: usize, text: ref u8, n: usize) bool = trust {+ val i: mut usize = 0+ for i < n {+ if @load8(@ptradd(p, (start + i) as i64)) != @load8(@ptradd(text, i as i64)) {+ return false }- err _ => {- return none+ i+++ }+ return true+}++fn capture(cmd: ref u8, out: mut buf.Buf) bool = trust {+ val pipe = popen(cmd, c"r")+ if pipe == none {+ return false+ }+ val chunk: ref u8 = @alloc(65536) as ref u8+ for {+ val n: usize = fread(chunk as ref void, 1, 65536, pipe)+ if n == 0 {+ break }+ buf.Buf.put_raw(out, chunk, n) }+ @free(chunk as ref void)+ pclose(pipe)+ return true } pub fn run(args: list[ref u8], start: usize) i32 = trust {@@ -61,89 +84,33 @@ 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)+ val listing: mut buf.Buf = buf.new(1048576)+ val list_cmd: ref u8 = @fmtalloc(+ "git --git-dir={s} rev-list --objects --all --filter=blob:none",+ {repo_view}+ )+ val listed: bool = capture(list_cmd, listing) @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", {})+ if !listed {+ buf.Buf.free(listing)+ stdio.eprint("vn: cannot run git rev-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)- }- }+ val lp: ref u8 = listing.ptr+ val llen: usize = listing.len+ val pos: mut usize = 0+ for pos < llen {+ val eol: usize = find_raw(lp, pos, llen, 10)+ val end: usize = find_raw(lp, pos, eol, 32)+ if end > pos {+ buf.Buf.put_raw(meta_buf, @ptradd(lp, pos as i64), end - pos)+ buf.Buf.put_u8(meta_buf, 10) }- cp = eol2 + 1+ pos = eol + 1 }- files.free_buffer(check)+ buf.Buf.free(listing) 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)@@ -150,37 +117,28 @@ 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 data: mut buf.Buf = buf.new(1048576) 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)+ val batched: bool = capture(batch_cmd, data) @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- }+ if !batched {+ buf.Buf.free(data)+ files.delete(meta_view)+ @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)+ @memcpy(key as ref void, c"vein-git-oid", 12) val lazy: mut idmap.IdSet = idmap.new(16384) val commits: mut usize = 0 val trees: mut usize = 0@@ -187,34 +145,39 @@ val entries: mut usize = 0 val lazy_count: mut usize = 0 + val dp: ref u8 = data.ptr+ val dlen: usize = data.len 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)+ for p < dlen {+ val nl: usize = find_raw(dp, p, dlen, 10)+ val sp1: usize = find_raw(dp, p, nl, 32)+ val sp2: usize = find_raw(dp, sp1 + 1, nl, 32)+ val size: usize = parse_size_raw(dp, 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)) {+ val kind_len: usize = sp2 - sp1 - 1++ if kind_len == 6 && eq_raw(dp, sp1 + 1, c"commit", 6) { commits++- } elif bytes.Bytes.eql(kind, bytes.bytes(c"tree", 4)) {+ } elif kind_len == 4 && eq_raw(dp, sp1 + 1, 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 space: usize = find_raw(dp, ep, body_end, 32)+ val nul: usize = find_raw(dp, space + 1, body_end, 0) 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))+ val mode_len: usize = space - ep+ val is_blob: bool = mode_len == 6+ && @load8(@ptradd(dp, ep as i64)) == 49+ && (@load8(@ptradd(dp, (ep + 1) as i64)) == 48+ || @load8(@ptradd(dp, (ep + 1) as i64)) == 50) if is_blob { @memcpy( @ptradd(key, 12) as ref void,- @ptradd(data.ptr, oid_start as i64) as ref void,+ @ptradd(dp, oid_start as i64) as ref void, 20 ) if idmap.put(lazy, bytes.bytes(key, 32)) {@@ -229,10 +192,8 @@ } idmap.free(lazy) @free(key as ref void)- files.free_buffer(data)- files.delete(all_view)+ buf.Buf.free(data) files.delete(meta_view)- @free(all_path as ref void) @free(meta_path as ref void) val ms: i64 = (time.now_ns() - t0) / 1000000