Skip to content

Commit

Permalink
Optimize OptionalTypeCoercer unconfigured to configured coercion for …
Browse files Browse the repository at this point in the history
…identity

Reviewed By: gabrielrussoc

shipit-source-id: 3255ca058795b1ef2f4f8a5b7b393c4dab05c325
  • Loading branch information
stepancheg authored and facebook-github-bot committed Feb 26, 2020
1 parent 6a00645 commit 140f565
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 2 deletions.
14 changes: 12 additions & 2 deletions src/com/facebook/buck/rules/coercer/OptionalTypeCoercer.java
Original file line number Diff line number Diff line change
Expand Up @@ -68,6 +68,11 @@ public void traverse(CellNameResolver cellRoots, Optional<T> object, Traversal t
}
}

@Override
public boolean unconfiguredToConfiguredCoercionIsIdentity() {
return coercer.unconfiguredToConfiguredCoercionIsIdentity();
}

@Override
public Optional<U> coerceToUnconfigured(
CellNameResolver cellRoots,
Expand All @@ -82,6 +87,7 @@ public Optional<U> coerceToUnconfigured(
coercer.coerceToUnconfigured(cellRoots, filesystem, pathRelativeToProjectRoot, object));
}

@SuppressWarnings("unchecked")
@Override
public Optional<T> coerce(
CellNameResolver cellRoots,
Expand All @@ -92,14 +98,18 @@ public Optional<T> coerce(
Optional<U> object)
throws CoerceFailedException {
if (object.isPresent()) {
return Optional.of(
T coerced =
coercer.coerce(
cellRoots,
filesystem,
pathRelativeToProjectRoot,
targetConfiguration,
hostConfiguration,
object.get()));
object.get());
if (coerced == object.get()) {
return (Optional<T>) object;
}
return Optional.of(coerced);
} else {
return Optional.empty();
}
Expand Down
19 changes: 19 additions & 0 deletions test/com/facebook/buck/rules/coercer/OptionalTypeCoercerTest.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,9 +19,11 @@
import static org.junit.Assert.assertEquals;
import static org.junit.Assert.assertFalse;
import static org.junit.Assert.assertNull;
import static org.junit.Assert.assertSame;
import static org.junit.Assert.assertThat;

import com.facebook.buck.core.cell.TestCellBuilder;
import com.facebook.buck.core.cell.nameresolver.TestCellNameResolver;
import com.facebook.buck.core.model.UnconfiguredTargetConfiguration;
import com.facebook.buck.core.path.ForwardRelativePath;
import com.facebook.buck.io.filesystem.ProjectFilesystem;
Expand Down Expand Up @@ -133,4 +135,21 @@ public void testConcatOfPresentConcatableElementsReturnsAggregatedResult() {
Optional.of(ImmutableList.of("a", "c"))))
.get());
}

@Test
public void coerceUnconfiguredToConfiguredOptimizedIdentity() throws Exception {
OptionalTypeCoercer<String, String> coercer =
new OptionalTypeCoercer<>(new StringTypeCoercer());

Optional<String> input = Optional.of("aaa");
Optional<String> coerced =
coercer.coerce(
TestCellNameResolver.forRoot(),
new FakeProjectFilesystem(),
ForwardRelativePath.EMPTY,
UnconfiguredTargetConfiguration.INSTANCE,
UnconfiguredTargetConfiguration.INSTANCE,
input);
assertSame(input, coerced);
}
}

0 comments on commit 140f565

Please sign in to comment.