Skip to content

Commit fc7e0f0

Browse files
committed
Single linear pass instead of per-block term lookup. Make var.s const where possible.
1 parent 990da47 commit fc7e0f0

2 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
@@ -85204,22 +85204,14 @@ bool flecs_query_apply_or_mask(
8520485204
const ecs_query_t *query,
8520585205
ecs_table_t *table,
8520685206
int32_t block_index,
85207-
int32_t field_index,
85207+
int32_t term_index,
8520885208
ecs_flags64_t *mask,
8520985209
bool *has_bitset)
8521085210
{
85211-
/* Find first term for field index. Terms in an OR chain share the
85212-
* same field index, so this finds the start of the chain. */
85213-
int32_t i = field_index, term_count = query->term_count;
8521485211
ecs_term_t *terms = query->terms;
85212+
int32_t i = term_index;
8521585213

85216-
for (; i < term_count; i ++) {
85217-
if (terms[i].field_index == field_index) {
85218-
break;
85219-
}
85220-
}
85221-
85222-
if (i == term_count || terms[i].oper != EcsOr) {
85214+
if (terms[i].oper != EcsOr) {
8522385215
return false;
8522485216
}
8522585217

@@ -85260,12 +85252,24 @@ flecs_query_row_mask_t flecs_query_get_row_mask(
8526085252
ecs_query_toggle_ctx_t *op_ctx)
8526185253
{
8526285254
ecs_flags64_t mask = UINT64_MAX;
85263-
int32_t i, field_count = it->field_count;
85264-
ecs_flags64_t fields = and_fields | not_fields;
85255+
int32_t i;
85256+
const ecs_flags64_t fields = and_fields | not_fields;
85257+
int32_t term_index = 0;
85258+
const int32_t term_count = query->term_count;
85259+
const ecs_term_t *terms = query->terms;
8526585260
bool has_bitset = false;
8526685261

85267-
for (i = 0; i < field_count; i ++) {
85268-
uint64_t field_bit = 1llu << i;
85262+
for (i = 0; i < it->field_count; i ++) {
85263+
const uint64_t field_bit = 1llu << i;
85264+
85265+
ecs_assert(term_index < term_count, ECS_INTERNAL_ERROR, NULL);
85266+
ecs_assert(terms[term_index].field_index == i, ECS_INTERNAL_ERROR, NULL);
85267+
85268+
const int32_t field_term_index = term_index;
85269+
do {
85270+
term_index ++;
85271+
} while ((term_index < term_count) && (terms[term_index].field_index == i));
85272+
8526985273
if (!(fields & field_bit)) {
8527085274
continue;
8527185275
}
@@ -85278,7 +85282,9 @@ flecs_query_row_mask_t flecs_query_get_row_mask(
8527885282
ecs_abort(ECS_INTERNAL_ERROR, NULL);
8527985283
}
8528085284

85281-
if (flecs_query_apply_or_mask(query, table, block_index, i, &mask, &has_bitset)) {
85285+
if (flecs_query_apply_or_mask(query, table, block_index,
85286+
field_term_index, &mask, &has_bitset))
85287+
{
8528285288
continue;
8528385289
}
8528485290

@@ -85317,7 +85323,7 @@ bool flecs_query_toggle_for_up(
8531785323
ecs_flags64_t fields = (and_fields | not_fields) & it->up_fields;
8531885324

8531985325
for (i = 0; i < field_count; i ++) {
85320-
uint64_t field_bit = 1llu << i;
85326+
const uint64_t field_bit = 1llu << i;
8532185327
if (!(fields & field_bit)) {
8532285328
continue;
8532385329
}

src/query/engine/eval_toggle.c

Lines changed: 23 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -33,22 +33,14 @@ bool flecs_query_apply_or_mask(
3333
const ecs_query_t *query,
3434
ecs_table_t *table,
3535
int32_t block_index,
36-
int32_t field_index,
36+
int32_t term_index,
3737
ecs_flags64_t *mask,
3838
bool *has_bitset)
3939
{
40-
/* Find first term for field index. Terms in an OR chain share the
41-
* same field index, so this finds the start of the chain. */
42-
int32_t i = field_index, term_count = query->term_count;
4340
ecs_term_t *terms = query->terms;
41+
int32_t i = term_index;
4442

45-
for (; i < term_count; i ++) {
46-
if (terms[i].field_index == field_index) {
47-
break;
48-
}
49-
}
50-
51-
if (i == term_count || terms[i].oper != EcsOr) {
43+
if (terms[i].oper != EcsOr) {
5244
return false;
5345
}
5446

@@ -89,12 +81,24 @@ flecs_query_row_mask_t flecs_query_get_row_mask(
8981
ecs_query_toggle_ctx_t *op_ctx)
9082
{
9183
ecs_flags64_t mask = UINT64_MAX;
92-
int32_t i, field_count = it->field_count;
93-
ecs_flags64_t fields = and_fields | not_fields;
84+
int32_t i;
85+
const ecs_flags64_t fields = and_fields | not_fields;
86+
int32_t term_index = 0;
87+
const int32_t term_count = query->term_count;
88+
const ecs_term_t *terms = query->terms;
9489
bool has_bitset = false;
9590

96-
for (i = 0; i < field_count; i ++) {
97-
uint64_t field_bit = 1llu << i;
91+
for (i = 0; i < it->field_count; i ++) {
92+
const uint64_t field_bit = 1llu << i;
93+
94+
ecs_assert(term_index < term_count, ECS_INTERNAL_ERROR, NULL);
95+
ecs_assert(terms[term_index].field_index == i, ECS_INTERNAL_ERROR, NULL);
96+
97+
const int32_t field_term_index = term_index;
98+
do {
99+
term_index ++;
100+
} while ((term_index < term_count) && (terms[term_index].field_index == i));
101+
98102
if (!(fields & field_bit)) {
99103
continue;
100104
}
@@ -107,7 +111,9 @@ flecs_query_row_mask_t flecs_query_get_row_mask(
107111
ecs_abort(ECS_INTERNAL_ERROR, NULL);
108112
}
109113

110-
if (flecs_query_apply_or_mask(query, table, block_index, i, &mask, &has_bitset)) {
114+
if (flecs_query_apply_or_mask(query, table, block_index,
115+
field_term_index, &mask, &has_bitset))
116+
{
111117
continue;
112118
}
113119

@@ -146,7 +152,7 @@ bool flecs_query_toggle_for_up(
146152
ecs_flags64_t fields = (and_fields | not_fields) & it->up_fields;
147153

148154
for (i = 0; i < field_count; i ++) {
149-
uint64_t field_bit = 1llu << i;
155+
const uint64_t field_bit = 1llu << i;
150156
if (!(fields & field_bit)) {
151157
continue;
152158
}

0 commit comments

Comments
 (0)