diff --git a/src/main/java/com/google/devtools/build/lib/collect/nestedset/Depset.java b/src/main/java/com/google/devtools/build/lib/collect/nestedset/Depset.java index d3ef68b6fc2f41..1d1d6e88a0302d 100644 --- a/src/main/java/com/google/devtools/build/lib/collect/nestedset/Depset.java +++ b/src/main/java/com/google/devtools/build/lib/collect/nestedset/Depset.java @@ -377,7 +377,16 @@ public static Depset fromDirectAndTransitive( if (builder.isEmpty()) { return builder.getOrder().emptyDepset(); } - return new Depset(type, builder.build()); + NestedSet 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. */ diff --git a/src/test/java/com/google/devtools/build/lib/collect/nestedset/DepsetTest.java b/src/test/java/com/google/devtools/build/lib/collect/nestedset/DepsetTest.java index 87b18b02860b70..2129bf31bb0cf7 100644 --- a/src/test/java/com/google/devtools/build/lib/collect/nestedset/DepsetTest.java +++ b/src/test/java/com/google/devtools/build/lib/collect/nestedset/DepsetTest.java @@ -419,4 +419,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")); + } }