Skip to content

Commit

Permalink
Tiny bugfix for StarlarkProvider.depsetTypePredictor.
Browse files Browse the repository at this point in the history
A witness value of null occurs the first time optimizeValue is called so
it should also get an unwrapped value.

PiperOrigin-RevId: 636793488
Change-Id: I7f9a23626a2044c33e962adbf5b9d5481c2058d1
  • Loading branch information
aoeui authored and copybara-github committed May 24, 2024
1 parent c4167e3 commit 015f3a8
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 2 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -484,7 +484,8 @@ Object optimizeField(int index, Object value) {
return depset.getSet();
}
Class<?> elementClass = depset.getElementClass();
if (depsetTypePredictor.compareAndExchange(index, null, elementClass) == elementClass) {
Class<?> witness = depsetTypePredictor.compareAndExchange(index, null, elementClass);
if (witness == elementClass || witness == null) {
return depset.getSet();
}
}
Expand All @@ -502,7 +503,7 @@ Object retrieveOptimizedField(int index, Object value) {
// This matches empty depsets created in Starlark with `depset()`.
return Depset.of(Object.class, nestedSet);
}
@SuppressWarnings("unchecked") // can't parametrize Class literal by a non-raw type
@SuppressWarnings("unchecked") // can't parameterize Class literal by a non-raw type
Depset depset = Depset.of((Class<Object>) depsetTypePredictor.get(index), nestedSet);
return depset;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -449,6 +449,29 @@ public void schemafulProvider_withDepset() throws Exception {
assertThat(instance6.getValue("field")).isNull();
}

@Test
public void schemafulProvider_optimizeField() {
StarlarkProvider provider =
StarlarkProvider.builder(Location.BUILTIN)
.setSchema(ImmutableList.of("field"))
.buildWithIdentityToken(generator.generate());

// The first set is unwrapped and the type String is registered in the predictor.
Depset depset1 = Depset.of(String.class, NestedSetBuilder.create(STABLE_ORDER, "a", "b", "c"));
assertThat(provider.optimizeField(0, depset1)).isSameInstanceAs(depset1.getSet());

// A set with Integer type does not match and cannot be optimized.
Depset depset2 =
Depset.of(
StarlarkInt.class,
NestedSetBuilder.create(STABLE_ORDER, StarlarkInt.of(1), StarlarkInt.of(2)));
assertThat(provider.optimizeField(0, depset2)).isSameInstanceAs(depset2);

// A third, matching Depset is unwrapped.
Depset depset3 = Depset.of(String.class, NestedSetBuilder.create(STABLE_ORDER, "d", "e"));
assertThat(provider.optimizeField(0, depset3)).isSameInstanceAs(depset3.getSet());
}

@Test
public void schemafulProvider_mutable() throws Exception {
StarlarkProvider.Key key =
Expand Down

0 comments on commit 015f3a8

Please sign in to comment.