navid/veins / commit
24917e1275ff
Navid M <navidmomtahen@gmail.com> · 2026-11-30
parents: ca62ff34c496
Browse files at this commit · Download source
--- a/.vnexclude+++ b/.vnexclude@@ -9,3 +9,4 @@ BENCH_RS_CPP.json BENCH_RS_CPP.md xveins/gmon.out+*.sum--- a/xveins/src/cli/cli.cpp+++ b/xveins/src/cli/cli.cpp@@ -2124,35 +2124,6 @@ return out; } -bool looks_binary(Bytes data) {- const std::size_t limit = std::min<std::size_t>(data.size(), 8192);- for (std::size_t i = 0; i < limit; ++i)- if (data[i] == 0)- return true;- return false;-}--std::uint64_t count_nonblank(Bytes data) {- std::uint64_t count = 0;- std::size_t i = 0;- while (i < data.size()) {- std::size_t end = i;- while (end < data.size() && data[end] != '\n')- ++end;- bool blank = true;- for (std::size_t j = i; j < end; ++j) {- if (data[j] != ' ' && data[j] != '\t' && data[j] != '\r') {- blank = false;- break;- }- }- if (!blank)- ++count;- i = end >= data.size() ? data.size() : end + 1;- }- return count;-}- std::uint64_t dir_size(const fs::path& path, std::size_t depth) { if (depth > 64) return 0;@@ -2262,13 +2233,14 @@ const auto& job = jobs[index]; FileResult result; result.ext = ext_of(job.entry->path);- if (auto data = read_blob(store, job.entry->target)) {- result.bytes = data->size();+ BlobMeasure measure;+ if (measure_blob(store, job.entry->target, measure)) {+ result.bytes = measure.bytes; const bool binary = job.attributes.text == AttributeText::Binary || (job.attributes.text == AttributeText::Auto &&- looks_binary(bytes_of(*data)));+ measure.has_nul); if (!binary)- result.lines = count_nonblank(bytes_of(*data));+ result.lines = measure.lines; } results[index] = std::move(result); }@@ -2276,10 +2248,12 @@ std::size_t workers = std::min<std::size_t>(available_cpu_count(), jobs.size()); workers = std::min<std::size_t>(workers, 16);+ // Load every pack index up front: the per-chunk lookups below are far+ // cheaper once the store no longer opens indexes lazily.+ store.preload(); if (workers <= 1) { worker(); } else {- store.preload(); std::vector<std::thread> threads; threads.reserve(workers); for (std::size_t w = 0; w < workers; ++w)--- a/xveins/src/object/object.h+++ b/xveins/src/object/object.h@@ -227,6 +227,15 @@ std::optional<Id> new_tree); std::optional<std::vector<std::uint8_t>> read_blob(Store& store, const Id& id);++struct BlobMeasure {+ std::uint64_t bytes = 0;+ std::uint64_t lines = 0;+ bool line_has_content = false;+ bool has_nul = false;+};++bool measure_blob(Store& store, const Id& id, BlobMeasure& out); bool is_lazy_blob(const Id& id); } // namespace vn--- a/xveins/src/store/store.cpp+++ b/xveins/src/store/store.cpp@@ -507,13 +507,11 @@ return {}; (void)ZSTD_CCtx_setParameter(ctx, ZSTD_c_checksumFlag, 0); const std::size_t bound = ZSTD_compressBound(data.size());- std::vector<std::uint8_t> out(bound);- const std::size_t n = ZSTD_compress2(ctx, out.data(), bound, data.data(), data.size());+ auto buffer = std::make_unique_for_overwrite<std::uint8_t[]>(bound);+ const std::size_t n = ZSTD_compress2(ctx, buffer.get(), bound, data.data(), data.size()); if (ZSTD_isError(n) || n >= data.size()) return {};- out.resize(n);- out.shrink_to_fit();- return out;+ return std::vector<std::uint8_t>(buffer.get(), buffer.get() + n); } namespace {@@ -546,12 +544,12 @@ (void)ZSTD_CCtx_setParameter(ctx, ZSTD_c_checksumFlag, 0); if (ZSTD_isError(ZSTD_CCtx_loadDictionary(ctx, dict.data(), dict.size()))) return false;- out.resize(ZSTD_compressBound(data.size()));- const std::size_t n = ZSTD_compress2(ctx, out.data(), out.size(), data.data(), data.size());+ const std::size_t bound = ZSTD_compressBound(data.size());+ auto buffer = std::make_unique_for_overwrite<std::uint8_t[]>(bound);+ const std::size_t n = ZSTD_compress2(ctx, buffer.get(), bound, data.data(), data.size()); if (ZSTD_isError(n) || n >= data.size()) return false;- out.resize(n);- out.shrink_to_fit();+ out.assign(buffer.get(), buffer.get() + n); return true; } @@ -839,7 +837,7 @@ std::size_t nearest = static_cast<std::size_t>(lower - base_root.offsets.begin()); std::size_t best_size = solo; std::size_t best_base = SIZE_MAX;- for (int delta = -2; delta <= 2; ++delta) {+ for (int delta : {0, -1, 1}) { const std::ptrdiff_t candidate = static_cast<std::ptrdiff_t>(nearest) + delta; if (candidate < 0 || candidate >= static_cast<std::ptrdiff_t>(base_root.entries.size()))@@ -1373,26 +1371,27 @@ } void Store::refresh(const fs::path& root) {- for (const auto& name : list_directory(packs_dir(root))) {+ const fs::path dir = packs_dir(root);+ std::unordered_set<std::string> known;+ known.reserve(handles_.size());+ for (const auto& handle : handles_)+ known.insert(basename_string(handle.path));+ bool added = false;+ for (const auto& name : list_directory(dir)) { if (name.size() <= 6) continue; if (name.compare(name.size() - 6, 6, ".vpack") != 0) continue;- const fs::path path = packs_dir(root) / path_from_bytes(name);- bool found = false;- for (const auto& handle : handles_) {- if (handle.path == path) {- found = true;- break;- }- }- if (!found) {- StoreHandle handle;- handle.path = path;- handle.mtime_ns = handle_mtime(path);- handles_.push_back(std::move(handle));- }+ if (!known.insert(name).second)+ continue;+ StoreHandle handle;+ handle.path = dir / path_from_bytes(name);+ handle.mtime_ns = handle_mtime(handle.path);+ handles_.push_back(std::move(handle));+ added = true; }+ if (!added)+ return; std::sort(handles_.begin(), handles_.end(), [](const StoreHandle& a, const StoreHandle& b) { if (a.mtime_ns != b.mtime_ns) return a.mtime_ns > b.mtime_ns;@@ -1538,6 +1537,71 @@ return std::move(*body); } +bool Store::append_body(const Id& id, std::uint8_t expected,+ std::vector<std::uint8_t>& out) {+ auto loc = find(id);+ if (!loc && expected == TYPE_CHUNK && promisor_fetcher && !root_.empty()) {+ if (promisor_fetcher(root_, id)) {+ refresh(root_);+ loc = find(id);+ }+ }+ if (!loc || loc->kind != expected)+ return false;+ StoreHandle& handle = handles_[loc->pack_index];+ const std::size_t start = out.size();+ std::vector<std::uint8_t> scratch;+ auto stored = handle_view(handle, loc->data_offset, static_cast<std::size_t>(loc->stored_len),+ scratch);+ if (!stored || static_cast<std::uint64_t>(stored->size()) != loc->stored_len)+ return false;+ if (loc->compression == 0) {+ out.insert(out.end(), stored->begin(), stored->end());+ } else if (loc->compression == 1) {+ const auto raw_len = static_cast<std::size_t>(loc->raw_len);+ out.resize(start + raw_len);+ const std::size_t n = ZSTD_decompressDCtx(thread_decompress_context(), out.data() + start,+ raw_len, stored->data(), stored->size());+ if (ZSTD_isError(n) || n != raw_len) {+ out.resize(start);+ return false;+ }+ } else if (loc->compression == 2) {+ Reader header(*stored);+ const auto base_off = header.varint();+ if (!base_off || *base_off >= loc->record_offset)+ return false;+ const auto frame = header.raw(header.remaining());+ if (!frame || frame->empty())+ return false;+ const auto base_loc = read_record_loc_at(handle, loc->pack_index, *base_off);+ if (!base_loc)+ return false;+ auto base_body = get_loc(handle, *base_loc, 1);+ if (!base_body)+ return false;+ const auto raw_len = static_cast<std::size_t>(loc->raw_len);+ out.resize(start + raw_len);+ const std::size_t n =+ ZSTD_decompress_usingDict(thread_decompress_context(), out.data() + start, raw_len,+ frame->data(), frame->size(), base_body->data(),+ base_body->size());+ if (ZSTD_isError(n) || n != raw_len) {+ out.resize(start);+ return false;+ }+ } else {+ return false;+ }+ const Id computed =+ object_id(expected, Bytes(out.data() + start, out.size() - start));+ if (std::memcmp(computed.data(), id.data(), ID_LEN) != 0) {+ out.resize(start);+ return false;+ }+ return true;+}+ std::optional<View> Store::read_view(const Id& id) { auto body = read_body(id, TYPE_VIEW); if (!body)@@ -1637,7 +1701,9 @@ int Writer::finish(const fs::path& root) { if (quota_hit_) return 2;- return write_pack(root, objects_, limit_);+ // Commits favour latency; `vn repack` re-compresses the history afterwards.+ constexpr int commit_level = 2;+ return write_pack(root, objects_, limit_, commit_level); } @@ -2466,11 +2532,13 @@ return true; } if (blob->form == BlobForm::Leaf) {+ std::uint64_t total = 0;+ for (const auto& entry : blob->entries)+ total += entry.length;+ out.reserve(out.size() + static_cast<std::size_t>(total)); for (const auto& entry : blob->entries) {- auto chunk = store.read_body(entry.id, TYPE_CHUNK);- if (!chunk)+ if (!store.append_body(entry.id, TYPE_CHUNK, out)) return false;- out.insert(out.end(), chunk->begin(), chunk->end()); } return true; }@@ -2488,7 +2556,91 @@ return out; } +static void scan_blob_bytes(Bytes data, BlobMeasure& out) {+ const std::uint8_t* p = data.data();+ std::size_t remaining = data.size();+ if (out.bytes < 8192) {+ const std::size_t prefix = std::min<std::size_t>(remaining, 8192 - out.bytes);+ if (std::memchr(p, 0, prefix) != nullptr)+ out.has_nul = true;+ }+ while (remaining > 0) {+ const auto* newline =+ static_cast<const std::uint8_t*>(std::memchr(p, '\n', remaining));+ const std::size_t length =+ newline ? static_cast<std::size_t>(newline - p) : remaining;+ if (!out.line_has_content) {+ for (std::size_t i = 0; i < length; ++i) {+ const std::uint8_t byte = p[i];+ if (byte != ' ' && byte != '\t' && byte != '\r') {+ out.line_has_content = true;+ break;+ }+ }+ }+ if (!newline) {+ p += length;+ remaining = 0;+ break;+ }+ if (out.line_has_content)+ ++out.lines;+ out.line_has_content = false;+ p = newline + 1;+ remaining -= length + 1;+ }+ out.bytes += data.size();+} +static bool measure_blob_data(Store& store, const Id& id, BlobMeasure& out,+ std::vector<std::uint8_t>& scratch, std::size_t depth) {+ if (depth > 64)+ return false;+ if (is_lazy_blob(id)) {+ auto data = git_read_lazy_blob(store.root(), id);+ if (!data)+ return false;+ scan_blob_bytes(bytes_of(*data), out);+ return true;+ }+ auto body = store.read_body(id, TYPE_BLOB);+ if (!body)+ return false;+ auto blob = decode_blob(bytes_of(*body));+ if (!blob)+ return false;+ if (blob->form == BlobForm::Inline) {+ scan_blob_bytes(bytes_of(blob->data), out);+ return true;+ }+ if (blob->form == BlobForm::Leaf) {+ for (const auto& entry : blob->entries) {+ scratch.clear();+ if (!store.append_body(entry.id, TYPE_CHUNK, scratch))+ return false;+ scan_blob_bytes(bytes_of(scratch), out);+ }+ return true;+ }+ for (const auto& entry : blob->entries) {+ if (!measure_blob_data(store, entry.id, out, scratch, depth + 1))+ return false;+ }+ return true;+}++bool measure_blob(Store& store, const Id& id, BlobMeasure& out) {+ std::vector<std::uint8_t> scratch;+ if (!measure_blob_data(store, id, out, scratch, 0))+ return false;+ if (out.line_has_content) {+ ++out.lines;+ out.line_has_content = false;+ }+ return true;+}++ namespace { struct FsckCtx {--- a/xveins/src/store/store.h+++ b/xveins/src/store/store.h@@ -136,6 +136,7 @@ std::optional<Loc> find(const Id& id); bool contains(const Id& id); Result<std::vector<std::uint8_t>> get(const Loc& loc);+ bool append_body(const Id& id, std::uint8_t expected, std::vector<std::uint8_t>& out); std::optional<std::vector<std::uint8_t>> read_body(const Id& id, std::uint8_t expected); std::optional<View> read_view(const Id& id); std::optional<Operation> read_operation(const Id& id);--- a/xveins/src/worktree/worktree.cpp+++ b/xveins/src/worktree/worktree.cpp@@ -363,6 +363,7 @@ struct BuildResult { BlobBuild build; bool ok = false;+ bool trusted = false; }; std::vector<BuildResult> results(jobs.size()); auto build_at = [&](std::size_t index) {@@ -379,6 +380,17 @@ results[index].build = build_blob(bytes_of(*data)); } results[index].ok = true;+ // A file whose timestamp has sub-millisecond precision cannot be+ // modified without the modification moving mtime, so a second stat+ // that matches the first makes this hash durable for the cache. Coarse+ // filesystems keep the conservative two-second window instead.+ if ((plan.info.mtime_ns % 1'000'000u) != 0) {+ const auto again = stat_path(plan.path);+ results[index].trusted =+ again && again->size == plan.info.size && again->mtime_ns == plan.info.mtime_ns &&+ again->inode == plan.info.inode && again->kind == plan.info.kind &&+ again->executable == plan.info.executable;+ } }; if (job_bytes >= 1'000'000) parallel_for(jobs.size(), build_at);@@ -397,6 +409,7 @@ (void)txn.add_known(object.type, id, std::move(object.body)); } plan.target = results[i].build.root;+ plan.trusted = plan.trusted || results[i].trusted; } std::vector<TreeEntry> entries;