back to atomics, no perf improvement

This commit is contained in:
Joe 2025-10-17 13:13:11 -07:00
parent b7c1f19204
commit 9dc0c4b9aa
3 changed files with 29 additions and 74 deletions

View file

@ -29,7 +29,7 @@ fn new_index(c: &mut Criterion) {
});
}
fn _update_positions(c: &mut Criterion) {
fn update_positions(c: &mut Criterion) {
c.bench_function("update positions", move |b| {
let points: Vec<_> = create_random_points(DEFAULT_NUM_POINTS, SEED_1)
.into_iter()
@ -60,5 +60,5 @@ fn _update_positions(c: &mut Criterion) {
});
}
criterion_group!(benches, new_index);
criterion_group!(benches, new_index, update_positions);
criterion_main!(benches);

View file

@ -40,8 +40,8 @@ impl Ord for PVal {
#[derive(Debug, Default, Clone)]
pub struct Spindex2d {
xpoints: Arc<RwLock<Vec<usize>>>,
ypoints: Arc<RwLock<Vec<usize>>>,
xpoints: Arc<Vec<AtomicUsize>>,
ypoints: Arc<Vec<AtomicUsize>>,
xs: Arc<RwLock<Vec<PVal>>>,
ys: Arc<RwLock<Vec<PVal>>>,
}
@ -64,13 +64,13 @@ impl Spindex2d {
};
ys.push(y);
xpoints.push(i);
ypoints.push(i);
xpoints.push(i.into());
ypoints.push(i.into());
}
let index = Self {
xpoints: Arc::new(RwLock::new(xpoints)),
ypoints: Arc::new(RwLock::new(ypoints)),
xpoints: Arc::new(xpoints),
ypoints: Arc::new(ypoints),
xs: Arc::new(RwLock::new(xs)),
ys: Arc::new(RwLock::new(ys)),
};
@ -84,14 +84,12 @@ impl Spindex2d {
/// when constructing this index. May panic if the updated points argument is longer than the
/// original points.
pub fn update(&self, points: &[Point2d]) {
{
let mut xs = self.xs.write().unwrap();
let mut ys = self.ys.write().unwrap();
let xpoints = self.xpoints.read().unwrap();
let ypoints = self.ypoints.read().unwrap();
for (i, point) in points.iter().enumerate() {
println!("hello: {i}");
let xi = xpoints[i];
let yi = ypoints[i];
let xi = self.xpoints[i].load(Relaxed);
let yi = self.ypoints[i].load(Relaxed);
let x = PVal {
point: i,
val: point.x,
@ -104,7 +102,7 @@ impl Spindex2d {
};
ys[yi] = y;
}
println!("done updating points");
}
self.sort();
}
@ -114,10 +112,9 @@ impl Spindex2d {
let xhandle = thread::spawn(move || {
let mut xs = xs.write().unwrap();
xs.sort_unstable();
let mut points = points.write().unwrap();
for (i, x) in xs.iter().enumerate() {
let p = x.point;
points[p] = i;
points[p].store(i, Relaxed);
}
});
@ -126,10 +123,10 @@ impl Spindex2d {
let yhandle = thread::spawn(move || {
let mut ys = ys.write().unwrap();
ys.sort_unstable();
let mut points = points.write().unwrap();
for (i, y) in ys.iter().enumerate() {
let p = y.point;
points[p] = i;
points[p].store(i, Relaxed);
}
});

View file

@ -1,43 +1 @@
use rand::{Rng, SeedableRng};
use rand_hc::Hc128Rng;
use spacedog::*;
fn create_random_points(num_points: usize, seed: &[u8; 32]) -> Vec<[f64; 2]> {
let mut rng = Hc128Rng::from_seed(*seed);
(0..num_points).map(|_| rng.random()).collect()
}
const SEED_1: &[u8; 32] = b"Gv0aHMtHkBGsUXNspGU9fLRuCWkZWHZx";
// const SEED_2: &[u8; 32] = b"km7DO4GeaFZfTcDXVpnO7ZJlgUY7hZiS";
const DEFAULT_NUM_POINTS: usize = 1_000_000;
fn main() {
let points: Vec<_> = create_random_points(DEFAULT_NUM_POINTS, SEED_1)
.into_iter()
.map(|[x, y]| Point2d { x, y })
.collect();
let mut rng = Hc128Rng::from_seed(*SEED_1);
let jitters: Vec<_> = points
.iter()
.map(|p| {
let x = p.x + rng.random_range(-5.0..=5.0);
let y = p.y + rng.random_range(-5.0..=5.0);
Point2d { x, y }
})
.collect();
let index = Spindex2d::new(&points);
for round in 0..100 {
dbg!(round);
if round % 2 == 0 {
index.update(&jitters);
} else {
index.update(&points);
}
}
}
fn main() {}