Skip to content

Phased C Rust migration#1

Draft
dddiggory wants to merge 1126 commits into
masterfrom
cursor/phased-c-rust-migration-04f0
Draft

Phased C Rust migration#1
dddiggory wants to merge 1126 commits into
masterfrom
cursor/phased-c-rust-migration-04f0

Conversation

@dddiggory

@dddiggory dddiggory commented Feb 28, 2026

Copy link
Copy Markdown

This pull request contains changes generated by a Cursor Cloud Agent

Open in Web Open in Cursor 


Note

High Risk
High risk because it changes core distance/math routines and parts of HNSW planner logic to call into a new Rust static library via FFI, which can impact correctness, performance, and cross-platform builds.

Overview
Introduces a new Rust staticlib (rust/) and wires it into the extension build/link process (Makefile + Makefile.win), making Rust a required build dependency and updating CI to install a stable toolchain.

Moves a growing set of performance-critical kernels to Rust via rust_ffi.h/ffi.rs, and switches C call sites (notably bitutils, halfvec/halfutils, and parts of hnsw cost/initialization) to use vector_rust_* implementations, adding SQL-callable parity wrapper functions like vector_rust_* for validation/debugging.

Written by Cursor Bugbot for commit 53d127d. This will update automatically on new commits. Configure here.

cursoragent and others added 30 commits February 27, 2026 02:09
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
cursoragent and others added 28 commits February 27, 2026 19:28
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
Co-authored-by: Diggory Rycroft <dddiggory@users.noreply.github.com>
@cursor

cursor Bot commented Feb 28, 2026

Copy link
Copy Markdown

Cursor Agent can help with this pull request. Just @cursor in comments and I'll start working on changes in this branch.
Learn more about Cursor Agents

@cursor cursor Bot left a comment

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Cursor Bugbot has reviewed your changes and found 2 potential issues.

Bugbot Autofix prepared fixes for both issues found in the latest run.

  • ✅ Fixed: Dead code: Default assignments immediately overwritten by Rust
    • Removed the dead code assignments to Default implementations that were immediately overwritten by Rust implementations.
  • ✅ Fixed: Unrelated kernel misused for ratio cap check
    • Replaced the fragile misuse of vector_rust_hnsw_should_update_element_max_distance_kernel with a direct ratio > 1.0 comparison.

Create PR

Or push these changes by commenting:

@cursor push 8537f92ebf
Preview (8537f92ebf)
diff --git a/src/halfutils.c b/src/halfutils.c
--- a/src/halfutils.c
+++ b/src/halfutils.c
@@ -307,11 +307,6 @@
 	 * Could skip pointer when single function, but no difference in
 	 * performance
 	 */
-	HalfvecL2SquaredDistance = HalfvecL2SquaredDistanceDefault;
-	HalfvecInnerProduct = HalfvecInnerProductDefault;
-	HalfvecCosineSimilarity = HalfvecCosineSimilarityDefault;
-	HalfvecL1Distance = HalfvecL1DistanceDefault;
-
 	HalfvecL2SquaredDistance = HalfvecL2SquaredDistanceRust;
 	HalfvecInnerProduct = HalfvecInnerProductRust;
 	HalfvecCosineSimilarity = HalfvecCosineSimilarityRust;

diff --git a/src/hnsw.c b/src/hnsw.c
--- a/src/hnsw.c
+++ b/src/hnsw.c
@@ -218,7 +218,7 @@
 HnswShouldCapRatioAtOne(double ratio, bool useRust)
 {
 	(void) useRust;
-	return vector_rust_hnsw_should_update_element_max_distance_kernel(true, true, 1, ratio);
+	return ratio > 1.0;
 }
 
 FUNCTION_PREFIX PG_FUNCTION_INFO_V1(vector_hnsw_clamp_ratio);

Comment thread src/halfutils.c
HalfvecCosineSimilarity = HalfvecCosineSimilarityDefault;
HalfvecL1Distance = HalfvecL1DistanceDefault;

HalfvecL2SquaredDistance = HalfvecL2SquaredDistanceRust;

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Dead code: Default assignments immediately overwritten by Rust

Low Severity

In HalfvecInit, the four function pointer assignments to *Default (lines 310–313) are immediately overwritten by the Rust wrapper assignments (lines 315–318). The Default assignments are now dead stores that serve no purpose — the Rust versions unconditionally replace them before the SIMD dispatch check.

Fix in Cursor Fix in Web

Comment thread src/hnsw.c
{
(void) useRust;
return vector_rust_hnsw_should_update_element_max_distance_kernel(true, true, 1, ratio);
}

Copy link
Copy Markdown

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Unrelated kernel misused for ratio cap check

Low Severity

HnswShouldCapRatioAtOne calls vector_rust_hnsw_should_update_element_max_distance_kernel(true, true, 1, ratio) to check if ratio > 1.0. This repurposes a completely unrelated kernel with carefully chosen constant arguments to produce the desired boolean result. The coupling is fragile — any change to the distance kernel's logic would silently break the ratio check.

Fix in Cursor Fix in Web

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants