back to atomics, no perf improvement
This commit is contained in:
parent
b7c1f19204
commit
9dc0c4b9aa
3 changed files with 29 additions and 74 deletions
|
@ -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| {
|
c.bench_function("update positions", move |b| {
|
||||||
let points: Vec<_> = create_random_points(DEFAULT_NUM_POINTS, SEED_1)
|
let points: Vec<_> = create_random_points(DEFAULT_NUM_POINTS, SEED_1)
|
||||||
.into_iter()
|
.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);
|
criterion_main!(benches);
|
||||||
|
|
55
src/lib.rs
55
src/lib.rs
|
@ -40,8 +40,8 @@ impl Ord for PVal {
|
||||||
|
|
||||||
#[derive(Debug, Default, Clone)]
|
#[derive(Debug, Default, Clone)]
|
||||||
pub struct Spindex2d {
|
pub struct Spindex2d {
|
||||||
xpoints: Arc<RwLock<Vec<usize>>>,
|
xpoints: Arc<Vec<AtomicUsize>>,
|
||||||
ypoints: Arc<RwLock<Vec<usize>>>,
|
ypoints: Arc<Vec<AtomicUsize>>,
|
||||||
xs: Arc<RwLock<Vec<PVal>>>,
|
xs: Arc<RwLock<Vec<PVal>>>,
|
||||||
ys: Arc<RwLock<Vec<PVal>>>,
|
ys: Arc<RwLock<Vec<PVal>>>,
|
||||||
}
|
}
|
||||||
|
@ -64,13 +64,13 @@ impl Spindex2d {
|
||||||
};
|
};
|
||||||
ys.push(y);
|
ys.push(y);
|
||||||
|
|
||||||
xpoints.push(i);
|
xpoints.push(i.into());
|
||||||
ypoints.push(i);
|
ypoints.push(i.into());
|
||||||
}
|
}
|
||||||
|
|
||||||
let index = Self {
|
let index = Self {
|
||||||
xpoints: Arc::new(RwLock::new(xpoints)),
|
xpoints: Arc::new(xpoints),
|
||||||
ypoints: Arc::new(RwLock::new(ypoints)),
|
ypoints: Arc::new(ypoints),
|
||||||
xs: Arc::new(RwLock::new(xs)),
|
xs: Arc::new(RwLock::new(xs)),
|
||||||
ys: Arc::new(RwLock::new(ys)),
|
ys: Arc::new(RwLock::new(ys)),
|
||||||
};
|
};
|
||||||
|
@ -84,27 +84,25 @@ impl Spindex2d {
|
||||||
/// when constructing this index. May panic if the updated points argument is longer than the
|
/// when constructing this index. May panic if the updated points argument is longer than the
|
||||||
/// original points.
|
/// original points.
|
||||||
pub fn update(&self, points: &[Point2d]) {
|
pub fn update(&self, points: &[Point2d]) {
|
||||||
let mut xs = self.xs.write().unwrap();
|
{
|
||||||
let mut ys = self.ys.write().unwrap();
|
let mut xs = self.xs.write().unwrap();
|
||||||
let xpoints = self.xpoints.read().unwrap();
|
let mut ys = self.ys.write().unwrap();
|
||||||
let ypoints = self.ypoints.read().unwrap();
|
for (i, point) in points.iter().enumerate() {
|
||||||
for (i, point) in points.iter().enumerate() {
|
let xi = self.xpoints[i].load(Relaxed);
|
||||||
println!("hello: {i}");
|
let yi = self.ypoints[i].load(Relaxed);
|
||||||
let xi = xpoints[i];
|
let x = PVal {
|
||||||
let yi = ypoints[i];
|
point: i,
|
||||||
let x = PVal {
|
val: point.x,
|
||||||
point: i,
|
};
|
||||||
val: point.x,
|
xs[xi] = x;
|
||||||
};
|
|
||||||
xs[xi] = x;
|
|
||||||
|
|
||||||
let y = PVal {
|
let y = PVal {
|
||||||
point: i,
|
point: i,
|
||||||
val: point.y,
|
val: point.y,
|
||||||
};
|
};
|
||||||
ys[yi] = y;
|
ys[yi] = y;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
println!("done updating points");
|
|
||||||
self.sort();
|
self.sort();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -114,10 +112,9 @@ impl Spindex2d {
|
||||||
let xhandle = thread::spawn(move || {
|
let xhandle = thread::spawn(move || {
|
||||||
let mut xs = xs.write().unwrap();
|
let mut xs = xs.write().unwrap();
|
||||||
xs.sort_unstable();
|
xs.sort_unstable();
|
||||||
let mut points = points.write().unwrap();
|
|
||||||
for (i, x) in xs.iter().enumerate() {
|
for (i, x) in xs.iter().enumerate() {
|
||||||
let p = x.point;
|
let p = x.point;
|
||||||
points[p] = i;
|
points[p].store(i, Relaxed);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
@ -126,10 +123,10 @@ impl Spindex2d {
|
||||||
let yhandle = thread::spawn(move || {
|
let yhandle = thread::spawn(move || {
|
||||||
let mut ys = ys.write().unwrap();
|
let mut ys = ys.write().unwrap();
|
||||||
ys.sort_unstable();
|
ys.sort_unstable();
|
||||||
let mut points = points.write().unwrap();
|
|
||||||
for (i, y) in ys.iter().enumerate() {
|
for (i, y) in ys.iter().enumerate() {
|
||||||
let p = y.point;
|
let p = y.point;
|
||||||
points[p] = i;
|
points[p].store(i, Relaxed);
|
||||||
}
|
}
|
||||||
});
|
});
|
||||||
|
|
||||||
|
|
44
src/main.rs
44
src/main.rs
|
@ -1,43 +1 @@
|
||||||
use rand::{Rng, SeedableRng};
|
fn main() {}
|
||||||
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);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
Loading…
Reference in a new issue