Skip to content

Commit e3c1ea3

Browse files
committed
doc-links(msbuild): a group's condition is evaluated once, not per child (S1, evaluator side)
The checkpoint writes an <ImportGroup>'s condition once into the project blob (S1), but the evaluator still evaluated it again for every import of the group, and an <ItemGroup>'s condition again for every <Using> of the group: the product imports (or items) x condition length that S1 removed from the blob came back as evaluator work. An <ImportGroup>'s condition is now evaluated once, at the group's first import, which is where MSBuild evaluates it: a property the group's first import sets no longer takes the group's later imports away. A legacy blob (the condition copied into every import) is still evaluated per import. An <ItemGroup>'s condition is evaluated once per group for the items of one pass; the final properties the items see do not change while they are read, so the result is the same. Tests: msbuild_import_group_condition_work and msbuild_item_group_condition_work (doubling both the children and the condition length at most doubles the evaluator's work counter; it quadrupled), msbuild_import_group_condition_once. Signed-off-by: Martin Vogel <martin.vogel.tech@gmail.com>
1 parent 429a40f commit e3c1ea3

2 files changed

Lines changed: 189 additions & 11 deletions

File tree

‎src/pipeline/doc_links_msbuild.c‎

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -557,6 +557,12 @@ typedef struct {
557557
st_component_t *owner;
558558
msb_tri_t group; /* the condition of the <PropertyGroup> being read */
559559
const msb_rec_t *hgroup; /* the <ItemGroup> being read */
560+
/* The <ImportGroup> being read: its condition's text (one string the
561+
* group's imports share) and what it came to. MSBuild evaluates a
562+
* group's condition once, where the group stands; evaluating it again
563+
* for every import cost imports x condition length. */
564+
const char *igroup_text;
565+
msb_tri_t igroup;
560566
} msb_frame_t;
561567

562568
enum { MSB_SEEDS = 5, MSB_LIVE_OWNERS = 5 };
@@ -2609,9 +2615,10 @@ static void msb_add_item(msb_eval_t *ev, const msb_rec_t *group, const msb_rec_t
26092615
msb_work(sizeof(msb_item_t));
26102616
}
26112617

