Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[WIP] Query random access APIs #32

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions benches/benchmarks.rs
Original file line number Diff line number Diff line change
Expand Up @@ -106,7 +106,7 @@ fn bench_iter_simple(c: &mut Criterion) {
let mut query = <(Read<Position>, Write<Rotation>)>::query();

b.iter(|| {
for (pos, mut rot) in query.iter(&world) {
for (pos, mut rot) in query.iter(&mut world) {
rot.0 = pos.0;
}
});
Expand All @@ -129,7 +129,7 @@ fn bench_iter_complex(c: &mut Criterion) {
.filter(!component::<A>() & tag_value(&Tag(2.0)));

b.iter(|| {
for (pos, mut rot) in query.iter(&world) {
for (pos, mut rot) in query.iter(&mut world) {
rot.0 = pos.0;
}
});
Expand All @@ -144,7 +144,7 @@ fn bench_iter_chunks_simple(c: &mut Criterion) {
let mut query = <(Write<Position>, Read<Rotation>)>::query();

b.iter(|| {
for c in query.iter_chunks(&world) {
for c in query.iter_chunks(&mut world) {
unsafe {
c.components_mut::<Position>()
.unwrap()
Expand Down Expand Up @@ -172,7 +172,7 @@ fn bench_iter_chunks_complex(c: &mut Criterion) {
.filter(!component::<A>() & tag_value(&Tag(2.0)));

b.iter(|| {
for c in query.iter_chunks(&world) {
for c in query.iter_chunks(&mut world) {
unsafe {
c.components_mut::<Position>()
.unwrap()
Expand Down
38 changes: 19 additions & 19 deletions benches/parallel_query.rs
Original file line number Diff line number Diff line change
Expand Up @@ -103,40 +103,40 @@ fn ideal(ab: &mut Vec<(A, B)>, ac: &mut Vec<(A, C)>) {
}
}

fn sequential(world: &World) {
for (mut b, a) in <(Write<B>, Read<A>)>::query().iter(&world) {
fn sequential(world: &mut World) {
for (mut b, a) in <(Write<B>, Read<A>)>::query().iter(world) {
b.0 = a.0;
}

for (mut c, a) in <(Write<C>, Read<A>)>::query().iter(&world) {
for (mut c, a) in <(Write<C>, Read<A>)>::query().iter(world) {
c.0 = a.0;
}
}

fn parallel(world: &World) {
fn parallel(world: &mut World) {
join(
|| {
for (mut b, a) in <(Write<B>, Read<A>)>::query().iter(&world) {
|| unsafe {
for (mut b, a) in <(Write<B>, Read<A>)>::query().iter_unchecked(&world) {
b.0 = a.0;
}
},
|| {
for (mut c, a) in <(Write<C>, Read<A>)>::query().iter(&world) {
|| unsafe {
for (mut c, a) in <(Write<C>, Read<A>)>::query().iter_unchecked(&world) {
c.0 = a.0;
}
},
);
}

fn par_for_each(world: &World) {
fn par_for_each(world: &mut World) {
join(
|| {
<(Write<B>, Read<A>)>::query().par_for_each(&world, |(mut b, a)| {
|| unsafe {
<(Write<B>, Read<A>)>::query().par_for_each_unchecked(&world, |(mut b, a)| {
b.0 = a.0;
});
},
|| {
<(Write<C>, Read<A>)>::query().par_for_each(&world, |(mut c, a)| {
|| unsafe {
<(Write<C>, Read<A>)>::query().par_for_each_unchecked(&world, |(mut c, a)| {
c.0 = a.0;
});
},
Expand All @@ -157,18 +157,18 @@ fn bench_ordered(c: &mut Criterion) {
)
.with_function("sequential", |b, n| {
let data = data(*n);
let world = setup(&data);
b.iter(|| sequential(&world));
let mut world = setup(&data);
b.iter(|| sequential(&mut world));
})
.with_function("parallel", |b, n| {
let data = data(*n);
let world = setup(&data);
join(|| {}, || b.iter(|| parallel(&world)));
let mut world = setup(&data);
join(|| {}, || b.iter(|| parallel(&mut world)));
})
.with_function("par_for_each", |b, n| {
let data = data(*n);
let world = setup(&data);
join(|| {}, || b.iter(|| par_for_each(&world)));
let mut world = setup(&data);
join(|| {}, || b.iter(|| par_for_each(&mut world)));
}),
);
}
Expand Down
16 changes: 8 additions & 8 deletions benches/transform.rs
Original file line number Diff line number Diff line change
Expand Up @@ -58,27 +58,27 @@ fn ideal(data: &mut Vec<(Position, Orientation, Scale, Transform)>) {
}
}

fn sequential(world: &World) {
fn sequential(world: &mut World) {
for (pos, orient, scale, mut trans) in <(
Read<Position>,
Read<Orientation>,
Read<Scale>,
Write<Transform>,
)>::query()
.iter(&world)
.iter(world)
{
trans.0 = process(&pos.0, &orient.0, &scale.0);
}
}

fn par_for_each(world: &World) {
fn par_for_each(world: &mut World) {
<(
Read<Position>,
Read<Orientation>,
Read<Scale>,
Write<Transform>,
)>::query()
.par_for_each(&world, |(pos, orient, scale, mut trans)| {
.par_for_each(world, |(pos, orient, scale, mut trans)| {
trans.0 = process(&pos.0, &orient.0, &scale.0);
});
}
Expand All @@ -96,13 +96,13 @@ fn bench_transform(c: &mut Criterion) {
)
.with_function("sequential", |b, n| {
let data = data(*n);
let world = setup(data);
b.iter(|| sequential(&world));
let mut world = setup(data);
b.iter(|| sequential(&mut world));
})
.with_function("par_for_each", |b, n| {
let data = data(*n);
let world = setup(data);
join(|| {}, || b.iter(|| par_for_each(&world)));
let mut world = setup(data);
join(|| {}, || b.iter(|| par_for_each(&mut world)));
}),
);
}
Expand Down
6 changes: 3 additions & 3 deletions examples/hello_world.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ fn main() {

// update positions
let mut query = <(Write<Pos>, Read<Vel>)>::query();
for (mut pos, vel) in query.iter(&world) {
for (mut pos, vel) in query.iter(&mut world) {
pos.0 += vel.0;
pos.1 += vel.1;
pos.2 += vel.2;
Expand All @@ -33,7 +33,7 @@ fn main() {
let mut query = <(Write<Pos>, Read<Vel>)>::query();
#[cfg(feature = "par-iter")]
{
query.par_for_each(&world, |(mut pos, vel)| {
query.par_for_each(&mut world, |(mut pos, vel)| {
pos.0 += vel.0;
pos.1 += vel.1;
pos.2 += vel.2;
Expand All @@ -42,7 +42,7 @@ fn main() {

#[cfg(not(feature = "par-iter"))]
{
query.for_each(&world, |(mut pos, vel)| {
query.for_each(&mut world, |(mut pos, vel)| {
pos.0 += vel.0;
pos.1 += vel.1;
pos.2 += vel.2;
Expand Down
12 changes: 6 additions & 6 deletions readme.md
Original file line number Diff line number Diff line change
Expand Up @@ -63,7 +63,7 @@ let entities: &[Entity] = world.insert(
let query = <(Write<Position>, Read<Velocity>)>::query();

// Iterate through all entities that match the query in the world
for (mut pos, vel) in query.iter(&world) {
for (mut pos, vel) in query.iter(&mut world) {
pos.x += vel.dx;
pos.y += vel.dy;
}
Expand All @@ -83,7 +83,7 @@ Additional data type filters:
// It is possible to specify that entities must contain data beyond that being fetched
let query = Read::<Position>::query()
.filter(component::<Velocity>());
for position in query.iter(&world) {
for position in query.iter(&mut world) {
// these entities also have `Velocity`
}
```
Expand All @@ -94,7 +94,7 @@ Filter boolean operations:
// Filters can be combined with boolean operators
let query = Read::<Position>::query()
.filter(tag::<Static>() | !component::<Velocity>());
for position in query.iter(&world) {
for position in query.iter(&mut world) {
// these entities are also either marked as `Static`, or do *not* have a `Velocity`
}
```
Expand All @@ -105,7 +105,7 @@ Filter by shared data value:
// Filters can filter by specific shared data values
let query = Read::<Position>::query()
.filter(tag_value(&Model(3)));
for position in query.iter(&world) {
for position in query.iter(&mut world) {
// these entities all have shared data value `Model(3)`
}
```
Expand All @@ -117,7 +117,7 @@ Change detection:
// has not changed since the last time the query was iterated.
let query = <(Read<Position>, Shared<Model>)>::query()
.filter(changed::<Position>());
for (pos, model) in query.iter(&world) {
for (pos, model) in query.iter(&mut world) {
// entities who have changed position
}
```
Expand Down Expand Up @@ -151,7 +151,7 @@ fn render_instanced(model: &Model, transforms: &[Transform]) {
let query = Read::<Transform>::query()
.filter(tag::<Model>());

for chunk in query.iter_chunks(&world) {
for chunk in query.iter_chunks(&mut world) {
// get the chunk's model
let model: &Model = chunk.tag().unwrap();

Expand Down
Loading