Skip to content

Commit 2363674

Browse files
committed
#1572 Default cache policy to cached for queries with cascade term
1 parent 7954400 commit 2363674

File tree

5 files changed

+53
-9
lines changed

5 files changed

+53
-9
lines changed

distr/flecs.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32130,6 +32130,17 @@ int flecs_query_set_caching_policy(
3213032130
bool group_order_by = desc->group_by || desc->group_by_callback ||
3213132131
desc->order_by || desc->order_by_callback;
3213232132

32133+
/* If the query has a Cascade term it'll use group_by */
32134+
int32_t i, term_count = impl->pub.term_count;
32135+
const ecs_term_t *terms = impl->pub.terms;
32136+
for (i = 0; i < term_count; i ++) {
32137+
const ecs_term_t *term = &terms[i];
32138+
if (term->src.id & EcsCascade) {
32139+
group_order_by = true;
32140+
break;
32141+
}
32142+
}
32143+
3213332144
/* If caching policy is default, try to pick a policy that does the right
3213432145
* thing in most cases. */
3213532146
if (kind == EcsQueryCacheDefault) {
@@ -32152,7 +32163,7 @@ int flecs_query_set_caching_policy(
3215232163
/* Don't cache query, even if it has cacheable terms */
3215332164
if (kind == EcsQueryCacheNone) {
3215432165
impl->pub.cache_kind = EcsQueryCacheNone;
32155-
if (desc->group_by || desc->order_by) {
32166+
if (group_order_by && !(impl->pub.flags & EcsQueryNested)) {
3215632167
ecs_err("cannot create uncached query with group_by/order_by");
3215732168
return -1;
3215832169
}
@@ -32186,8 +32197,6 @@ int flecs_query_set_caching_policy(
3218632197
* terms, as this would build a cache that contains all tables. */
3218732198
int32_t not_optional_terms = 0, cacheable_terms = 0;
3218832199
if (!group_order_by) {
32189-
int32_t i, term_count = impl->pub.term_count;
32190-
const ecs_term_t *terms = impl->pub.terms;
3219132200
for (i = 0; i < term_count; i ++) {
3219232201
const ecs_term_t *term = &terms[i];
3219332202
if (term->flags_ & EcsTermIsCacheable) {

src/query/api.c

Lines changed: 12 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -76,6 +76,17 @@ int flecs_query_set_caching_policy(
7676
bool group_order_by = desc->group_by || desc->group_by_callback ||
7777
desc->order_by || desc->order_by_callback;
7878

79+
/* If the query has a Cascade term it'll use group_by */
80+
int32_t i, term_count = impl->pub.term_count;
81+
const ecs_term_t *terms = impl->pub.terms;
82+
for (i = 0; i < term_count; i ++) {
83+
const ecs_term_t *term = &terms[i];
84+
if (term->src.id & EcsCascade) {
85+
group_order_by = true;
86+
break;
87+
}
88+
}
89+
7990
/* If caching policy is default, try to pick a policy that does the right
8091
* thing in most cases. */
8192
if (kind == EcsQueryCacheDefault) {
@@ -98,7 +109,7 @@ int flecs_query_set_caching_policy(
98109
/* Don't cache query, even if it has cacheable terms */
99110
if (kind == EcsQueryCacheNone) {
100111
impl->pub.cache_kind = EcsQueryCacheNone;
101-
if (desc->group_by || desc->order_by) {
112+
if (group_order_by && !(impl->pub.flags & EcsQueryNested)) {
102113
ecs_err("cannot create uncached query with group_by/order_by");
103114
return -1;
104115
}
@@ -132,8 +143,6 @@ int flecs_query_set_caching_policy(
132143
* terms, as this would build a cache that contains all tables. */
133144
int32_t not_optional_terms = 0, cacheable_terms = 0;
134145
if (!group_order_by) {
135-
int32_t i, term_count = impl->pub.term_count;
136-
const ecs_term_t *terms = impl->pub.terms;
137146
for (i = 0; i < term_count; i ++) {
138147
const ecs_term_t *term = &terms[i];
139148
if (term->flags_ & EcsTermIsCacheable) {

test/query/project.json

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1627,7 +1627,8 @@
16271627
"invalid_cascade_for_second",
16281628
"invalid_desc_without_cascade",
16291629
"invalid_desc_for_first",
1630-
"invalid_desc_for_second"
1630+
"invalid_desc_for_second",
1631+
"cascade_w_cache_kind_default"
16311632
]
16321633
}, {
16331634
"id": "Cached",

test/query/src/Cascade.c

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -974,7 +974,8 @@ void Cascade_invalid_cascade_for_uncached(void) {
974974
ecs_log_set_level(-4);
975975

976976
ecs_query_t *q = ecs_query(world, {
977-
.expr = "Position(cascade)"
977+
.expr = "Position(cascade)",
978+
.cache_kind = EcsQueryCacheNone
978979
});
979980

980981
test_assert(q == NULL);
@@ -1079,3 +1080,22 @@ void Cascade_invalid_desc_for_second(void) {
10791080

10801081
ecs_fini(world);
10811082
}
1083+
1084+
void Cascade_cascade_w_cache_kind_default(void) {
1085+
ecs_world_t *world = ecs_mini();
1086+
1087+
ECS_COMPONENT(world, Position);
1088+
1089+
ecs_log_set_level(-4);
1090+
1091+
ecs_query_t *q = ecs_query(world, {
1092+
.expr = "Position(cascade)",
1093+
.cache_kind = EcsQueryCacheDefault
1094+
});
1095+
1096+
test_assert(q != NULL);
1097+
1098+
ecs_query_fini(q);
1099+
1100+
ecs_fini(world);
1101+
}

test/query/src/main.c

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1566,6 +1566,7 @@ void Cascade_invalid_cascade_for_second(void);
15661566
void Cascade_invalid_desc_without_cascade(void);
15671567
void Cascade_invalid_desc_for_first(void);
15681568
void Cascade_invalid_desc_for_second(void);
1569+
void Cascade_cascade_w_cache_kind_default(void);
15691570

15701571
// Testsuite 'Cached'
15711572
void Cached_simple_query_existing_table(void);
@@ -8288,6 +8289,10 @@ bake_test_case Cascade_testcases[] = {
82888289
{
82898290
"invalid_desc_for_second",
82908291
Cascade_invalid_desc_for_second
8292+
},
8293+
{
8294+
"cascade_w_cache_kind_default",
8295+
Cascade_cascade_w_cache_kind_default
82918296
}
82928297
};
82938298

@@ -10799,7 +10804,7 @@ static bake_test_suite suites[] = {
1079910804
"Cascade",
1080010805
NULL,
1080110806
NULL,
10802-
23,
10807+
24,
1080310808
Cascade_testcases
1080410809
},
1080510810
{

0 commit comments

Comments
 (0)