Skip to content

Commit 165b7de

Browse files
authored
fix: vectorize as much code as possible (#569)
Signed-off-by: usamoi <[email protected]>
1 parent d259b24 commit 165b7de

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

107 files changed

+3567
-5122
lines changed

Cargo.lock

Lines changed: 5 additions & 20 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -23,10 +23,10 @@ arrayvec.workspace = true
2323
bincode.workspace = true
2424
byteorder.workspace = true
2525
chrono = "0.4.38"
26+
half.workspace = true
2627
libc.workspace = true
2728
log.workspace = true
2829
memmap2.workspace = true
29-
num-traits.workspace = true
3030
paste.workspace = true
3131
pgrx = { version = "=0.12.1", default-features = false, features = [] }
3232
rand.workspace = true
@@ -67,11 +67,10 @@ arc-swap = "1.7.0"
6767
arrayvec = "0.7.4"
6868
bincode = "1.3.3"
6969
byteorder = "1.5.0"
70-
half = { version = "2.4.0", features = ["num-traits", "rand_distr", "serde"] }
70+
half = { version = "2.4.0", features = ["rand_distr", "serde"] }
7171
libc = "0.2.153"
7272
log = { version = "0.4.21", features = ["std"] }
7373
memmap2 = "0.9.4"
74-
num-traits = "0.2.19"
7574
parking_lot = "0.12.1"
7675
paste = "1.0.14"
7776
rand = "0.8.5"
@@ -99,3 +98,4 @@ overflow-checks = false
9998
[profile.release]
10099
lto = "fat"
101100
codegen-units = 1
101+
debug = true

crates/base/Cargo.toml

Lines changed: 0 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -6,15 +6,13 @@ edition.workspace = true
66
[dependencies]
77
half.workspace = true
88
libc.workspace = true
9-
num-traits.workspace = true
109
rand.workspace = true
1110
serde.workspace = true
1211
thiserror.workspace = true
1312
toml.workspace = true
1413
validator.workspace = true
1514

1615
base_macros = { path = "../base_macros" }
17-
ccc = { path = "../ccc" }
1816
detect = { path = "../detect" }
1917

2018
[lints]

crates/base/src/aligned.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#[derive(Debug, Clone, Copy)]
2+
#[repr(C, align(32))]
3+
pub struct Aligned32<T>(pub T);

crates/base/src/lib.rs

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1-
#![feature(core_intrinsics)]
21
#![feature(avx512_target_feature)]
32
#![cfg_attr(target_arch = "x86_64", feature(stdarch_x86_avx512))]
4-
#![allow(internal_features)]
3+
#![cfg_attr(target_arch = "x86_64", feature(stdarch_x86_avx512_f16))]
54
#![allow(clippy::derivable_impls)]
65
#![allow(clippy::len_without_is_empty)]
76
#![allow(clippy::len_zero)]
87
#![allow(clippy::needless_range_loop)]
98
#![allow(clippy::nonminimal_bool)]
109

10+
pub mod aligned;
1111
pub mod always_equal;
1212
pub mod distance;
1313
pub mod index;

crates/base/src/operator/bvector_dot.rs renamed to crates/base/src/operator/bvect_dot.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use crate::vector::*;
66
pub enum BVectorDot {}
77

88
impl Operator for BVectorDot {
9-
type VectorOwned = BVectorOwned;
10-
11-
const DISTANCE_KIND: DistanceKind = DistanceKind::Dot;
9+
type Vector = BVectOwned;
1210

1311
fn distance(lhs: Borrowed<'_, Self>, rhs: Borrowed<'_, Self>) -> Distance {
1412
lhs.operator_dot(rhs)

crates/base/src/operator/bvector_hamming.rs renamed to crates/base/src/operator/bvect_hamming.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use crate::vector::*;
66
pub enum BVectorHamming {}
77

88
impl Operator for BVectorHamming {
9-
type VectorOwned = BVectorOwned;
10-
11-
const DISTANCE_KIND: DistanceKind = DistanceKind::Hamming;
9+
type Vector = BVectOwned;
1210

1311
fn distance(lhs: Borrowed<'_, Self>, rhs: Borrowed<'_, Self>) -> Distance {
1412
lhs.operator_hamming(rhs)

crates/base/src/operator/bvector_jaccard.rs renamed to crates/base/src/operator/bvect_jaccard.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,9 +6,7 @@ use crate::vector::*;
66
pub enum BVectorJaccard {}
77

88
impl Operator for BVectorJaccard {
9-
type VectorOwned = BVectorOwned;
10-
11-
const DISTANCE_KIND: DistanceKind = DistanceKind::Jaccard;
9+
type Vector = BVectOwned;
1210

1311
fn distance(lhs: Borrowed<'_, Self>, rhs: Borrowed<'_, Self>) -> Distance {
1412
lhs.operator_jaccard(rhs)

crates/base/src/operator/mod.rs

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -1,34 +1,27 @@
1-
mod bvector_dot;
2-
mod bvector_hamming;
3-
mod bvector_jaccard;
4-
mod svecf32_dot;
5-
mod svecf32_l2;
6-
mod vecf16_dot;
7-
mod vecf16_l2;
8-
mod vecf32_dot;
9-
mod vecf32_l2;
1+
mod bvect_dot;
2+
mod bvect_hamming;
3+
mod bvect_jaccard;
4+
mod svect_dot;
5+
mod svect_l2;
6+
mod vect_dot;
7+
mod vect_l2;
108

11-
pub use bvector_dot::BVectorDot;
12-
pub use bvector_hamming::BVectorHamming;
13-
pub use bvector_jaccard::BVectorJaccard;
14-
pub use svecf32_dot::SVecf32Dot;
15-
pub use svecf32_l2::SVecf32L2;
16-
pub use vecf16_dot::Vecf16Dot;
17-
pub use vecf16_l2::Vecf16L2;
18-
pub use vecf32_dot::Vecf32Dot;
19-
pub use vecf32_l2::Vecf32L2;
9+
pub use bvect_dot::BVectorDot;
10+
pub use bvect_hamming::BVectorHamming;
11+
pub use bvect_jaccard::BVectorJaccard;
12+
pub use svect_dot::SVectDot;
13+
pub use svect_l2::SVectL2;
14+
pub use vect_dot::VectDot;
15+
pub use vect_l2::VectL2;
2016

2117
use crate::distance::*;
2218
use crate::vector::*;
2319

2420
pub trait Operator: Copy + 'static + Send + Sync {
25-
type VectorOwned: VectorOwned;
26-
27-
const DISTANCE_KIND: DistanceKind;
21+
type Vector: VectorOwned;
2822

2923
fn distance(lhs: Borrowed<'_, Self>, rhs: Borrowed<'_, Self>) -> Distance;
3024
}
3125

32-
pub type Owned<T> = <T as Operator>::VectorOwned;
33-
pub type Borrowed<'a, T> = <<T as Operator>::VectorOwned as VectorOwned>::Borrowed<'a>;
34-
pub type Scalar<T> = <<T as Operator>::VectorOwned as VectorOwned>::Scalar;
26+
pub type Owned<T> = <T as Operator>::Vector;
27+
pub type Borrowed<'a, T> = <<T as Operator>::Vector as VectorOwned>::Borrowed<'a>;

crates/base/src/operator/svecf32_l2.rs

Lines changed: 0 additions & 16 deletions
This file was deleted.

crates/base/src/operator/svecf32_dot.rs renamed to crates/base/src/operator/svect_dot.rs

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
use crate::distance::*;
22
use crate::operator::*;
3+
use crate::scalar::ScalarLike;
34
use crate::vector::*;
5+
use std::marker::PhantomData;
46

57
#[derive(Debug, Clone, Copy)]
6-
pub enum SVecf32Dot {}
8+
pub struct SVectDot<S>(std::convert::Infallible, PhantomData<fn(S) -> S>);
79

8-
impl Operator for SVecf32Dot {
9-
type VectorOwned = SVecf32Owned;
10-
11-
const DISTANCE_KIND: DistanceKind = DistanceKind::Dot;
10+
impl<S: ScalarLike> Operator for SVectDot<S> {
11+
type Vector = SVectOwned<S>;
1212

1313
fn distance(lhs: Borrowed<'_, Self>, rhs: Borrowed<'_, Self>) -> Distance {
1414
lhs.operator_dot(rhs)

crates/base/src/operator/svect_l2.rs

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,16 @@
1+
use crate::distance::*;
2+
use crate::operator::*;
3+
use crate::scalar::ScalarLike;
4+
use crate::vector::*;
5+
use std::marker::PhantomData;
6+
7+
#[derive(Debug, Clone, Copy)]
8+
pub struct SVectL2<S>(std::convert::Infallible, PhantomData<fn(S) -> S>);
9+
10+
impl<S: ScalarLike> Operator for SVectL2<S> {
11+
type Vector = SVectOwned<S>;
12+
13+
fn distance(lhs: SVectBorrowed<'_, S>, rhs: SVectBorrowed<'_, S>) -> Distance {
14+
lhs.operator_l2(rhs)
15+
}
16+
}

crates/base/src/operator/vecf16_dot.rs

Lines changed: 0 additions & 16 deletions
This file was deleted.

crates/base/src/operator/vecf16_l2.rs

Lines changed: 0 additions & 16 deletions
This file was deleted.

crates/base/src/operator/vecf32_dot.rs

Lines changed: 0 additions & 16 deletions
This file was deleted.

crates/base/src/operator/vecf32_l2.rs

Lines changed: 0 additions & 16 deletions
This file was deleted.

0 commit comments

Comments
 (0)