Skip to content

Commit 1283604

Browse files
committed
refactor perf Iter : cache component size + resolve count() in the native iter setter and next method
1 parent fdd5d11 commit 1283604

3 files changed

Lines changed: 20 additions & 30 deletions

File tree

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

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,10 @@ public class Field<T> {
1717
this.componentView = world.viewCache().getComponentView(componentClass);
1818
}
1919

20+
long componentSize() {
21+
return this.componentSize;
22+
}
23+
2024
public void reset(MemorySegment memorySegment, int count) {
2125
this.memorySegment = memorySegment;
2226
this.count = count;

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

Lines changed: 15 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -19,29 +19,26 @@ public class Iter {
1919

2020
void setNativeIter(MemorySegment nativeIter) {
2121
this.nativeIter = nativeIter;
22-
this.count = -1;
22+
this.count = ecs_iter_t.count(nativeIter);
2323
}
2424

2525
World world() {
2626
return this.world;
2727
}
2828

2929
public boolean next() {
30-
this.count = -1;
31-
return flecs_h.ecs_iter_next(this.nativeIter);
30+
boolean hasNext = flecs_h.ecs_iter_next(this.nativeIter);
31+
this.count = hasNext ? ecs_iter_t.count(this.nativeIter) : 0;
32+
return hasNext;
3233
}
3334

3435
public int count() {
35-
if (this.count < 0) {
36-
this.count = ecs_iter_t.count(this.nativeIter);
37-
}
3836
return this.count;
3937
}
4038

4139
public long entityId(int index) {
42-
if (index < 0 || index >= this.count()) {
43-
throw new IndexOutOfBoundsException("Index " + index + " out of bounds for count " + this.count());
44-
}
40+
assert (index >= 0 && index < 32) : "The index must be between 0 and 31.";
41+
4542
MemorySegment entities = ecs_iter_t.entities(this.nativeIter);
4643
if (entities == null || entities.address() == 0) {
4744
throw new IllegalStateException("Entities array is null");
@@ -63,41 +60,32 @@ public float deltaSystemTime() {
6360

6461
@SuppressWarnings("unchecked")
6562
public <T> Field<T> field(Class<T> componentClass, int index) {
66-
if (index < 0 || index > 127) {
67-
throw new IndexOutOfBoundsException("The field index must be between 0 and 127.");
68-
}
69-
70-
byte flecsIndex = (byte) index;
71-
Component<T> component = this.world.componentRegistry().getComponent(componentClass);
72-
MemorySegment columnPtr = flecs_h.ecs_field_w_size(this.nativeIter, component.size(), flecsIndex);
73-
74-
if (columnPtr == null || columnPtr.address() == 0) {
75-
return null;
76-
}
63+
assert (index >= 0 && index < 32) : "The field index must be between 0 and 31.";
7764

7865
Field<T> field = (Field<T>) this.fields[index];
66+
7967
if (field == null) {
68+
Component<T> component = this.world.componentRegistry().getComponent(componentClass);
69+
MemorySegment columnPtr = flecs_h.ecs_field_w_size(this.nativeIter, component.size(), (byte) index);
8070
field = new Field<>(columnPtr, this.count(), this.world, componentClass);
8171
this.fields[index] = field;
8272
} else {
73+
MemorySegment columnPtr = flecs_h.ecs_field_w_size(this.nativeIter, field.componentSize(), (byte) index);
8374
field.reset(columnPtr, this.count());
8475
}
76+
8577
return field;
8678
}
8779

8880
public boolean isFieldSet(int index) {
89-
if (index < 0 || index > 31) {
90-
throw new IndexOutOfBoundsException("The field index must be between 0 and 31.");
91-
}
81+
assert (index >= 0 && index < 32) : "The index must be between 0 and 31.";
9282

9383
int setFields = ecs_iter_t.set_fields(this.nativeIter);
9484
return (setFields & (1 << index)) != 0;
9585
}
9686

9787
public long termId(int index) {
98-
if (index < 0 || index > 127) {
99-
throw new IndexOutOfBoundsException("The term index must be between 0 and 127.");
100-
}
88+
assert (index >= 0 && index < 32) : "The index must be between 0 and 31.";
10189

10290
MemorySegment ids = ecs_iter_t.ids(this.nativeIter);
10391
if (ids == null || ids.address() == 0) {
@@ -108,9 +96,7 @@ public long termId(int index) {
10896
}
10997

11098
public int fieldSize(int index) {
111-
if (index < 0 || index > 127) {
112-
throw new IndexOutOfBoundsException("The field index must be between 0 and 127.");
113-
}
99+
assert (index >= 0 && index < 32) : "The index must be between 0 and 31.";
114100

115101
MemorySegment sizes = ecs_iter_t.sizes(this.nativeIter);
116102
if (sizes == null || sizes.address() == 0) {

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

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -64,9 +64,9 @@ public void iter(IterCallback callback) {
6464
throw new IllegalStateException("ecs_query_iter returned a null iterator");
6565
}
6666

67+
this.world.viewCache().resetCursors();
6768
while (flecs_h.ecs_iter_next(iterSegment)) {
6869
this.iter.setNativeIter(iterSegment);
69-
this.world.viewCache().resetCursors();
7070
callback.accept(this.iter);
7171
}
7272
}

0 commit comments

Comments
 (0)