Skip to content

Commit 891a4f0

Browse files
committed
improve code clarity
1 parent 45d3225 commit 891a4f0

3 files changed

Lines changed: 46 additions & 34 deletions

File tree

distr/flecs.c

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -80001,13 +80001,13 @@ int flecs_query_insert_toggle(
8000180001
* set, separate instructions let the query engine backtrack to get
8000280002
* the right results. */
8000380003
if (optional_toggles) {
80004-
ecs_flags64_t optional_done = 0;
80004+
ecs_flags64_t optional_fields_processed = 0;
8000580005
for (j = i; j < term_count; j ++) {
8000680006
uint64_t field_bit = 1ull << terms[j].field_index;
8000780007
if (!(optional_toggles & field_bit)) {
8000880008
continue;
8000980009
}
80010-
if (optional_done & field_bit) {
80010+
if (optional_fields_processed & field_bit) {
8001180011
continue;
8001280012
}
8001380013

@@ -80018,7 +80018,7 @@ int flecs_query_insert_toggle(
8001880018
op.flags = cur.flags;
8001980019
flecs_query_op_insert(&op, ctx);
8002080020

80021-
optional_done |= field_bit;
80021+
optional_fields_processed |= field_bit;
8002280022
}
8002380023
}
8002480024
}
@@ -85202,7 +85202,7 @@ static inline int32_t flecs_ctz64(uint64_t v) {
8520285202
static
8520385203
flecs_query_row_mask_t flecs_query_get_row_mask(
8520485204
ecs_iter_t *it,
85205-
const ecs_query_t *q,
85205+
const ecs_query_t *query,
8520685206
ecs_table_t *table,
8520785207
int32_t block_index,
8520885208
ecs_flags64_t and_fields,
@@ -85229,43 +85229,50 @@ flecs_query_row_mask_t flecs_query_get_row_mask(
8522985229
}
8523085230

8523185231
ecs_term_t *field_term = NULL;
85232-
int32_t ti;
85233-
for (ti = 0; ti < q->term_count; ti ++) {
85234-
if (q->terms[ti].field_index == i) {
85235-
field_term = &q->terms[ti];
85232+
int32_t term_idx;
85233+
for (term_idx = 0; term_idx < query->term_count; term_idx ++) {
85234+
if (query->terms[term_idx].field_index == i) {
85235+
field_term = &query->terms[term_idx];
8523685236
break;
8523785237
}
8523885238
}
8523985239

8524085240
bool is_or = false;
8524185241
if (field_term) {
8524285242
is_or = field_term->oper == EcsOr ||
85243-
((field_term != q->terms) && (field_term[-1].oper == EcsOr));
85243+
((field_term != query->terms) && (field_term[-1].oper == EcsOr));
8524485244
}
8524585245

8524685246
if (is_or) {
85247-
int32_t start = flecs_itoi32(field_term - q->terms);
85247+
int32_t start = flecs_itoi32(field_term - query->terms);
8524885248
int32_t end = start;
8524985249

85250-
while (start && q->terms[start - 1].oper == EcsOr) {
85250+
/* Find start and end of OR chain. The oper field is stored on the
85251+
* term preceding the operand, so:
85252+
* - start loop checks if previous term connects via OR
85253+
* - end loop checks if current term connects to next via OR */
85254+
while (start && query->terms[start - 1].oper == EcsOr) {
8525185255
start --;
8525285256
}
85253-
while (end < (q->term_count - 1) && q->terms[end].oper == EcsOr) {
85257+
while (end < (query->term_count - 1) && query->terms[end].oper == EcsOr) {
8525485258
end ++;
8525585259
}
8525685260

8525785261
ecs_flags64_t block = 0;
8525885262
bool chain_has_bitset = false;
8525985263

85260-
int32_t j;
85261-
for (j = start; j <= end; j ++) {
85262-
ecs_id_t id = q->terms[j].id;
85264+
int32_t or_chain_term_idx;
85265+
for (or_chain_term_idx = start; or_chain_term_idx <= end; or_chain_term_idx ++) {
85266+
ecs_id_t id = query->terms[or_chain_term_idx].id;
8526385267
ecs_bitset_t *bs = flecs_table_get_toggle(table, id);
8526485268
if (bs) {
8526585269
ecs_assert((64 * block_index) < bs->size, ECS_INTERNAL_ERROR, NULL);
8526685270
block |= bs->data[block_index];
8526785271
chain_has_bitset = true;
85268-
} else if (ecs_table_has_id(q->world, table, id)) {
85272+
} else if (ecs_table_has_id(query->world, table, id)) {
85273+
/* If a non-toggle component is present, it is always enabled
85274+
* for all entities in the table. Since this is an OR chain,
85275+
* the entire expression becomes true for this block. */
8526985276
block = UINT64_MAX;
8527085277
break;
8527185278
}
@@ -85566,7 +85573,6 @@ repeat: {}
8556685573
return result;
8556785574
}
8556885575

85569-
8557085576
/**
8557185577
* @file query/engine/eval_trav.c
8557285578
* @brief Transitive/reflexive relationship traversal.

src/query/compiler/compiler.c

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -811,13 +811,13 @@ int flecs_query_insert_toggle(
811811
* set, separate instructions let the query engine backtrack to get
812812
* the right results. */
813813
if (optional_toggles) {
814-
ecs_flags64_t optional_done = 0;
814+
ecs_flags64_t optional_fields_processed = 0;
815815
for (j = i; j < term_count; j ++) {
816816
uint64_t field_bit = 1ull << terms[j].field_index;
817817
if (!(optional_toggles & field_bit)) {
818818
continue;
819819
}
820-
if (optional_done & field_bit) {
820+
if (optional_fields_processed & field_bit) {
821821
continue;
822822
}
823823

@@ -828,7 +828,7 @@ int flecs_query_insert_toggle(
828828
op.flags = cur.flags;
829829
flecs_query_op_insert(&op, ctx);
830830

831-
optional_done |= field_bit;
831+
optional_fields_processed |= field_bit;
832832
}
833833
}
834834
}

src/query/engine/eval_toggle.c

Lines changed: 20 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ static inline int32_t flecs_ctz64(uint64_t v) {
3131
static
3232
flecs_query_row_mask_t flecs_query_get_row_mask(
3333
ecs_iter_t *it,
34-
const ecs_query_t *q,
34+
const ecs_query_t *query,
3535
ecs_table_t *table,
3636
int32_t block_index,
3737
ecs_flags64_t and_fields,
@@ -58,43 +58,50 @@ flecs_query_row_mask_t flecs_query_get_row_mask(
5858
}
5959

6060
ecs_term_t *field_term = NULL;
61-
int32_t ti;
62-
for (ti = 0; ti < q->term_count; ti ++) {
63-
if (q->terms[ti].field_index == i) {
64-
field_term = &q->terms[ti];
61+
int32_t term_idx;
62+
for (term_idx = 0; term_idx < query->term_count; term_idx ++) {
63+
if (query->terms[term_idx].field_index == i) {
64+
field_term = &query->terms[term_idx];
6565
break;
6666
}
6767
}
6868

6969
bool is_or = false;
7070
if (field_term) {
7171
is_or = field_term->oper == EcsOr ||
72-
((field_term != q->terms) && (field_term[-1].oper == EcsOr));
72+
((field_term != query->terms) && (field_term[-1].oper == EcsOr));
7373
}
7474

7575
if (is_or) {
76-
int32_t start = flecs_itoi32(field_term - q->terms);
76+
int32_t start = flecs_itoi32(field_term - query->terms);
7777
int32_t end = start;
7878

79-
while (start && q->terms[start - 1].oper == EcsOr) {
79+
/* Find start and end of OR chain. The oper field is stored on the
80+
* term preceding the operand, so:
81+
* - start loop checks if previous term connects via OR
82+
* - end loop checks if current term connects to next via OR */
83+
while (start && query->terms[start - 1].oper == EcsOr) {
8084
start --;
8185
}
82-
while (end < (q->term_count - 1) && q->terms[end].oper == EcsOr) {
86+
while (end < (query->term_count - 1) && query->terms[end].oper == EcsOr) {
8387
end ++;
8488
}
8589

8690
ecs_flags64_t block = 0;
8791
bool chain_has_bitset = false;
8892

89-
int32_t j;
90-
for (j = start; j <= end; j ++) {
91-
ecs_id_t id = q->terms[j].id;
93+
int32_t or_chain_term_idx;
94+
for (or_chain_term_idx = start; or_chain_term_idx <= end; or_chain_term_idx ++) {
95+
ecs_id_t id = query->terms[or_chain_term_idx].id;
9296
ecs_bitset_t *bs = flecs_table_get_toggle(table, id);
9397
if (bs) {
9498
ecs_assert((64 * block_index) < bs->size, ECS_INTERNAL_ERROR, NULL);
9599
block |= bs->data[block_index];
96100
chain_has_bitset = true;
97-
} else if (ecs_table_has_id(q->world, table, id)) {
101+
} else if (ecs_table_has_id(query->world, table, id)) {
102+
/* If a non-toggle component is present, it is always enabled
103+
* for all entities in the table. Since this is an OR chain,
104+
* the entire expression becomes true for this block. */
98105
block = UINT64_MAX;
99106
break;
100107
}
@@ -394,4 +401,3 @@ repeat: {}
394401

395402
return result;
396403
}
397-

0 commit comments

Comments
 (0)