usearch 2.26.4 → 2.26.5

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
package/README.md CHANGED
@@ -568,7 +568,7 @@ doi = {10.5281/zenodo.7949415},
568
568
  author = {Vardanian, Ash},
569
569
  title = {{USearch by Unum Cloud}},
570
570
  url = {https://github.com/unum-cloud/USearch},
571
- version = {2.26.4},
571
+ version = {2.26.5},
572
572
  year = {2026},
573
573
  }
574
574
  ```
package/c/CMakeLists.txt CHANGED
@@ -30,4 +30,13 @@ if (USEARCH_BUILD_LIB_C)
30
30
  setup_target(usearch_c)
31
31
  setup_target(usearch_static_c)
32
32
 
33
+ if (USEARCH_INSTALL)
34
+ install(TARGETS usearch_c usearch_static_c
35
+ LIBRARY DESTINATION ${CMAKE_INSTALL_LIBDIR}
36
+ ARCHIVE DESTINATION ${CMAKE_INSTALL_LIBDIR}
37
+ RUNTIME DESTINATION ${CMAKE_INSTALL_BINDIR})
38
+ install(FILES ${CMAKE_CURRENT_SOURCE_DIR}/usearch.h
39
+ DESTINATION ${CMAKE_INSTALL_INCLUDEDIR})
40
+ endif ()
41
+
33
42
  endif ()
@@ -1,4 +1,7 @@
1
- Name: ${PROJECT_NAME}
1
+ prefix=@CMAKE_INSTALL_PREFIX@
2
+ includedir=@CMAKE_INSTALL_FULL_INCLUDEDIR@
3
+
4
+ Name: @PROJECT_NAME@
2
5
  Description: Smaller & Faster Single-File Vector Search Engine from Unum
3
- Version: ${PROJECT_VERSION}
4
- Cflags: -I${CMAKE_INSTALL_FULL_INCLUDEDIR}
6
+ Version: @PROJECT_VERSION@
7
+ Cflags: -I${includedir}
package/cpp/test.cpp CHANGED
@@ -1534,6 +1534,36 @@ void test_view_of_truncated_file() {
1534
1534
  std::remove(path);
1535
1535
  }
1536
1536
 
1537
+ /**
1538
+ * @brief Regression test: every thread context must draw node levels of its own. Default-seeded, all
1539
+ * contexts drew the same levels and started over after every `reserve`, so every per-level node
1540
+ * count was a multiple of `contexts * steps`, the upper levels too full and the top ones missing.
1541
+ */
1542
+ void test_level_generators() {
1543
+ constexpr std::size_t contexts = 16, steps = 16, per_context_step = 64, dimensions = 2;
1544
+ metric_punned_t metric(dimensions, metric_kind_t::l2sq_k, scalar_kind_t::f32_k);
1545
+ index_dense_t index = index_dense_t::make(metric);
1546
+
1547
+ std::mt19937 generator(42);
1548
+ std::uniform_real_distribution<float> distribution(0.0, 1.0);
1549
+
1550
+ std::size_t key = 0;
1551
+ for (std::size_t step = 0; step != steps; ++step) {
1552
+ expect(index.try_reserve(index_limits_t((step + 1) * contexts * per_context_step, contexts)));
1553
+ for (std::size_t idx = 0; idx != contexts * per_context_step; ++idx, ++key) {
1554
+ float vector[dimensions] = {distribution(generator), distribution(generator)};
1555
+ expect(index.add(key, vector, idx % contexts));
1556
+ }
1557
+ }
1558
+ expect_eq(index.size(), contexts * steps * per_context_step);
1559
+
1560
+ // Independent draws put about 1024 nodes on level 1 and 64 on level 2: almost never multiples of 256.
1561
+ bool all_multiples = true;
1562
+ for (std::size_t level = 1; level <= index.max_level(); ++level)
1563
+ all_multiples = all_multiples && index.stats(level).nodes % (contexts * steps) == 0;
1564
+ expect(!all_multiples);
1565
+ }
1566
+
1537
1567
  int main(int, char**) {
1538
1568
  install_crash_handlers();
1539
1569
 
@@ -1632,5 +1662,6 @@ int main(int, char**) {
1632
1662
  test_compact();
1633
1663
  test_load_after_metric_make();
1634
1664
  test_view_of_truncated_file();
1665
+ test_level_generators();
1635
1666
  return 0;
1636
1667
  }
@@ -9,7 +9,7 @@
9
9
 
10
10
  #define USEARCH_VERSION_MAJOR 2
11
11
  #define USEARCH_VERSION_MINOR 26
12
- #define USEARCH_VERSION_PATCH 4
12
+ #define USEARCH_VERSION_PATCH 5
13
13
 
14
14
  // Inferring C++ version
15
15
  // https://stackoverflow.com/a/61552074
@@ -96,7 +96,7 @@
96
96
  #include <cstring> // `std::memset`
97
97
  #include <iterator> // `std::reverse_iterator`
98
98
  #include <mutex> // `std::unique_lock` - replacement candidate
99
- #include <random> // `std::default_random_engine` - replacement candidate
99
+ #include <random> // `std::uniform_real_distribution`
100
100
  #include <stdexcept> // `std::runtime_exception`
101
101
  #include <thread> // `std::thread`
102
102
  #include <utility> // `std::pair`
@@ -1277,6 +1277,29 @@ template <typename element_at> struct hash_gt {
1277
1277
  std::size_t operator()(element_at const& element) const noexcept { return std::hash<element_at>{}(element); }
1278
1278
  };
1279
1279
 
1280
+ /**
1281
+ * @brief Eight-byte, non-cryptographic uniform random bit generator for STL distributions.
1282
+ * @see https://prng.di.unimi.it/splitmix64.c (public-domain reference implementation).
1283
+ * @details Every seed is valid, including zero. Generation and skipping use unsigned wraparound.
1284
+ */
1285
+ class splitmix64_t {
1286
+ std::uint64_t state_;
1287
+
1288
+ public:
1289
+ using result_type = std::uint64_t;
1290
+ static result_type mix_uint64(result_type x) noexcept {
1291
+ x = (x ^ (x >> 30u)) * 0xBF58476D1CE4E5B9ULL;
1292
+ x = (x ^ (x >> 27u)) * 0x94D049BB133111EBULL;
1293
+ return x ^ (x >> 31u);
1294
+ }
1295
+ constexpr explicit splitmix64_t(result_type value = 0) noexcept : state_(value) {}
1296
+ static constexpr result_type(min)() noexcept { return 0; }
1297
+ static constexpr result_type(max)() noexcept { return (std::numeric_limits<result_type>::max)(); }
1298
+ void seed(result_type value = 0) noexcept { state_ = value; }
1299
+ result_type operator()() noexcept { return mix_uint64(state_ += 0x9E3779B97F4A7C15ULL); }
1300
+ void discard(unsigned long long count) noexcept { state_ += result_type(count) * 0x9E3779B97F4A7C15ULL; }
1301
+ };
1302
+
1280
1303
  /**
1281
1304
  * @brief SplitMix64 finalizer, used to scatter integer keys before masking.
1282
1305
  *
@@ -1289,10 +1312,7 @@ template <typename element_at> struct hash_gt {
1289
1312
  */
1290
1313
  template <> struct hash_gt<std::uint64_t> {
1291
1314
  std::size_t operator()(std::uint64_t const& element) const noexcept {
1292
- std::uint64_t x = element;
1293
- x = (x ^ (x >> 30u)) * 0xBF58476D1CE4E5B9ULL;
1294
- x = (x ^ (x >> 27u)) * 0x94D049BB133111EBULL;
1295
- return static_cast<std::size_t>(x ^ (x >> 31u));
1315
+ return static_cast<std::size_t>(splitmix64_t::mix_uint64(element));
1296
1316
  }
1297
1317
  };
1298
1318
 
@@ -2489,7 +2509,7 @@ class index_gt {
2489
2509
  top_candidates_t top_for_refine{};
2490
2510
  next_candidates_t next_candidates{};
2491
2511
  visits_hash_set_t visits{};
2492
- std::default_random_engine level_generator{};
2512
+ splitmix64_t level_generator{};
2493
2513
  std::size_t iteration_cycles{};
2494
2514
  std::size_t computed_distances{};
2495
2515
  std::size_t computed_distances_in_refines{};
@@ -2687,6 +2707,10 @@ class index_gt {
2687
2707
  if (!other.reserve(limits_))
2688
2708
  return result.failed("Failed to reserve the contexts");
2689
2709
 
2710
+ // A copy must continue the same node-level streams as the original.
2711
+ for (std::size_t i = 0; i != contexts_.size(); ++i)
2712
+ other.contexts_[i].level_generator = contexts_[i].level_generator;
2713
+
2690
2714
  // Now all is left - is to allocate new `node_t` instances and populate
2691
2715
  // the `other.nodes_` array into it.
2692
2716
  for (std::size_t i = 0; i != nodes_count_; ++i)
@@ -2892,10 +2916,15 @@ class index_gt {
2892
2916
 
2893
2917
  /**
2894
2918
  * @brief Increases the `capacity()` of the index to allow adding more vectors.
2919
+ * @note Thread reservations never shrink; `reset()` releases all contexts.
2895
2920
  * @return `true` on success, `false` on memory allocation errors.
2896
2921
  */
2897
2922
  bool try_reserve(index_limits_t limits) usearch_noexcept_m {
2898
2923
 
2924
+ // Keep previously allocated contexts, including streams for temporarily unused threads.
2925
+ limits.threads_add = (std::max)(limits.threads_add, limits_.threads_add);
2926
+ limits.threads_search = (std::max)(limits.threads_search, limits_.threads_search);
2927
+
2899
2928
  if (limits.threads_add <= limits_.threads_add //
2900
2929
  && limits.threads_search <= limits_.threads_search //
2901
2930
  && limits.members <= limits_.members)
@@ -2918,10 +2947,19 @@ class index_gt {
2918
2947
  // Move the nodes info, and deallocate previous buffers.
2919
2948
  if (nodes_)
2920
2949
  std::memcpy(new_nodes.data(), nodes_.data(), sizeof(node_t) * size());
2921
- for (std::size_t i = 0; i != new_contexts.size(); ++i)
2950
+ for (std::size_t i = 0; i != new_contexts.size(); ++i) {
2922
2951
  if (!new_contexts[i].top_for_refine.reserve(connectivity_max + 1))
2923
2952
  return false;
2924
2953
 
2954
+ // Copy rather than advance the old stream, so a failed reserve leaves it unchanged.
2955
+ if (i < contexts_.size())
2956
+ new_contexts[i].level_generator = contexts_[i].level_generator;
2957
+ else {
2958
+ // Mix logical thread slots instead of using adjacent seeds (or an OS thread ID).
2959
+ new_contexts[i].level_generator.seed(splitmix64_t::mix_uint64(static_cast<std::uint64_t>(i) + 1));
2960
+ }
2961
+ }
2962
+
2925
2963
  limits_ = limits;
2926
2964
  nodes_capacity_ = limits.members;
2927
2965
  nodes_ = std::move(new_nodes);
@@ -3211,11 +3249,13 @@ class index_gt {
3211
3249
  if (!next.reserve(config.expansion))
3212
3250
  return result.failed("Out of memory!");
3213
3251
 
3252
+ // Each caller exclusively owns its context; sampling needs no shared graph lock.
3253
+ level_t new_target_level = choose_random_level_(context.level_generator);
3254
+
3214
3255
  // Determining how much memory to allocate for the node depends on the target level
3215
3256
  std::unique_lock<std::mutex> new_level_lock(global_mutex_);
3216
3257
  level_t max_level_copy = max_level_; // Copy under lock
3217
3258
  compressed_slot_t entry_slot_copy = static_cast<compressed_slot_t>(entry_slot_); // Copy under lock
3218
- level_t new_target_level = choose_random_level_(context.level_generator);
3219
3259
 
3220
3260
  // Make sure we are not overflowing
3221
3261
  std::size_t capacity = nodes_capacity_.load();
@@ -4366,10 +4406,11 @@ class index_gt {
4366
4406
  }
4367
4407
  }
4368
4408
 
4369
- level_t choose_random_level_(std::default_random_engine& level_generator) const noexcept {
4409
+ level_t choose_random_level_(splitmix64_t& level_generator) const noexcept {
4370
4410
  std::uniform_real_distribution<double> distribution(0.0, 1.0);
4371
- double r = -std::log(distribution(level_generator)) * pre_.inverse_log_connectivity;
4372
- return (level_t)r;
4411
+ // Reflect into (0, 1] and guard against a rounded upper endpoint from the STL.
4412
+ double uniform = (std::max)(1.0 - distribution(level_generator), (std::numeric_limits<double>::min)());
4413
+ return static_cast<level_t>(-std::log(uniform) * pre_.inverse_log_connectivity);
4373
4414
  }
4374
4415
 
4375
4416
  struct candidates_range_t;
@@ -1053,6 +1053,9 @@ class index_dense_gt {
1053
1053
  // Reserving is monotonic: reducing thread limits must not shrink member-addressed storage.
1054
1054
  limits.members = (std::max)(limits.members, typed_->capacity());
1055
1055
  limits.members = (std::max)(limits.members, vectors_lookup_.size());
1056
+ // Match the core's retained contexts so cast buffers and available thread IDs stay consistent.
1057
+ limits.threads_add = (std::max)(limits.threads_add, typed_->limits().threads_add);
1058
+ limits.threads_search = (std::max)(limits.threads_search, typed_->limits().threads_search);
1056
1059
 
1057
1060
  // The slot lookup system will generally prefer power-of-two sizes.
1058
1061
  if (config_.enable_key_lookups) {
@@ -526,8 +526,10 @@ inline std::uint16_t f32_to_f16(float f32) noexcept {
526
526
  */
527
527
  inline float bf16_to_f32(std::uint16_t u16) noexcept {
528
528
  #if USEARCH_USE_NUMKONG
529
+ nk_bf16_t bits;
530
+ std::memcpy(&bits, &u16, sizeof(bits));
529
531
  nk_f32_t result;
530
- nk_bf16_to_f32_serial((nk_bf16_t const*)&u16, &result);
532
+ nk_bf16_to_f32_serial(&bits, &result);
531
533
  return result;
532
534
  #else
533
535
  union float_or_unsigned_int_t {
package/package.json CHANGED
@@ -1,6 +1,6 @@
1
1
  {
2
2
  "name": "usearch",
3
- "version": "2.26.4",
3
+ "version": "2.26.5",
4
4
  "description": "Smaller & Faster Single-File Vector Search Engine from Unum",
5
5
  "author": "Ash Vardanian (https://ashvardanian.com/)",
6
6
  "license": "Apache 2.0",
Binary file
Binary file
Binary file