Skip to content

Commit f24600f

Browse files
committed
Improvement on Field, Iter, observerBuilder and SystemBuilder
Field : resolve the map seach of component in the constructor Iter : cached count builders : reuse of iter
1 parent 42acc43 commit f24600f

4 files changed

Lines changed: 46 additions & 19 deletions

File tree

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

Lines changed: 5 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -5,14 +5,12 @@
55
public class Field<T> {
66
private final MemorySegment memorySegment;
77
private final int count;
8-
private final Flecs world;
9-
private final Class<T> componentClass;
8+
private final Component<T> component;
109

1110
Field(MemorySegment memorySegment, int count, Flecs world, Class<T> componentClass) {
1211
this.memorySegment = memorySegment;
1312
this.count = count;
14-
this.world = world;
15-
this.componentClass = componentClass;
13+
this.component = world.componentRegistry().getComponent(componentClass);
1614
}
1715

1816
public boolean isSet() {
@@ -31,11 +29,9 @@ public T get(int i) {
3129
throw new IndexOutOfBoundsException("Index " + i + " out of bounds for count " + this.count);
3230
}
3331

34-
Component<T> component = this.world.componentRegistry().getComponent(this.componentClass);
32+
long elementOffset = i * this.component.size();
33+
MemorySegment elementSegment = this.memorySegment.asSlice(elementOffset, this.component.size());
3534

36-
long elementOffset = i * component.size();
37-
MemorySegment elementSegment = this.memorySegment.asSlice(elementOffset, component.size());
38-
39-
return component.read(elementSegment);
35+
return this.component.read(elementSegment);
4036
}
4137
}

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

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,20 +4,31 @@
44

55
public class Iter {
66

7-
private final MemorySegment nativeIter;
7+
private MemorySegment nativeIter;
88
private final Flecs world;
9+
private int count;
910

1011
Iter(MemorySegment nativeIter, Flecs world) {
1112
this.nativeIter = nativeIter;
1213
this.world = world;
14+
this.count = -1;
15+
}
16+
17+
void setNativeIter(MemorySegment nativeIter) {
18+
this.nativeIter = nativeIter;
19+
this.count = -1;
1320
}
1421

1522
public boolean next() {
23+
this.count = -1;
1624
return flecs_h.ecs_iter_next(this.nativeIter);
1725
}
1826

1927
public int count() {
20-
return ecs_iter_t.count(this.nativeIter);
28+
if (this.count < 0) {
29+
this.count = ecs_iter_t.count(this.nativeIter);
30+
}
31+
return this.count;
2132
}
2233

2334
public long entityId(int index) {

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -159,9 +159,14 @@ public ObserverBuilder observerFlags(int flags) {
159159
public FlecsObserver iter(Query.IterCallback callback) {
160160
this.iterCallback = callback;
161161

162+
final Iter[] iterHolder = new Iter[1];
162163
MemorySegment callbackStub = ecs_iter_action_t.allocate(it -> {
163-
Iter iter = new Iter(it, this.world);
164-
callback.accept(iter);
164+
if (iterHolder[0] == null) {
165+
iterHolder[0] = new Iter(it, this.world);
166+
} else {
167+
iterHolder[0].setNativeIter(it);
168+
}
169+
callback.accept(iterHolder[0]);
165170
}, this.world.arena());
166171

167172
ecs_observer_desc_t.callback(this.desc, callbackStub);
@@ -172,9 +177,14 @@ public FlecsObserver iter(Query.IterCallback callback) {
172177
public FlecsObserver run(Query.RunCallback callback) {
173178
this.runCallback = callback;
174179

180+
final Iter[] iterHolder = new Iter[1];
175181
MemorySegment callbackStub = ecs_run_action_t.allocate(it -> {
176-
Iter iter = new Iter(it, this.world);
177-
callback.accept(iter);
182+
if (iterHolder[0] == null) {
183+
iterHolder[0] = new Iter(it, this.world);
184+
} else {
185+
iterHolder[0].setNativeIter(it);
186+
}
187+
callback.accept(iterHolder[0]);
178188
}, this.world.arena());
179189

180190
ecs_observer_desc_t.run(this.desc, callbackStub);

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

Lines changed: 14 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -187,9 +187,14 @@ public <T> SystemBuilder read(Class<T> componentClass) {
187187
public FlecsSystem iter(Query.IterCallback callback) {
188188
this.iterCallback = callback;
189189

190+
final Iter[] iterHolder = new Iter[1];
190191
MemorySegment callbackStub = ecs_iter_action_t.allocate(it -> {
191-
Iter iter = new Iter(it, this.world);
192-
callback.accept(iter);
192+
if (iterHolder[0] == null) {
193+
iterHolder[0] = new Iter(it, this.world);
194+
} else {
195+
iterHolder[0].setNativeIter(it);
196+
}
197+
callback.accept(iterHolder[0]);
193198
}, this.world.arena());
194199

195200
ecs_system_desc_t.callback(this.desc, callbackStub);
@@ -200,9 +205,14 @@ public FlecsSystem iter(Query.IterCallback callback) {
200205
public FlecsSystem run(Query.RunCallback callback) {
201206
this.runCallback = callback;
202207

208+
final Iter[] iterHolder = new Iter[1];
203209
MemorySegment callbackStub = ecs_run_action_t.allocate(it -> {
204-
Iter iter = new Iter(it, this.world);
205-
callback.accept(iter);
210+
if (iterHolder[0] == null) {
211+
iterHolder[0] = new Iter(it, this.world);
212+
} else {
213+
iterHolder[0].setNativeIter(it);
214+
}
215+
callback.accept(iterHolder[0]);
206216
}, this.world.arena());
207217

208218
ecs_system_desc_t.run(this.desc, callbackStub);

0 commit comments

Comments
 (0)