navid/veins / commit

2adfcb3ebdd7

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

parents: a56780e67a9f

Browse files at this commit · Download source

opt: dont rehash the entire working tree here
--- a/xveins/src/worktree/worktree.cpp+++ b/xveins/src/worktree/worktree.cpp@@ -27,6 +27,7 @@     std::uint8_t kind = 0;     bool executable = false;     Id target{};+    bool trusted = false; };  struct Cache {@@ -79,6 +80,8 @@         return false;     if (entry.executable != executable || entry.kind != info.kind)         return false;+    if (entry.trusted)+        return true;     const std::int64_t now_ns = now_ms * 1'000'000;     const std::int64_t written_ns = written_ms * 1'000'000;     constexpr std::int64_t window = 2'000'000'000;@@ -98,7 +101,7 @@         return cache;     Reader r(Bytes(data->data() + 4, data->size() - 4));     const auto version = r.u8();-    if (!version || *version != 49)+    if (!version || (*version != 49 && *version != 50))         return cache;     const auto written = r.u64le();     if (!written)@@ -116,11 +119,19 @@         const auto inode = r.u64le();         const auto kind = r.u8();         const auto executable = r.u8();+        bool trusted = false;+        if (*version >= 50) {+            const auto trusted_byte = r.u8();+            if (!trusted_byte)+                break;+            trusted = *trusted_byte != 0;+        }         const auto target = r.id();         if (!size || !mtime || !inode || !kind || !executable || !target)             break;         cache.entries.push_back(-            CacheEntry{std::move(*path), *size, *mtime, *inode, *kind, *executable != 0, *target});+            CacheEntry{std::move(*path), *size, *mtime, *inode, *kind, *executable != 0, *target,+                       trusted});     }     std::stable_sort(cache.entries.begin(), cache.entries.end(),                      [](const CacheEntry& a, const CacheEntry& b) {@@ -133,7 +144,7 @@                 std::int64_t written_ms) {     ByteWriter w(4096);     w.raw("VST1");-    w.u8(49);+    w.u8(50);     w.u64le(static_cast<std::uint64_t>(written_ms));     w.varint(entries.size());     for (const auto& entry : entries) {@@ -143,6 +154,7 @@         w.u64le(entry.inode);         w.u8(entry.kind);         w.u8(entry.executable ? 1 : 0);+        w.u8(entry.trusted ? 1 : 0);         w.id(entry.target);     }     const fs::path final_path = working_copy_dir(root) / "stat-cache";@@ -184,6 +196,33 @@     if (!body)         return std::nullopt;     return txn.add_object(TYPE_TREE, std::move(*body));+}++template <typename Function> void parallel_for(std::size_t count, Function function) {+    if (count == 0)+        return;+    std::size_t workers = std::min<std::size_t>(available_cpu_count(), count);+    workers = std::min<std::size_t>(workers, 16);+    if (workers <= 1) {+        for (std::size_t i = 0; i < count; ++i)+            function(i);+        return;+    }+    std::atomic<std::size_t> next{0};+    std::vector<std::thread> threads;+    threads.reserve(workers);+    for (std::size_t worker = 0; worker < workers; ++worker) {+        threads.emplace_back([&]() {+            for (;;) {+                const std::size_t index = next.fetch_add(1);+                if (index >= count)+                    return;+                function(index);+            }+        });+    }+    for (auto& thread : threads)+        thread.join(); }  std::optional<Id> build_dir(Txn& txn, const fs::path& root, std::string_view base,@@ -226,9 +265,37 @@         return compare(bytes_of(a), bytes_of(b)) < 0;     }); -    std::vector<TreeEntry> entries;-    for (const auto& name : names) {+    std::vector<std::optional<StatInfo>> infos(names.size());+    auto stat_at = [&](std::size_t index) {+        infos[index] = stat_path(dir / path_from_bytes(names[index]));+    };+    if (names.size() >= 256)+        parallel_for(names.size(), stat_at);+    else+        for (std::size_t i = 0; i < names.size(); ++i)+            stat_at(i);++    enum class PlanKind { Skip, Conflict, Directory, Symlink, File };+    struct Plan {+        PlanKind kind = PlanKind::Skip;+        std::string name;+        std::string rel;+        fs::path path;+        StatInfo info;+        std::optional<Id> target;+        std::optional<Id> child_old;+        bool executable = false;+        bool needs_read = false;+        bool trusted = false;+    };++    std::vector<Plan> plans;+    plans.reserve(names.size());+    for (std::size_t i = 0; i < names.size(); ++i) {+        const std::string& name = names[i];         if (is_meta_name(name))+            continue;+        if (!infos[i])             continue;         if (old_tree) {             if (auto base_name = sidecar_base(name)) {@@ -239,19 +306,19 @@             }         } -        const std::string rel = join_rel(base, name);-        const fs::path fs_path = dir / path_from_bytes(name);-        const auto info = stat_path(fs_path);-        if (!info)-            continue;-        const bool is_dir = info->kind == 2;-        const bool ignored = ignore_ignored(local, rel, is_dir);+        Plan plan;+        plan.name = name;+        plan.rel = join_rel(base, name);+        plan.path = dir / path_from_bytes(name);+        plan.info = *infos[i];+        const bool is_dir = plan.info.kind == 2;+        const bool ignored = ignore_ignored(local, plan.rel, is_dir);         const int old_index = old_tree ? find_tree_name(*old_tree, name) : -1;         if (old_index >= 0 &&             old_tree->entries[static_cast<std::size_t>(old_index)].kind == KIND_CONFLICT) {-            entries.push_back(-                TreeEntry{name, KIND_CONFLICT,-                          old_tree->entries[static_cast<std::size_t>(old_index)].target});+            plan.kind = PlanKind::Conflict;+            plan.target = old_tree->entries[static_cast<std::size_t>(old_index)].target;+            plans.push_back(std::move(plan));             continue;         }         if (ignored && old_index < 0)@@ -258,56 +325,125 @@             continue;          if (is_dir) {-            std::optional<Id> child_old;+            plan.kind = PlanKind::Directory;             if (old_tree && old_index >= 0) {                 const auto& old_entry = old_tree->entries[static_cast<std::size_t>(old_index)];                 if (old_entry.kind == KIND_SUBTREE)-                    child_old = old_entry.target;+                    plan.child_old = old_entry.target;             }-            auto child = build_dir(txn, root, rel, child_old, local, attributes, cache_valid, cache,-                                   new_cache, now_ms, state, depth + 1);+            plans.push_back(std::move(plan));+        } else if (plan.info.kind == 3) {+            plan.kind = PlanKind::Symlink;+            plans.push_back(std::move(plan));+        } else if (plan.info.kind == 1) {+            plan.kind = PlanKind::File;+            plan.executable = plan.info.executable;+            if (cache_valid) {+                if (const auto* cached = cache_lookup(cache, plan.rel)) {+                    if (cache_matches(*cached, plan.info, plan.executable, now_ms,+                                      cache.written_ms)) {+                        plan.target = cached->target;+                        plan.trusted = cached->trusted;+                    }+                }+            }+            plan.needs_read = !plan.target.has_value();+            plans.push_back(std::move(plan));+        }+    }++    std::vector<std::size_t> jobs;+    std::uint64_t job_bytes = 0;+    for (std::size_t i = 0; i < plans.size(); ++i) {+        if (plans[i].kind == PlanKind::File && plans[i].needs_read) {+            jobs.push_back(i);+            job_bytes += plans[i].info.size;+        }+    }++    struct BuildResult {+        BlobBuild build;+        bool ok = false;+    };+    std::vector<BuildResult> results(jobs.size());+    auto build_at = [&](std::size_t index) {+        const Plan& plan = plans[jobs[index]];+        auto data = read_file_opt(plan.path);+        if (!data)+            return;+        const auto path_attributes = attributes.lookup(plan.rel);+        if (path_attributes.eol != AttributeEol::None &&+            path_attributes.text != AttributeText::Binary) {+            const auto normalized = normalize_eol(bytes_of(*data), path_attributes.eol);+            results[index].build = build_blob(bytes_of(normalized));+        } else {+            results[index].build = build_blob(bytes_of(*data));+        }+        results[index].ok = true;+    };+    if (job_bytes >= 1'000'000)+        parallel_for(jobs.size(), build_at);+    else+        for (std::size_t i = 0; i < jobs.size(); ++i)+            build_at(i);++    for (std::size_t i = 0; i < jobs.size(); ++i) {+        if (!results[i].ok) {+            state.failed = true;+            return std::nullopt;+        }+        Plan& plan = plans[jobs[i]];+        for (auto& object : results[i].build.objects) {+            const Id id = object.id ? *object.id : object_id(object.type, bytes_of(object.body));+            (void)txn.add_known(object.type, id, std::move(object.body));+        }+        plan.target = results[i].build.root;+    }++    std::vector<TreeEntry> entries;+    entries.reserve(plans.size());+    for (auto& plan : plans) {+        switch (plan.kind) {+        case PlanKind::Skip:+            break;+        case PlanKind::Conflict:+            if (plan.target)+                entries.push_back(TreeEntry{plan.name, KIND_CONFLICT, *plan.target});+            break;+        case PlanKind::Directory: {+            auto child = build_dir(txn, root, plan.rel, plan.child_old, local, attributes, cache_valid,+                                   cache, new_cache, now_ms, state, depth + 1);             if (state.failed)                 return std::nullopt;             if (child)-                entries.push_back(TreeEntry{name, KIND_SUBTREE, *child});-        } else if (info->kind == 3) {-            auto target = path_read_symlink(fs_path);+                entries.push_back(TreeEntry{plan.name, KIND_SUBTREE, *child});+            break;+        }+        case PlanKind::Symlink: {+            auto target = path_read_symlink(plan.path);             if (!target) {                 state.failed = true;                 return std::nullopt;             }             const Id blob = add_blob_data(txn, bytes_of(*target));-            entries.push_back(TreeEntry{name, KIND_SYMLINK, blob});-            new_cache.push_back(-                CacheEntry{rel, info->size, info->mtime_ns, info->inode, info->kind, false, blob});-        } else if (info->kind == 1) {-            const bool executable = info->executable;-            std::optional<Id> target;-            if (cache_valid) {-                if (const auto* cached = cache_lookup(cache, rel)) {-                    if (cache_matches(*cached, *info, executable, now_ms, cache.written_ms)) {-                        target = cached->target;-                    }-                }-            }-            if (!target) {-                auto data = read_file_opt(fs_path);-                if (!data) {-                    state.failed = true;-                    return std::nullopt;-                }-                const auto path_attributes = attributes.lookup(rel);-                if (path_attributes.eol != AttributeEol::None &&-                    path_attributes.text != AttributeText::Binary) {-                    const auto normalized = normalize_eol(bytes_of(*data), path_attributes.eol);-                    target = add_blob_data(txn, bytes_of(normalized));-                } else {-                    target = add_blob_data(txn, bytes_of(*data));-                }+            entries.push_back(TreeEntry{plan.name, KIND_SYMLINK, blob});+            new_cache.push_back(CacheEntry{plan.rel, plan.info.size, plan.info.mtime_ns,+                                           plan.info.inode, plan.info.kind, false, blob, false});+            break;+        }+        case PlanKind::File: {+            if (!plan.target) {+                state.failed = true;+                return std::nullopt;             }-            new_cache.push_back(CacheEntry{rel, info->size, info->mtime_ns, info->inode, info->kind,-                                           executable, *target});-            entries.push_back(TreeEntry{name, executable ? KIND_EXECUTABLE : KIND_FILE, *target});+            new_cache.push_back(CacheEntry{plan.rel, plan.info.size, plan.info.mtime_ns,+                                           plan.info.inode, plan.info.kind, plan.executable,+                                           *plan.target, plan.trusted});+            entries.push_back(TreeEntry{plan.name,+                                        plan.executable ? KIND_EXECUTABLE : KIND_FILE,+                                        *plan.target});+            break;+        }         }     } @@ -615,6 +751,26 @@         remove_tracked_path(root, path);     }     return ok;+}++void save_checkout_cache(const fs::path& root, const std::vector<FlatEntry>& entries) {+    std::vector<CacheEntry> cache;+    cache.reserve(entries.size());+    const std::int64_t now_ms = unix_now_millis();+    for (const auto& entry : entries) {+        if (entry.kind != KIND_FILE && entry.kind != KIND_EXECUTABLE &&+            entry.kind != KIND_SYMLINK)+            continue;+        const auto info = stat_path(root / path_from_bytes(entry.path));+        if (!info)+            continue;+        cache.push_back(CacheEntry{entry.path, info->size, info->mtime_ns, info->inode,+                                   info->kind, info->executable, entry.target, true});+    }+    std::stable_sort(cache.begin(), cache.end(), [](const CacheEntry& a, const CacheEntry& b) {+        return compare(bytes_of(a.path), bytes_of(b.path)) < 0;+    });+    (void)save_cache(root, cache, now_ms); }  int materialize_tree(Store& store, const fs::path& root, const std::vector<FlatEntry>& old_entries,@@ -753,6 +909,7 @@             progress->advance();     } +    save_checkout_cache(root, new_entries);     if (progress)         progress->finish();     info_prefix();