Skip to content

Commit c328f16

Browse files
committed
feat : field with view component
1 parent 9876255 commit c328f16

5 files changed

Lines changed: 59 additions & 301 deletions

File tree

examples/src/main/java/com/github/elebras1/flecs/examples/DirectFieldAccessExample.java

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

examples/src/main/java/com/github/elebras1/flecs/examples/SimpleQueryBenchmark.java

Lines changed: 40 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -42,10 +42,16 @@ public static void main(String[] args) {
4242
System.out.println("--- Benchmark 1: each() + entity.get(Component.class) ---");
4343
runBenchmark("each+get", () -> benchmarkEachGet(world, query));
4444

45-
System.out.println("--- Benchmark 2: iter() + Field<T>.get(i) ---");
46-
runBenchmark("iter+Field", () -> benchmarkIterField(query));
45+
System.out.println("--- Benchmark 2: each() + entity.getMutView(Component.class) ---");
46+
runBenchmark("each+getMutView", () -> benchmarkEachGetMutView(world, query));
4747

48-
System.out.println("--- Benchmark 3: iter() + fieldFloat() (native) ---");
48+
System.out.println("--- Benchmark 3: iter() + Field<T>.get(i) ---");
49+
runBenchmark("iter+Field+get", () -> benchmarkIterFieldGet(query));
50+
51+
System.out.println("--- Benchmark 4: iter() + Field<T>.getMutView(i) ---");
52+
runBenchmark("iter+Field+getMutView", () -> benchmarkIterFieldGetMutView(query));
53+
54+
System.out.println("--- Benchmark 5: iter() + fieldFloat() (native) ---");
4955
runBenchmark("iter+native", () -> benchmarkIterNative(query));
5056

5157
System.out.println();
@@ -67,7 +73,20 @@ private static float benchmarkEachGet(World world, Query query) {
6773
return sum[0];
6874
}
6975

70-
private static float benchmarkIterField(Query query) {
76+
private static float benchmarkEachGetMutView(World world, Query query) {
77+
final float[] sum = {0.0f};
78+
79+
query.each(entityId -> {
80+
Entity entity = world.obtainEntity(entityId);
81+
PositionView pos = entity.getMutView(Position.class);
82+
VelocityView vel = entity.getMutView(Velocity.class);
83+
sum[0] += pos.x() + pos.y() + vel.dx() + vel.dy();
84+
});
85+
86+
return sum[0];
87+
}
88+
89+
private static float benchmarkIterFieldGet(Query query) {
7190
final float[] sum = {0.0f};
7291

7392
query.iter(iter -> {
@@ -84,6 +103,23 @@ private static float benchmarkIterField(Query query) {
84103
return sum[0];
85104
}
86105

106+
private static float benchmarkIterFieldGetMutView(Query query) {
107+
final float[] sum = {0.0f};
108+
109+
query.iter(iter -> {
110+
Field<Position> positions = iter.field(Position.class, 0);
111+
Field<Velocity> velocities = iter.field(Velocity.class, 1);
112+
113+
for (int i = 0; i < iter.count(); i++) {
114+
PositionView pos = positions.getMutView(i);
115+
VelocityView vel = velocities.getMutView(i);
116+
sum[0] += pos.x() + pos.y() + vel.dx() + vel.dy();
117+
}
118+
});
119+
120+
return sum[0];
121+
}
122+
87123
private static float benchmarkIterNative(Query query) {
88124
final float[] sum = {0.0f};
89125

src/main/java/com/github/elebras1/flecs/Entity.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -200,7 +200,7 @@ public <T> T get(Class<T> componentClass, long target) {
200200
}
201201

202202
@SuppressWarnings("unchecked")
203-
public <T> T getMutView(Class<?> componentClass) {
203+
public <T extends ComponentView> T getMutView(Class<?> componentClass) {
204204
ComponentView view = FlecsContext.CURRENT_CACHE.get().get(componentClass);
205205

206206
long componentId = this.world.componentRegistry().getComponentId(componentClass);

src/main/java/com/github/elebras1/flecs/Field.java

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,11 +7,13 @@ public class Field<T> {
77
private final MemorySegment memorySegment;
88
private final int count;
99
private final Component<T> component;
10+
private final ComponentView componentView;
1011

1112
Field(MemorySegment memorySegment, int count, World world, Class<T> componentClass) {
1213
this.memorySegment = memorySegment;
1314
this.count = count;
1415
this.component = world.componentRegistry().getComponent(componentClass);
16+
this.componentView = FlecsContext.CURRENT_CACHE.get().get(componentClass);
1517
}
1618

1719
public boolean isSet() {
@@ -36,6 +38,22 @@ public T get(int i) {
3638
return this.component.read(elementSegment);
3739
}
3840

41+
@SuppressWarnings("unchecked")
42+
public <V extends ComponentView> V getMutView(int i) {
43+
if (!this.isSet()) {
44+
throw new IllegalStateException("Field is not set.");
45+
}
46+
if (i < 0 || i >= this.count) {
47+
throw new IndexOutOfBoundsException("Index " + i + " out of bounds for count " + this.count);
48+
}
49+
50+
long elementOffset = i * this.component.size();
51+
MemorySegment elementSegment = this.memorySegment.asSlice(elementOffset, this.component.size());
52+
this.componentView.setMemorySegment(elementSegment);
53+
54+
return (V) this.componentView;
55+
}
56+
3957
public void set(int i, T componentData) {
4058
if (!this.isSet()) {
4159
throw new IllegalStateException("Field is not set.");

0 commit comments

Comments
 (0)