Skip to content

Commit d00804d

Browse files
committed
[queries] Resolve top-level up target without the traversal cache map
1 parent 0e1037f commit d00804d

3 files changed

Lines changed: 90 additions & 24 deletions

File tree

distr/flecs.c

Lines changed: 45 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1485,6 +1485,7 @@ typedef enum {
14851485
typedef struct {
14861486
ecs_map_t src; /* map<table_id, trav_up_t> (up direction) */
14871487
ecs_trav_down_t down; /* reused buffer (down direction) */
1488+
ecs_trav_up_t up; /* reused buffer for top-level up result */
14881489
ecs_id_t with;
14891490
ecs_trav_direction_t dir;
14901491
} ecs_trav_up_cache_t;
@@ -90156,13 +90157,21 @@ ecs_trav_up_t* flecs_trav_table_up(
9015690157
ecs_id_t with,
9015790158
ecs_id_t rel,
9015890159
ecs_component_record_t *cr_with,
90159-
ecs_component_record_t *cr_trav)
90160-
{
90161-
ecs_trav_up_t *up = flecs_trav_up_ensure(ctx, cache, src);
90162-
if (up->ready) {
90163-
return up;
90164-
}
90160+
ecs_component_record_t *cr_trav);
9016590161

90162+
static
90163+
void flecs_trav_table_up_w(
90164+
const ecs_query_run_ctx_t *ctx,
90165+
ecs_allocator_t *a,
90166+
ecs_trav_up_cache_t *cache,
90167+
const ecs_world_t *world,
90168+
ecs_entity_t src,
90169+
ecs_id_t with,
90170+
ecs_id_t rel,
90171+
ecs_component_record_t *cr_with,
90172+
ecs_component_record_t *cr_trav,
90173+
ecs_trav_up_t *up)
90174+
{
9016690175
ecs_record_t *src_record = flecs_entities_get_any(world, src);
9016790176
ecs_table_t *table = src_record->table;
9016890177
if (!table) {
@@ -90277,6 +90286,28 @@ ecs_trav_up_t* flecs_trav_table_up(
9027790286
not_found:
9027890287
up->tr = NULL;
9027990288
found:
90289+
return;
90290+
}
90291+
90292+
static
90293+
ecs_trav_up_t* flecs_trav_table_up(
90294+
const ecs_query_run_ctx_t *ctx,
90295+
ecs_allocator_t *a,
90296+
ecs_trav_up_cache_t *cache,
90297+
const ecs_world_t *world,
90298+
ecs_entity_t src,
90299+
ecs_id_t with,
90300+
ecs_id_t rel,
90301+
ecs_component_record_t *cr_with,
90302+
ecs_component_record_t *cr_trav)
90303+
{
90304+
ecs_trav_up_t *up = flecs_trav_up_ensure(ctx, cache, src);
90305+
if (up->ready) {
90306+
return up;
90307+
}
90308+
90309+
flecs_trav_table_up_w(ctx, a, cache, world, src, with, rel, cr_with,
90310+
cr_trav, up);
9028090311
up->ready = true;
9028190312
return up;
9028290313
}
@@ -90318,9 +90349,10 @@ ecs_trav_up_t* flecs_query_get_up_cache(
9031890349
ecs_assert(p != NULL, ECS_INTERNAL_ERROR, NULL);
9031990350

9032090351
ecs_entity_t tgt = (uint32_t)p->value;
90321-
ecs_trav_up_t *result = flecs_trav_table_up(ctx, a, cache, world, tgt,
90322-
with, ecs_pair(trav, EcsWildcard), cr_with, cr_trav);
90323-
ecs_assert(result != NULL, ECS_INTERNAL_ERROR, NULL);
90352+
ecs_trav_up_t *result = &cache->up;
90353+
*result = (ecs_trav_up_t){0};
90354+
flecs_trav_table_up_w(ctx, a, cache, world, tgt,
90355+
with, ecs_pair(trav, EcsWildcard), cr_with, cr_trav, result);
9032490356
if (result->src != 0) {
9032590357
return result;
9032690358
}
@@ -90338,9 +90370,10 @@ ecs_trav_up_t* flecs_query_get_up_cache(
9033890370
for (; i < end; i ++) {
9033990371
ecs_id_t id = table->type.array[i];
9034090372
ecs_entity_t tgt = ECS_PAIR_SECOND(id);
90341-
ecs_trav_up_t *result = flecs_trav_table_up(ctx, a, cache, world, tgt,
90342-
with, ecs_pair(trav, EcsWildcard), cr_with, cr_trav);
90343-
ecs_assert(result != NULL, ECS_INTERNAL_ERROR, NULL);
90373+
ecs_trav_up_t *result = &cache->up;
90374+
*result = (ecs_trav_up_t){0};
90375+
flecs_trav_table_up_w(ctx, a, cache, world, tgt,
90376+
with, ecs_pair(trav, EcsWildcard), cr_with, cr_trav, result);
9034490377
if (result->src != 0) {
9034590378
return result;
9034690379
}

src/query/engine/trav_up_cache.c

Lines changed: 44 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -70,13 +70,21 @@ ecs_trav_up_t* flecs_trav_table_up(
7070
ecs_id_t with,
7171
ecs_id_t rel,
7272
ecs_component_record_t *cr_with,
73-
ecs_component_record_t *cr_trav)
74-
{
75-
ecs_trav_up_t *up = flecs_trav_up_ensure(ctx, cache, src);
76-
if (up->ready) {
77-
return up;
78-
}
73+
ecs_component_record_t *cr_trav);
7974

75+
static
76+
void flecs_trav_table_up_w(
77+
const ecs_query_run_ctx_t *ctx,
78+
ecs_allocator_t *a,
79+
ecs_trav_up_cache_t *cache,
80+
const ecs_world_t *world,
81+
ecs_entity_t src,
82+
ecs_id_t with,
83+
ecs_id_t rel,
84+
ecs_component_record_t *cr_with,
85+
ecs_component_record_t *cr_trav,
86+
ecs_trav_up_t *up)
87+
{
8088
ecs_record_t *src_record = flecs_entities_get_any(world, src);
8189
ecs_table_t *table = src_record->table;
8290
if (!table) {
@@ -191,6 +199,28 @@ ecs_trav_up_t* flecs_trav_table_up(
191199
not_found:
192200
up->tr = NULL;
193201
found:
202+
return;
203+
}
204+
205+
static
206+
ecs_trav_up_t* flecs_trav_table_up(
207+
const ecs_query_run_ctx_t *ctx,
208+
ecs_allocator_t *a,
209+
ecs_trav_up_cache_t *cache,
210+
const ecs_world_t *world,
211+
ecs_entity_t src,
212+
ecs_id_t with,
213+
ecs_id_t rel,
214+
ecs_component_record_t *cr_with,
215+
ecs_component_record_t *cr_trav)
216+
{
217+
ecs_trav_up_t *up = flecs_trav_up_ensure(ctx, cache, src);
218+
if (up->ready) {
219+
return up;
220+
}
221+
222+
flecs_trav_table_up_w(ctx, a, cache, world, src, with, rel, cr_with,
223+
cr_trav, up);
194224
up->ready = true;
195225
return up;
196226
}
@@ -232,9 +262,10 @@ ecs_trav_up_t* flecs_query_get_up_cache(
232262
ecs_assert(p != NULL, ECS_INTERNAL_ERROR, NULL);
233263

234264
ecs_entity_t tgt = (uint32_t)p->value;
235-
ecs_trav_up_t *result = flecs_trav_table_up(ctx, a, cache, world, tgt,
236-
with, ecs_pair(trav, EcsWildcard), cr_with, cr_trav);
237-
ecs_assert(result != NULL, ECS_INTERNAL_ERROR, NULL);
265+
ecs_trav_up_t *result = &cache->up;
266+
*result = (ecs_trav_up_t){0};
267+
flecs_trav_table_up_w(ctx, a, cache, world, tgt,
268+
with, ecs_pair(trav, EcsWildcard), cr_with, cr_trav, result);
238269
if (result->src != 0) {
239270
return result;
240271
}
@@ -252,9 +283,10 @@ ecs_trav_up_t* flecs_query_get_up_cache(
252283
for (; i < end; i ++) {
253284
ecs_id_t id = table->type.array[i];
254285
ecs_entity_t tgt = ECS_PAIR_SECOND(id);
255-
ecs_trav_up_t *result = flecs_trav_table_up(ctx, a, cache, world, tgt,
256-
with, ecs_pair(trav, EcsWildcard), cr_with, cr_trav);
257-
ecs_assert(result != NULL, ECS_INTERNAL_ERROR, NULL);
286+
ecs_trav_up_t *result = &cache->up;
287+
*result = (ecs_trav_up_t){0};
288+
flecs_trav_table_up_w(ctx, a, cache, world, tgt,
289+
with, ecs_pair(trav, EcsWildcard), cr_with, cr_trav, result);
258290
if (result->src != 0) {
259291
return result;
260292
}

src/query/types.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -223,6 +223,7 @@ typedef enum {
223223
typedef struct {
224224
ecs_map_t src; /* map<table_id, trav_up_t> (up direction) */
225225
ecs_trav_down_t down; /* reused buffer (down direction) */
226+
ecs_trav_up_t up; /* reused buffer for top-level up result */
226227
ecs_id_t with;
227228
ecs_trav_direction_t dir;
228229
} ecs_trav_up_cache_t;

0 commit comments

Comments
 (0)