Skip to content

Commit

Permalink
Avoid storing LateBoundDefault attribute values in Rule.
Browse files Browse the repository at this point in the history
The change that produces the desired effect is to compare the attribute value with `Attribute#getDefaultValueUnchecked`. The unchecked method returns `LateBoundDefault`, not its default value. This allows the optimization added in 1d03d53 to also work for `LateBoundDefault`.

Rename `getRawAttrValue` to `getAttrIfStored` to better reflect that it does not fall back to the default value.

PiperOrigin-RevId: 518720504
Change-Id: I3141a1d731aaede8ff81b46ebc819cdb6af0fa0a
  • Loading branch information
justinhorvitz authored and copybara-github committed Mar 23, 2023
1 parent 745ca28 commit c82168e
Show file tree
Hide file tree
Showing 3 changed files with 86 additions and 40 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ private <T> void visitLabels(
if (type.getLabelClass() == LabelClass.NONE) {
// The only way for LabelClass.NONE to contain labels is in select keys.
if (includeSelectKeys && attr.isConfigurable()) {
rawVal = rule.getRawAttrValue(i);
rawVal = rule.getAttrIfStored(i);
if (rawVal instanceof SelectorList) {
visitLabelsInSelect(
(SelectorList<T>) rawVal,
Expand All @@ -128,7 +128,7 @@ private <T> void visitLabels(
}
return;
}
rawVal = rule.getRawAttrValue(i);
rawVal = rule.getAttrIfStored(i);
if (rawVal == null) {
// Frozen rules don't store computed defaults.
if (!attr.hasComputedDefault() || rule.isFrozen()) {
Expand Down
20 changes: 13 additions & 7 deletions src/main/java/com/google/devtools/build/lib/packages/Rule.java
Original file line number Diff line number Diff line change
Expand Up @@ -505,7 +505,7 @@ public <T> Object getAttr(String attrName, Type<T> type) {
*/
@Nullable
private Object getAttrWithIndex(int attrIndex) {
Object value = getRawAttrValue(attrIndex);
Object value = getAttrIfStored(attrIndex);
if (value != null) {
return value;
}
Expand All @@ -519,6 +519,11 @@ private Object getAttrWithIndex(int attrIndex) {
// compute the value.
return isFrozen() ? attr.getDefaultValue() : null;
}
if (attr.isLateBound()) {
// Frozen rules don't store late bound defaults.
checkState(isFrozen(), "Mutable rule missing LateBoundDefault");
return attr.getLateBoundDefault();
}
switch (attr.getName()) {
case GENERATOR_FUNCTION:
return callstack.size() > 1 ? callstack.getFrame(1).name : "";
Expand All @@ -536,7 +541,7 @@ private Object getAttrWithIndex(int attrIndex) {
* <p>Unlike {@link #getAttr}, does not fall back to the default value.
*/
@Nullable
Object getRawAttrValue(int attrIndex) {
Object getAttrIfStored(int attrIndex) {
checkPositionIndex(attrIndex, attrCount() - 1);
switch (getAttrState()) {
case MUTABLE:
Expand Down Expand Up @@ -642,8 +647,9 @@ private void checkAttrType(String attrName, Type<?> requestedType, Attribute att
* Returns {@code true} if this rule's attributes are immutable.
*
* <p>Frozen rules optimize for space by omitting storage for non-explicit attribute values that
* match the {@link Attribute} default. If {@link #getRawAttrValue} returns {@code null}, the
* value should be taken from {@link Attribute#getDefaultValue}, even for computed defaults.
* match the {@link Attribute} default. If {@link #getAttrIfStored} returns {@code null}, the
* value should be taken from either {@link Attribute#getLateBoundDefault} for late-bound defaults
* or {@link Attribute#getDefaultValue} for all other attributes (including computed defaults).
*
* <p>Mutable rules have no such optimization. During rule creation, this allows for
* distinguishing whether a computed default (which may depend on other unset attributes) is
Expand All @@ -667,7 +673,7 @@ void freeze() {
}
if (!getExplicitBit(i)) {
Attribute attr = ruleClass.getAttribute(i);
if (value.equals(attr.getDefaultValue())) {
if (value.equals(attr.getDefaultValueUnchecked())) {
// Non-explicit value matches the attribute's default. Save space by omitting storage.
continue;
}
Expand Down Expand Up @@ -774,7 +780,7 @@ public <T> BuildType.SelectorList<T> getSelectorList(String attributeName, Type<
if (index == null) {
return null;
}
Object attrValue = getRawAttrValue(index);
Object attrValue = getAttrIfStored(index);
if (!(attrValue instanceof BuildType.SelectorList)) {
return null;
}
Expand Down Expand Up @@ -1108,7 +1114,7 @@ private List<Label> getRawVisibilityLabels() {
if (visibilityIndex == null) {
return null;
}
return (List<Label>) getRawAttrValue(visibilityIndex);
return (List<Label>) getAttrIfStored(visibilityIndex);
}

private RuleVisibility getDefaultVisibility() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,7 @@
import com.google.devtools.build.lib.analysis.util.MockRule;
import com.google.devtools.build.lib.analysis.util.MockRuleDefaults;
import com.google.devtools.build.lib.packages.Attribute.ComputedDefault;
import com.google.devtools.build.lib.packages.Attribute.LateBoundDefault;
import com.google.devtools.build.lib.testutil.TestRuleClassProvider;
import com.google.testing.junit.testparameterinjector.TestParameter;
import com.google.testing.junit.testparameterinjector.TestParameterInjector;
Expand All @@ -39,6 +40,7 @@ public final class RuleAttributeStorageTest extends BuildViewTestCase {

private static final String STRING_DEFAULT = Type.STRING.getDefaultValue();
private static final int COMPUTED_DEFAULT_OFFSET = 1;
private static final int LATE_BOUND_DEFAULT_OFFSET = 2;

private enum ContainerSize {
SMALL(16),
Expand All @@ -60,6 +62,8 @@ private enum ContainerSize {
private Attribute lastCustomAttr;
private int computedDefaultIndex;
private Attribute computedDefaultAttr;
private int lateBoundDefaultIndex;
private Attribute lateBoundDefaultAttr;

@Override
protected ConfiguredRuleClassProvider createRuleClassProvider() {
Expand All @@ -72,18 +76,29 @@ protected ConfiguredRuleClassProvider createRuleClassProvider() {
IntStream.range(0, numCustomAttrs)
.mapToObj(
i -> {
var attr = attr("attr" + i, Type.STRING);
// Make one of the attributes a computed default.
// Make one attribute a computed default and one a late bound default.
if (i == COMPUTED_DEFAULT_OFFSET) {
attr.value(
new ComputedDefault() {
@Override
public Object getDefault(AttributeMap rule) {
return "computed";
}
});
return attr("attr" + i + "_computed_default", Type.STRING)
.value(
new ComputedDefault() {
@Override
public Object getDefault(AttributeMap rule) {
return "computed";
}
});
}
return attr;
if (i == LATE_BOUND_DEFAULT_OFFSET) {
return attr(":attr" + i + "_late_bound_default", Type.STRING)
.value(
new LateBoundDefault<>(Void.class, "late_bound") {
@Override
public String resolve(
Rule rule, AttributeMap attributes, Void input) {
return "late_bound";
}
});
}
return attr("attr" + i, Type.STRING);
})
.toArray(Attribute.Builder[]::new));
var builder = new ConfiguredRuleClassProvider.Builder().addRuleDefinition(exampleRule);
Expand Down Expand Up @@ -111,6 +126,8 @@ public void setUpForRule() throws Exception {
lastCustomAttr = attrAt(lastCustomAttrIndex);
computedDefaultIndex = firstCustomAttrIndex + COMPUTED_DEFAULT_OFFSET;
computedDefaultAttr = attrAt(computedDefaultIndex);
lateBoundDefaultIndex = firstCustomAttrIndex + LATE_BOUND_DEFAULT_OFFSET;
lateBoundDefaultAttr = attrAt(lateBoundDefaultIndex);
}

@Test
Expand All @@ -122,9 +139,9 @@ public void attributeSettingAndRetrieval(@TestParameter boolean frozen) {
rule.freeze();
}

assertThat(rule.getRawAttrValue(firstCustomAttrIndex)).isEqualTo("val1");
assertThat(rule.getAttrIfStored(firstCustomAttrIndex)).isEqualTo("val1");
assertThat(rule.isAttributeValueExplicitlySpecified(firstCustomAttr)).isTrue();
assertThat(rule.getRawAttrValue(lastCustomAttrIndex)).isEqualTo("val2");
assertThat(rule.getAttrIfStored(lastCustomAttrIndex)).isEqualTo("val2");
assertThat(rule.isAttributeValueExplicitlySpecified(lastCustomAttr)).isTrue();
}

Expand All @@ -134,7 +151,7 @@ public void indexOutOfBounds_throws(@TestParameter boolean frozen) {
rule.freeze();
}
assertThrows(
IndexOutOfBoundsException.class, () -> rule.getRawAttrValue(lastCustomAttrIndex + 1));
IndexOutOfBoundsException.class, () -> rule.getAttrIfStored(lastCustomAttrIndex + 1));
}

@Test
Expand All @@ -146,10 +163,10 @@ public void testForOffByOneError(@TestParameter boolean frozen) {
rule.freeze();
}

assertThat(rule.getRawAttrValue(firstCustomAttrIndex - 1)).isNull();
assertThat(rule.getAttrIfStored(firstCustomAttrIndex - 1)).isNull();
assertThat(rule.isAttributeValueExplicitlySpecified(attrAt(firstCustomAttrIndex - 1)))
.isFalse();
assertThat(rule.getRawAttrValue(firstCustomAttrIndex + 1)).isNull();
assertThat(rule.getAttrIfStored(firstCustomAttrIndex + 1)).isNull();
assertThat(rule.isAttributeValueExplicitlySpecified(attrAt(firstCustomAttrIndex + 1)))
.isFalse();
}
Expand All @@ -166,9 +183,9 @@ public void testFreezeWorks() {
// Double freezing is a no-op
rule.freeze();
// reads/explicit bits work as expected
assertThat(rule.getRawAttrValue(firstCustomAttrIndex)).isEqualTo("val1");
assertThat(rule.getAttrIfStored(firstCustomAttrIndex)).isEqualTo("val1");
assertThat(rule.isAttributeValueExplicitlySpecified(firstCustomAttr)).isTrue();
assertThat(rule.getRawAttrValue(lastCustomAttrIndex)).isEqualTo("val2");
assertThat(rule.getAttrIfStored(lastCustomAttrIndex)).isEqualTo("val2");
assertThat(rule.isAttributeValueExplicitlySpecified(lastCustomAttr)).isFalse();
// writes no longer work.
assertThrows(
Expand All @@ -189,7 +206,7 @@ public void allAttributesSet(@TestParameter boolean frozen) {
}

for (int i = 1; i < size; i++) { // Skip attribute 0 (name) which is never stored.
assertThat(rule.getRawAttrValue(i)).isEqualTo("value " + i);
assertThat(rule.getAttrIfStored(i)).isEqualTo("value " + i);
assertWithMessage("attribute " + i)
.that(rule.isAttributeValueExplicitlySpecified(attrAt(i)))
.isEqualTo(i % 2 == 0);
Expand Down Expand Up @@ -235,7 +252,7 @@ public void boundaryOfFrozenContainer() {

assertThat(rule.getAttr("name")).isEqualTo(ruleName);
assertThat(rule.isAttributeValueExplicitlySpecified("name")).isTrue();
assertThat(rule.getRawAttrValue(lastCustomAttrIndex)).isEqualTo("last");
assertThat(rule.getAttrIfStored(lastCustomAttrIndex)).isEqualTo("last");
assertThat(rule.isAttributeValueExplicitlySpecified(lastCustomAttr)).isTrue();
}

Expand All @@ -248,7 +265,7 @@ public void nameNotStoredAsRawAttr(@TestParameter boolean frozen) {
rule.freeze();
}

assertThat(rule.getRawAttrValue(0)).isNull();
assertThat(rule.getAttrIfStored(0)).isNull();
assertThat(rule.getRawAttrValues()).doesNotContain(ruleName);
assertThat(rule.getAttr("name")).isEqualTo(ruleName);
assertThat(rule.isAttributeValueExplicitlySpecified("name")).isTrue();
Expand All @@ -262,15 +279,15 @@ public void explicitDefaultValue_stored(@TestParameter boolean frozen) {
rule.freeze();
}

assertThat(rule.getRawAttrValue(firstCustomAttrIndex)).isNotNull();
assertThat(rule.getAttrIfStored(firstCustomAttrIndex)).isNotNull();
assertThat(rule.isAttributeValueExplicitlySpecified(firstCustomAttr)).isTrue();
}

@Test
public void nonExplicitDefaultValue_mutable_stored() {
rule.setAttributeValue(firstCustomAttr, STRING_DEFAULT, /* explicit= */ false);

assertThat(rule.getRawAttrValue(firstCustomAttrIndex)).isNotNull();
assertThat(rule.getAttrIfStored(firstCustomAttrIndex)).isNotNull();
assertThat(rule.isAttributeValueExplicitlySpecified(firstCustomAttr)).isFalse();
}

Expand All @@ -280,37 +297,60 @@ public void nonExplicitDefaultValue_frozen_notStored() {

rule.freeze();

assertThat(rule.getRawAttrValue(firstCustomAttrIndex)).isNull();
assertThat(rule.getAttrIfStored(firstCustomAttrIndex)).isNull();
assertThat(rule.isAttributeValueExplicitlySpecified(firstCustomAttr)).isFalse();
}

@Test
public void computedDefault_mutable_stored() {
Attribute attr = rule.getRuleClassObject().getAttribute(computedDefaultIndex);
var computedDefault = attr.getDefaultValue();
assertThat(attr.hasComputedDefault()).isTrue();
var computedDefault = computedDefaultAttr.getDefaultValue();
assertThat(computedDefaultAttr.hasComputedDefault()).isTrue();
assertThat(computedDefault).isInstanceOf(ComputedDefault.class);

rule.setAttributeValue(computedDefaultAttr, computedDefault, /* explicit= */ false);

assertThat(rule.getRawAttrValue(computedDefaultIndex)).isEqualTo(computedDefault);
assertThat(rule.getAttrIfStored(computedDefaultIndex)).isEqualTo(computedDefault);
assertThat(rule.getAttr(computedDefaultAttr.getName())).isEqualTo(computedDefault);
assertThat(rule.isAttributeValueExplicitlySpecified(computedDefaultAttr)).isFalse();
}

@Test
public void computedDefault_frozen_notStored() {
Attribute attr = rule.getRuleClassObject().getAttribute(computedDefaultIndex);
var computedDefault = attr.getDefaultValue();
assertThat(attr.hasComputedDefault()).isTrue();
var computedDefault = computedDefaultAttr.getDefaultValue();
assertThat(computedDefaultAttr.hasComputedDefault()).isTrue();
assertThat(computedDefault).isInstanceOf(ComputedDefault.class);

rule.setAttributeValue(computedDefaultAttr, computedDefault, /* explicit= */ false);
rule.freeze();

assertThat(rule.getRawAttrValue(computedDefaultIndex)).isNull();
assertThat(rule.getAttrIfStored(computedDefaultIndex)).isNull();
assertThat(rule.getAttr(computedDefaultAttr.getName())).isEqualTo(computedDefault);
assertThat(rule.isAttributeValueExplicitlySpecified(computedDefaultAttr)).isFalse();
}

@Test
public void lateBoundDefault_mutable_stored() {
var lateBoundDefault = lateBoundDefaultAttr.getLateBoundDefault();

rule.setAttributeValue(lateBoundDefaultAttr, lateBoundDefault, /* explicit= */ false);

assertThat(rule.getAttrIfStored(lateBoundDefaultIndex)).isEqualTo(lateBoundDefault);
assertThat(rule.getAttr(lateBoundDefaultAttr.getName())).isEqualTo(lateBoundDefault);
assertThat(rule.isAttributeValueExplicitlySpecified(lateBoundDefaultAttr)).isFalse();
}

@Test
public void lateBoundDefault_frozen_notStored() {
var lateBoundDefault = lateBoundDefaultAttr.getLateBoundDefault();

rule.setAttributeValue(lateBoundDefaultAttr, lateBoundDefault, /* explicit= */ false);
rule.freeze();

assertThat(rule.getAttrIfStored(lateBoundDefaultIndex)).isNull();
assertThat(rule.getAttr(lateBoundDefaultAttr.getName())).isEqualTo(lateBoundDefault);
assertThat(rule.isAttributeValueExplicitlySpecified(lateBoundDefaultAttr)).isFalse();
}

private Attribute attrAt(int attrIndex) {
return rule.getRuleClassObject().getAttribute(attrIndex);
}
Expand Down

0 comments on commit c82168e

Please sign in to comment.