navid/veins / commit

5abea8f29f2c

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

parents: c1fd2fd008fa

Browse files at this commit · Download source

opt: push optimizations
--- a/xveins/src/cli/net_commands.cpp+++ b/xveins/src/cli/net_commands.cpp@@ -17,6 +17,7 @@ #include <optional> #include <set> #include <string>+#include <unordered_set> #include <vector>  namespace vn {@@ -190,6 +191,25 @@     }     return ok; }++struct IdHasher {+    std::size_t operator()(const Id& id) const noexcept {+        std::size_t h = 1469598103934665603ull;+        for (auto b : id) {+            h ^= b;+            h *= 1099511628211ull;+        }+        return h;+    }+};++struct IdEqual {+    bool operator()(const Id& a, const Id& b) const noexcept {+        return std::memcmp(a.data(), b.data(), ID_LEN) == 0;+    }+};++using IdSet = std::unordered_set<Id, IdHasher, IdEqual>;  struct Walker {     Walker(Store& store, bool want_objects) : store(store), want_objects(want_objects) {}@@ -196,11 +216,11 @@      Store& store;     bool want_objects = false;-    std::set<Id> blobs;-    std::set<Id> conflicts;-    std::set<Id> chunks;-    std::set<Id> trees;-    std::set<Id> objects;+    IdSet blobs;+    IdSet conflicts;+    IdSet chunks;+    IdSet trees;+    IdSet objects;     std::vector<Id> chunk_out;     std::vector<Id> object_out; @@ -258,9 +278,34 @@             walk_conflict(id);         else             walk_blob(id);+    }++    void walk_entry(const TreeEntry& entry) {+        if (entry.kind == KIND_SUBTREE)+            walk_tree(entry.target);+        else if (entry.kind == KIND_CONFLICT)+            walk_conflict(entry.target);+        else if (entry.kind != KIND_SUBMODULE)+            walk_blob(entry.target);     }      void walk_tree(const Id& id) {+        if (!trees.insert(id).second)+            return;+        add_object(id);+        auto body = store.read_body(id, TYPE_TREE);+        if (!body)+            return;+        auto tree = decode_tree(bytes_of(*body));+        if (!tree)+            return;+        for (const auto& entry : tree->entries)+            walk_entry(entry);+    }++    void walk_tree_delta(const Id& id, const std::optional<Id>& old_id) {+        if (old_id && same_id(*old_id, id))+            return;         if (!trees.insert(id).second)             return;         add_object(id);@@ -270,13 +315,36 @@         auto tree = decode_tree(bytes_of(*body));         if (!tree)             return;+        std::optional<Tree> old_tree;+        if (old_id) {+            auto old_body = store.read_body(*old_id, TYPE_TREE);+            if (old_body) {+                auto decoded = decode_tree(bytes_of(*old_body));+                if (decoded)+                    old_tree = std::move(*decoded);+            }+        }+        std::size_t old_index = 0;         for (const auto& entry : tree->entries) {-            if (entry.kind == KIND_SUBTREE)-                walk_tree(entry.target);-            else if (entry.kind == KIND_CONFLICT)-                walk_conflict(entry.target);+            if (!old_tree) {+                walk_entry(entry);+                continue;+            }+            while (old_index < old_tree->entries.size() &&+                   compare(bytes_of(old_tree->entries[old_index].name), bytes_of(entry.name)) < 0)+                ++old_index;+            if (old_index >= old_tree->entries.size() ||+                compare(bytes_of(old_tree->entries[old_index].name), bytes_of(entry.name)) != 0) {+                walk_entry(entry);+                continue;+            }+            const auto& old_entry = old_tree->entries[old_index++];+            if (old_entry.kind == entry.kind && same_id(old_entry.target, entry.target))+                continue;+            if (entry.kind == KIND_SUBTREE && old_entry.kind == KIND_SUBTREE)+                walk_tree_delta(entry.target, old_entry.target);             else if (entry.kind != KIND_SUBMODULE)-                walk_blob(entry.target);+                walk_entry(entry);         }     } };@@ -291,15 +359,56 @@     return std::move(walker.chunk_out); } -std::vector<Id> objects_for_commits(Store& store, const std::vector<Id>& commits,-                                    const std::function<void(std::size_t)>& progress = {}) {+std::vector<Id> collect_commits_until(Store& store, std::span<const Id> start,+                                      const IdSet& stop) {+    std::vector<Id> out;+    std::vector<Id> stack;+    IdSet seen;+    for (const auto& id : start) {+        if (stop.find(id) == stop.end() && seen.insert(id).second)+            stack.push_back(id);+    }+    while (!stack.empty()) {+        const Id id = stack.back();+        stack.pop_back();+        out.push_back(id);+        auto loaded = load_commit(store, id);+        if (!loaded)+            continue;+        for (const auto& parent : loaded->commit.parents) {+            if (stop.find(parent) != stop.end())+                continue;+            if (seen.insert(parent).second)+                stack.push_back(parent);+        }+    }+    return out;+}++std::vector<Id> objects_for_commit_deltas(Store& store, const std::vector<Id>& commits,+                                         const std::function<void(std::size_t)>& progress = {}) {     Walker walker{store, true};     std::size_t done = 0;     for (const auto& id : commits) {         walker.add_object(id);         auto loaded = load_commit(store, id);-        if (loaded)-            walker.walk_tree(loaded->commit.root_tree);+        if (loaded) {+            std::optional<Id> old_tree;+            bool tree_covered = false;+            for (const auto& parent_id : loaded->commit.parents) {+                auto parent = load_commit(store, parent_id);+                if (!parent)+                    continue;+                if (same_id(parent->commit.root_tree, loaded->commit.root_tree)) {+                    tree_covered = true;+                    break;+                }+                if (!old_tree)+                    old_tree = parent->commit.root_tree;+            }+            if (!tree_covered)+                walker.walk_tree_delta(loaded->commit.root_tree, old_tree);+        }         if (progress)             progress(++done);     }@@ -976,23 +1085,26 @@         std::cout << "nothing to push, everything is up to date\n";         return 0;     }+    IdSet remote_commits;+    remote_commits.reserve(refs.size());+    for (const auto& ref : refs)+        remote_commits.insert(ref.target);+     std::vector<Id> roots{*target};     for (const auto& tag : tags)         roots.push_back(tag.second);-    const std::vector<Id> commits = collect_commits(store, roots);+    const std::vector<Id> commits = collect_commits_until(store, roots, remote_commits);     std::vector<Id> objects;-    {+    if (!commits.empty()) {         Progress progress("collecting objects", commits.size());-        objects = objects_for_commits(store, commits,-                                      [&](std::size_t done) { progress.update(done); });+        objects = objects_for_commit_deltas(+            store, commits, [&](std::size_t done) { progress.update(done); });         progress.finish();     }-    bool pushed = false;+    bool pushed = true;     if (!objects.empty()) {         const std::vector<Id> missing = missing_objects(*client, objects);-        if (missing.empty()) {-            pushed = true;-        } else {+        if (!missing.empty()) {             Progress progress("pushing objects", missing.size());             pushed = client->push_objects(root, missing,                                           [&](std::size_t done) { progress.update(done); });@@ -999,8 +1111,6 @@             if (pushed)                 progress.finish();         }-    } else {-        pushed = client->push_objects(root, objects);     }     if (!pushed) {         report_client_error(*client, "push failed");--- a/xveins/src/net/net.cpp+++ b/xveins/src/net/net.cpp@@ -29,6 +29,7 @@ #include <limits.h> #include <netdb.h> #include <netinet/in.h>+#include <netinet/tcp.h> #include <signal.h> #include <sys/socket.h> #include <sys/types.h>@@ -98,7 +99,17 @@     }     return out; }+#endif++void enable_tcp_nodelay(Socket socket) {+    int one = 1;+#if defined(_WIN32)+    (void)setsockopt(static_cast<SOCKET>(socket), IPPROTO_TCP, TCP_NODELAY,+                     reinterpret_cast<const char*>(&one), sizeof(one));+#else+    (void)setsockopt(static_cast<int>(socket), IPPROTO_TCP, TCP_NODELAY, &one, sizeof(one)); #endif+}  std::string ssl_error_string() {     unsigned long code = ERR_get_error();@@ -463,7 +474,10 @@ #endif     }     freeaddrinfo(results);-    return connected == INVALID_SOCKET_VALUE ? std::nullopt : std::optional<Socket>(connected);+    if (connected == INVALID_SOCKET_VALUE)+        return std::nullopt;+    enable_tcp_nodelay(connected);+    return std::optional<Socket>(connected); }  Socket tcp_listen(int port, int backlog) {@@ -509,11 +523,14 @@     const SOCKET accepted = ::accept(static_cast<SOCKET>(listener), nullptr, nullptr);     if (accepted == INVALID_SOCKET)         return std::nullopt;-    return static_cast<Socket>(accepted);+    const Socket socket = static_cast<Socket>(accepted);+    enable_tcp_nodelay(socket);+    return socket; #else     const int accepted = ::accept(static_cast<int>(listener), nullptr, nullptr);     if (accepted < 0)         return std::nullopt;+    enable_tcp_nodelay(accepted);     return accepted; #endif }--- a/xveins/tests/integration_tests.cpp+++ b/xveins/tests/integration_tests.cpp@@ -1243,9 +1243,12 @@     f.vn({"cfg", "remote.origin.key", "integration"}, clone_a);     f.write("a.txt", "from clone a\n", clone_a);     f.commit("clone a change", clone_a);+    f.write("c.txt", "from clone a again\n", clone_a);+    f.commit("clone a second change", clone_a);     const CommandResult pushed = f.vn({"push", "origin", "master"}, clone_a);     t.expect_contains(pushed.out, "pushed master");     t.expect_contains(f.vn({"log"}, source).out, "clone a change");+    t.expect_contains(f.vn({"log"}, source).out, "clone a second change");     t.expect_contains(f.vn({"whoami", "origin"}, clone_a).out, "role: write");      f.vn({"tag", "set", "v0.0.1"}, clone_a);