@@ -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) {
8520285202static
8520385203flecs_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.
0 commit comments