Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[6.4.0] Also apply NestedSet optimizations to Depset #19492

Merged
merged 2 commits into from
Sep 14, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -392,7 +392,16 @@ static Depset fromDirectAndTransitive(
if (builder.isEmpty()) {
return builder.getOrder().emptyDepset();
}
return new Depset(type, builder.build());
NestedSet<Object> set = builder.build();
// If the nested set was optimized to one of the transitive elements, reuse the corresponding
// depset.
for (Depset x : transitive) {
if (x.getSet() == set) {
return x;
}
}

return new Depset(type, set);
}

/** An exception thrown when validation fails on the type of elements of a nested set. */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -417,4 +417,17 @@ public void testEmptyDepsetInternedPerOrder() throws Exception {
assertThat(ev.lookup("stable1")).isNotSameInstanceAs(ev.lookup("preorder1"));
assertThat(ev.lookup("stable2")).isNotSameInstanceAs(ev.lookup("preorder2"));
}

@Test
public void testSingleNonEmptyTransitiveAndNoDirectsUnwrapped() throws Exception {
ev.exec(
"inner = depset([1, 2, 3])", "outer = depset(transitive = [depset(), inner, depset()])");
assertThat(ev.lookup("outer")).isSameInstanceAs(ev.lookup("inner"));
}

@Test
public void testSingleNonEmptyTransitiveAndMatchingDirectUnwrapped() throws Exception {
ev.exec("inner = depset([1])", "outer = depset([1], transitive = [depset(), inner, depset()])");
assertThat(ev.lookup("outer")).isSameInstanceAs(ev.lookup("inner"));
}
}