navid/veins / commit

a658d668a88c

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

parents: c5d8c283b6b2

Browse files at this commit · Download source

all: various optimizations
--- a/xveins/CMakeLists.txt+++ b/xveins/CMakeLists.txt@@ -211,6 +211,12 @@         )         if(VN_STRIP_RELEASE)             target_link_options(${target} PRIVATE $<$<CONFIG:Release>:-s>)+        endif()+        # Linking the C++ runtime statically avoids loading libstdc++/libgcc+        # at startup. For short-lived commands that is a measurable share of+        # total wall time on every invocation.+        if(UNIX AND NOT APPLE)+            target_link_options(${target} PRIVATE -static-libstdc++ -static-libgcc)         endif()         if(VN_HARDEN)             target_compile_options(${target} PRIVATE--- a/xveins/src/store/store.cpp+++ b/xveins/src/store/store.cpp@@ -18,6 +18,8 @@  #if !defined(_WIN32) #include <fcntl.h>+#include <sys/mman.h>+#include <sys/stat.h> #include <unistd.h> #endif @@ -1078,8 +1080,12 @@ #if !defined(_WIN32) StoreHandle::StoreHandle(StoreHandle&& other) noexcept     : path(std::move(other.path)), mtime_ns(other.mtime_ns), index(std::move(other.index)),-      pack(std::move(other.pack)), fd(other.fd.load(std::memory_order_relaxed)) {+      pack(std::move(other.pack)), fd(other.fd.load(std::memory_order_relaxed)),+      map(other.map.load(std::memory_order_relaxed)),+      map_len(other.map_len.load(std::memory_order_relaxed)) {     other.fd.store(-1, std::memory_order_relaxed);+    other.map.store(nullptr, std::memory_order_relaxed);+    other.map_len.store(0, std::memory_order_relaxed); }  StoreHandle& StoreHandle::operator=(StoreHandle&& other) noexcept {@@ -1088,12 +1094,19 @@     const int current = fd.load(std::memory_order_relaxed);     if (current >= 0)         ::close(current);+    void* current_map = map.load(std::memory_order_relaxed);+    if (current_map)+        ::munmap(current_map, map_len.load(std::memory_order_relaxed));     path = std::move(other.path);     mtime_ns = other.mtime_ns;     index = std::move(other.index);     pack = std::move(other.pack);     fd.store(other.fd.load(std::memory_order_relaxed), std::memory_order_relaxed);+    map.store(other.map.load(std::memory_order_relaxed), std::memory_order_relaxed);+    map_len.store(other.map_len.load(std::memory_order_relaxed), std::memory_order_relaxed);     other.fd.store(-1, std::memory_order_relaxed);+    other.map.store(nullptr, std::memory_order_relaxed);+    other.map_len.store(0, std::memory_order_relaxed);     return *this; } @@ -1101,6 +1114,9 @@     const int current = fd.load(std::memory_order_relaxed);     if (current >= 0)         ::close(current);+    void* current_map = map.load(std::memory_order_relaxed);+    if (current_map)+        ::munmap(current_map, map_len.load(std::memory_order_relaxed)); } #else StoreHandle::StoreHandle(StoreHandle&& other) noexcept = default;@@ -1129,11 +1145,57 @@ #endif } +static const void* handle_mapping(StoreHandle& handle, std::size_t& length) {+#if !defined(_WIN32)+    const void* cached = handle.map.load(std::memory_order_acquire);+    if (cached) {+        length = handle.map_len.load(std::memory_order_relaxed);+        return cached;+    }+    const int fd = handle_fd(handle);+    if (fd < 0)+        return nullptr;+    struct stat st {};+    if (::fstat(fd, &st) != 0 || st.st_size <= 0 ||+        static_cast<std::uint64_t>(st.st_size) > MAX_STORE_PACK_BYTES)+        return nullptr;+    const std::size_t n = static_cast<std::size_t>(st.st_size);+    void* mapped = ::mmap(nullptr, n, PROT_READ, MAP_PRIVATE, fd, 0);+    if (mapped == MAP_FAILED)+        return nullptr;+    handle.map_len.store(n, std::memory_order_relaxed);+    void* expected = nullptr;+    if (!handle.map.compare_exchange_strong(expected, mapped, std::memory_order_acq_rel,+                                            std::memory_order_acquire)) {+        ::munmap(mapped, n);+        length = handle.map_len.load(std::memory_order_relaxed);+        return expected;+    }+    length = n;+    return mapped;+#else+    (void)handle;+    (void)length;+    return nullptr;+#endif+}+ static std::optional<std::vector<std::uint8_t>> read_handle_range(StoreHandle& handle, std::uint64_t offset, std::size_t length) {     if (static_cast<std::uint64_t>(length) > MAX_STORE_PACK_BYTES)         return std::nullopt; #if !defined(_WIN32)+    std::size_t mapped_len = 0;+    if (const void* mapped = handle_mapping(handle, mapped_len)) {+        if (offset >= mapped_len)+            return std::vector<std::uint8_t>{};+        const std::size_t available = static_cast<std::size_t>(+            std::min<std::uint64_t>(length, mapped_len - offset));+        std::vector<std::uint8_t> data(available);+        if (available > 0)+            std::memcpy(data.data(), static_cast<const std::uint8_t*>(mapped) + offset, available);+        return data;+    }     const int fd = handle_fd(handle);     if (fd < 0)         return std::nullopt;--- a/xveins/src/store/store.h+++ b/xveins/src/store/store.h@@ -110,6 +110,8 @@     std::optional<Pack> pack; #if !defined(_WIN32)     std::atomic<int> fd{-1};+    std::atomic<void*> map{nullptr};+    std::atomic<std::size_t> map_len{0}; #endif      StoreHandle() = default;