2612-
/* An <Import> standing in the file on top of the frame stack. */
2613-
static void msb_import(msb_eval_t *ev, int file, const msb_rec_t *r) {
2614-
msb_tri_t c = tri_and(msb_cond(ev, file, r->f[0]), msb_cond(ev, file, r->f[1]));
2618+
/* An <Import> standing in the file on top of the frame stack, under its
2619+
* <ImportGroup>'s condition `group` (MSB_TRUE outside a group). */
2620+
static void msb_import(msb_eval_t *ev, int file, const msb_rec_t *r, msb_tri_t group) {
2621+
msb_tri_t c = tri_and(group, msb_cond(ev, file, r->f[1]));
26152622
if (c == MSB_FALSE || r->f[3]) {
26162623
return; /* not taken; or an SDK's file, which is no file of the repository */
26172624
}
@@ -2704,9 +2711,20 @@ static void msb_pass1(msb_eval_t *ev, int file) {
27042711
case 'C':
27052712
ev->unevaluable++;
27062713
break;
2707-
case 'I':
2708-
msb_import(ev, at, r);
2714+
case 'I': {
2715+
/* the group's condition once per group: its imports share the
2716+
* text (cbm_msb_add); a legacy blob's copies are evaluated each */
2717+
msb_tri_t group = MSB_TRUE;
2718+
if (r->f[0]) {
2719+
if (fr->igroup_text != r->f[0]) {
2720+
fr->igroup_text = r->f[0];
2721+
fr->igroup = msb_cond(ev, at, r->f[0]);
2722+
}
2723+
group = fr->igroup;
2724+
}
2725+
msb_import(ev, at, r, group); /* may push a frame: fr is not used after it */
27092726
break;
2727+
}
27102728
default:
27112729
break;
27122730
}
@@ -2846,12 +2864,32 @@ static int target_cmp(const void *a, const void *b) {
28462864
return strcmp(((const cbm_msb_using_t *)a)->target, ((const cbm_msb_using_t *)b)->target);
28472865
}
28482866

2867+
/* An <ItemGroup>'s condition for the items of one pass over them. MSBuild
2868+
* evaluates it once, where the group stands, and the final properties the
2869+
* items see do not change while they are read: evaluating it again for
2870+
* every item cost items x condition length. */
2871+
typedef struct {
2872+
const msb_rec_t *group;
2873+
msb_tri_t value;
2874+
} msb_group_memo_t;
2875+
2876+
static msb_tri_t item_group_cond(msb_eval_t *ev, const msb_item_t *it, msb_group_memo_t *memo) {
2877+
if (!it->group) {
2878+
return MSB_TRUE;
2879+
}
2880+
if (memo->group != it->group) {
2881+
memo->group = it->group;
2882+
memo->value = msb_cond(ev, it->file, it->group->f[0]);
2883+
}
2884+
return memo->value;
2885+
}
2886+
28492887
/* One <Using> item with the final properties: into `inc` or `rem`. */
2850-
static void msb_using(msb_eval_t *ev, const msb_item_t *it, msb_ulist_t *inc, msb_ulist_t *rem) {
2888+
static void msb_using(msb_eval_t *ev, const msb_item_t *it, msb_ulist_t *inc, msb_ulist_t *rem,
2889+
msb_group_memo_t *memo) {
28512890
const msb_rec_t *r = it->item;
28522891
msb_record();
2853-
msb_tri_t c = tri_and(msb_cond(ev, it->file, it->group ? it->group->f[0] : NULL),
2854-
msb_cond(ev, it->file, r->f[0]));
2892+
msb_tri_t c = tri_and(item_group_cond(ev, it, memo), msb_cond(ev, it->file, r->f[0]));
28552893
if (c == MSB_FALSE) {
28562894
return;
28572895
}
@@ -3111,8 +3149,9 @@ bool cbm_msb_eval(const cbm_msb_t *m, const char *project_rel, cbm_msb_result_t
31113149
for (int i = 0; implicit && implicit[i]; i++) {
31123150
ulist_add(&ev, &inc, 'n', "", implicit[i]);
31133151
}
3152+
msb_group_memo_t memo = {0};
31143153
for (int i = 0; i < ev.nitems; i++) {
3115-
msb_using(&ev, &ev.items[i], &inc, &rem);
3154+
msb_using(&ev, &ev.items[i], &inc, &rem, &memo);
31163155
/* All four expansions remain live until aliases and targets
31173156
* have been copied into the persistent evaluation arena. */
31183157
cbm_arena_reset(&ev.scratch);
@@ -3435,8 +3474,9 @@ static bool apply_targets(cbm_msb_eval_context_t *context, msb_eval_t *ev, int r
34353474

34363475
static void eval_items(msb_eval_t *ev, const msb_eval_t *owner, msb_ulist_t *inc,
34373476
msb_ulist_t *rem) {
3477+
msb_group_memo_t memo = {0};
34383478
for (int i = 0; !ev->oom && i < owner->nitems; i++) {
3439-
msb_using(ev, &owner->items[i], inc, rem);
3479+
msb_using(ev, &owner->items[i], inc, rem, &memo);
34403480
cbm_arena_reset(&ev->scratch);
34413481
}
34423482
}
@@ -3682,14 +3722,15 @@ bool cbm_msb_eval_context_eval(cbm_msb_eval_context_t *context, const char *proj
36823722
static void st_eval_rope(msb_eval_t *ev, st_rope_t *rope, msb_ulist_t *inc, msb_ulist_t *rem) {
36833723
st_rope_t *stack[CBM_SZ_64];
36843724
int depth = 0;
3725+
msb_group_memo_t memo = {0};
36853726
while (!ev->oom && (rope || depth)) {
36863727
if (!rope) {
36873728
rope = stack[--depth];
36883729
continue;
36893730
}
36903731
msb_work(1);
36913732
if (!rope->left) {
3692-
msb_using(ev, &rope->item, inc, rem);
3733+
msb_using(ev, &rope->item, inc, rem, &memo);
36933734
cbm_arena_reset(&ev->scratch);
36943735
rope = NULL;
36953736
} else {

‎tests/test_doc_mentions.c‎

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3483,6 +3483,140 @@ TEST(doc_mentions_msbuild_import_group_roundtrip) {
34833483
PASS();
34843484
}
34853485

3486+
/* The evaluator side of the same product: an <ImportGroup>'s condition is
3487+
* evaluated once, where the group stands, not again for every import in it.
3488+
* Doubling both the imports and the condition's length at most doubles the
3489+
* evaluator's work; evaluated per import, it would quadruple. */
3490+
TEST(doc_mentions_msbuild_import_group_condition_work) {
3491+
enum { IMPORTS = 100, TERMS = 100, CAP = 64 * 1024 };
3492+
/* Flag is defined (an undefined property is unknown); no term holds */
3493+
static const char term[] = "'$(Flag)' == 'aaaaaaaaaaaaaaaa'";
3494+
uint64_t work[2] = {0};
3495+
char *xml = malloc(CAP);
3496+
ASSERT_NOT_NULL(xml);
3497+
for (int sample = 0; sample < 2; sample++) {
3498+
int imports = IMPORTS << sample;
3499+
int terms = TERMS << sample;
3500+
size_t used = (size_t)snprintf(xml, CAP,
3501+
"<Project><PropertyGroup><Flag>b</Flag></PropertyGroup>"
3502+
"<ImportGroup Condition=\"");
3503+
for (int i = 0; i < terms; i++) {
3504+
used += (size_t)snprintf(xml + used, CAP - used, "%s%s", i ? " or " : "", term);
3505+
}
3506+
used += (size_t)snprintf(xml + used, CAP - used, "\">");
3507+
for (int i = 0; i < imports; i++) {
3508+
used += (size_t)snprintf(xml + used, CAP - used, "<Import Project=\"X.props\"/>");
3509+
}
3510+
used += (size_t)snprintf(xml + used, CAP - used,
3511+
"</ImportGroup><ItemGroup><Using Include=\"Kept\"/>"
3512+
"</ItemGroup></Project>");
3513+
ASSERT_LT(used, (size_t)CAP);
3514+
cbm_msb_t *m = cbm_msb_new();
3515+
ASSERT_NOT_NULL(m);
3516+
ASSERT_TRUE(dm_msb_add_xml(m, "App.csproj", xml));
3517+
ASSERT_TRUE(dm_msb_add_xml(
3518+
m, "X.props",
3519+
"<Project><ItemGroup><Using Include=\"Not.Taken\"/></ItemGroup></Project>"));
3520+
cbm_msb_test_cost_reset();
3521+
cbm_msb_result_t r;
3522+
ASSERT_TRUE(cbm_msb_eval(m, "App.csproj", &r));
3523+
work[sample] = cbm_msb_test_work();
3524+
ASSERT_TRUE(dm_has_using(&r, 'n', "Kept"));
3525+
ASSERT_FALSE(dm_has_using(&r, 'n', "Not.Taken"));
3526+
ASSERT_EQ(r.count, 1);
3527+
ASSERT_FALSE(r.open);
3528+
ASSERT_EQ(r.unevaluable, 0);
3529+
cbm_msb_result_free(&r);
3530+
cbm_msb_free(m);
3531+
}
3532+
free(xml);
3533+
fprintf(stderr, "msbuild import group condition work: %llu -> %llu\n",
3534+
(unsigned long long)work[0], (unsigned long long)work[1]);
3535+
ASSERT_GT(work[0], 0);
3536+
ASSERT_LTE(work[1], 2 * work[0]);
3537+
PASS();
3538+
}
3539+
3540+
/* The same for an <ItemGroup>: its condition is evaluated once for the
3541+
* items of a pass, not again for every <Using> in it. */
3542+
TEST(doc_mentions_msbuild_item_group_condition_work) {
3543+
enum { USINGS = 100, TERMS = 100, CAP = 64 * 1024 };
3544+
static const char term[] = "'$(Flag)' == 'aaaaaaaaaaaaaaaa'";
3545+
uint64_t work[2] = {0};
3546+
char *xml = malloc(CAP);
3547+
ASSERT_NOT_NULL(xml);
3548+
for (int sample = 0; sample < 2; sample++) {
3549+
int usings = USINGS << sample;
3550+
int terms = TERMS << sample;
3551+
size_t used = (size_t)snprintf(xml, CAP,
3552+
"<Project><PropertyGroup><Flag>b</Flag></PropertyGroup>"
3553+
"<ItemGroup Condition=\"");
3554+
for (int i = 0; i < terms; i++) {
3555+
used += (size_t)snprintf(xml + used, CAP - used, "%s%s", i ? " or " : "", term);
3556+
}
3557+
used += (size_t)snprintf(xml + used, CAP - used, "\">");
3558+
for (int i = 0; i < usings; i++) {
3559+
used +=
3560+
(size_t)snprintf(xml + used, CAP - used, "<Using Include=\"Not.Taken%04d\"/>", i);
3561+
}
3562+
used += (size_t)snprintf(xml + used, CAP - used,
3563+
"</ItemGroup><ItemGroup><Using Include=\"Kept\"/>"
3564+
"</ItemGroup></Project>");
3565+
ASSERT_LT(used, (size_t)CAP);
3566+
cbm_msb_t *m = cbm_msb_new();
3567+
ASSERT_NOT_NULL(m);
3568+
ASSERT_TRUE(dm_msb_add_xml(m, "App.csproj", xml));
3569+
cbm_msb_test_cost_reset();
3570+
cbm_msb_result_t r;
3571+
ASSERT_TRUE(cbm_msb_eval(m, "App.csproj", &r));
3572+
work[sample] = cbm_msb_test_work();
3573+
ASSERT_TRUE(dm_has_using(&r, 'n', "Kept"));
3574+
ASSERT_EQ(r.count, 1);
3575+
ASSERT_FALSE(r.open);
3576+
ASSERT_EQ(r.unevaluable, 0);
3577+
cbm_msb_result_free(&r);
3578+
cbm_msb_free(m);
3579+
}
3580+
free(xml);
3581+
fprintf(stderr, "msbuild item group condition work: %llu -> %llu\n",
3582+
(unsigned long long)work[0], (unsigned long long)work[1]);
3583+
ASSERT_GT(work[0], 0);
3584+
ASSERT_LTE(work[1], 2 * work[0]);
3585+
PASS();
3586+
}
3587+
3588+
/* MSBuild evaluates an <ImportGroup>'s condition once, before its imports:
3589+
* a property the group's first import sets does not take the second import
3590+
* away. */
3591+
TEST(doc_mentions_msbuild_import_group_condition_once) {
3592+
const dm_project_file_t files[] = {
3593+
/* a property no file defines is unknown (the SDK or the
3594+
* environment may set it): Stop is defined first */
3595+
{"App.csproj", "<Project><PropertyGroup><Stop>no</Stop></PropertyGroup>"
3596+
"<ImportGroup Condition=\"'$(Stop)' == 'no'\">"
3597+
"<Import Project=\"First.props\"/><Import Project=\"Second.props\"/>"
3598+
"</ImportGroup>"
3599+
"<ImportGroup Condition=\"'$(Stop)' == 'no'\">"
3600+
"<Import Project=\"Third.props\"/></ImportGroup></Project>"},
3601+
{"First.props", "<Project><PropertyGroup><Stop>yes</Stop></PropertyGroup>"
3602+
"<ItemGroup><Using Include=\"From.First\"/></ItemGroup></Project>"},
3603+
{"Second.props",
3604+
"<Project><ItemGroup><Using Include=\"From.Second\"/></ItemGroup></Project>"},
3605+
{"Third.props",
3606+
"<Project><ItemGroup><Using Include=\"From.Third\"/></ItemGroup></Project>"},
3607+
};
3608+
cbm_msb_result_t r;
3609+
ASSERT_TRUE(dm_msb_eval(files, 4, "App.csproj", &r));
3610+
ASSERT_TRUE(dm_has_using(&r, 'n', "From.First"));
3611+
ASSERT_TRUE(dm_has_using(&r, 'n', "From.Second"));
3612+
/* a later group with the same text is evaluated where it stands */
3613+
ASSERT_FALSE(dm_has_using(&r, 'n', "From.Third"));
3614+
ASSERT_EQ(r.count, 2);
3615+
ASSERT_FALSE(r.open);
3616+
cbm_msb_result_free(&r);
3617+
PASS();
3618+
}
3619+
34863620
TEST(doc_mentions_msbuild_legacy_import_blob) {
34873621
cbm_msb_t *m = cbm_msb_new();
34883622
ASSERT_NOT_NULL(m);
@@ -9837,6 +9971,9 @@ SUITE(doc_mentions) {
98379971
RUN_TEST(doc_mentions_msbuild_blob);
98389972
RUN_TEST(doc_mentions_msbuild_import_group_blob_growth);
98399973
RUN_TEST(doc_mentions_msbuild_import_group_roundtrip);
9974+
RUN_TEST(doc_mentions_msbuild_import_group_condition_work);
9975+
RUN_TEST(doc_mentions_msbuild_item_group_condition_work);
9976+
RUN_TEST(doc_mentions_msbuild_import_group_condition_once);
98409977
RUN_TEST(doc_mentions_msbuild_legacy_import_blob);
98419978
RUN_TEST(doc_mentions_msbuild_bad_import_group_blob);
98429979
RUN_TEST(doc_mentions_msbuild_imports);

0 commit comments

Comments
 (0)