Skip to content

Commit

Permalink
Automated rollback of commit 76c8c3a.
Browse files Browse the repository at this point in the history
*** Reason for rollback ***

Partially rolling back, because CapturingMapFn seems to be still applicable, see
#14892

*** Original change description ***

Remove native proto_library implementation.

PiperOrigin-RevId: 440820625
  • Loading branch information
comius authored and copybara-github committed Apr 11, 2022
1 parent 3a1aea3 commit c70e072
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,15 @@ abstract class ParametrizedMapFn<T> implements MapFn<T> {
public abstract int maxInstancesAllowed();
}

/**
* Use this map function when your map function needs to capture per-rule information.
*
* <p>Use of this class prevents sharing sub-computations over shared NestedSets, since the map
* function is per-target. This will make your action key computations become O(N^2). Please avoid
* if possible.
*/
interface CapturingMapFn<T> extends MapFn<T> {}

/** Expands the object into the command line as a string. */
String expandToCommandLine();

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
import com.google.common.collect.Multiset;
import com.google.devtools.build.lib.actions.CommandLineExpansionException;
import com.google.devtools.build.lib.actions.CommandLineItem;
import com.google.devtools.build.lib.actions.CommandLineItem.MapFn;
import com.google.devtools.build.lib.util.Fingerprint;
import com.google.devtools.build.lib.vfs.DigestHashFunction;
import java.util.HashSet;
Expand All @@ -44,6 +45,10 @@ public <T> void addNestedSetToFingerprint(Fingerprint fingerprint, NestedSet<T>
public <T> void addNestedSetToFingerprint(
CommandLineItem.MapFn<? super T> mapFn, Fingerprint fingerprint, NestedSet<T> nestedSet)
throws CommandLineExpansionException, InterruptedException {
if (mapFn instanceof CommandLineItem.CapturingMapFn) {
addNestedSetToFingerprintSlow(mapFn, fingerprint, nestedSet);
return;
}
// Only top-level nested sets can be empty, so we can bail here
if (nestedSet.isEmpty()) {
fingerprint.addInt(EMPTY_SET_DIGEST);
Expand All @@ -55,6 +60,14 @@ public <T> void addNestedSetToFingerprint(
addToFingerprint(mapFn, fingerprint, digestMap, children);
}

private <T> void addNestedSetToFingerprintSlow(
MapFn<? super T> mapFn, Fingerprint fingerprint, NestedSet<T> nestedSet)
throws CommandLineExpansionException, InterruptedException {
for (T object : nestedSet.toList()) {
addToFingerprint(mapFn, fingerprint, object);
}
}

public void clear() {
mapFnToDigestMap = createMap();
seenMapFns.clear();
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@
import com.google.common.collect.Multiset;
import com.google.devtools.build.lib.actions.CommandLineExpansionException;
import com.google.devtools.build.lib.actions.CommandLineItem;
import com.google.devtools.build.lib.actions.CommandLineItem.CapturingMapFn;
import com.google.devtools.build.lib.actions.CommandLineItem.MapFn;
import com.google.devtools.build.lib.util.Fingerprint;
import java.util.function.Consumer;
Expand Down Expand Up @@ -141,6 +142,12 @@ public void testMultipleInstancesOfMapFnThrows() throws Exception {
(s, args) -> args.accept(s + "_mapped"), new Fingerprint(), nestedSet);
}

// Make sure a CapturingMapFn doesn't get denied
for (int i = 0; i < 2; ++i) {
cache.addNestedSetToFingerprint(
(CapturingMapFn<String>) (s, args) -> args.accept(s + 1), new Fingerprint(), nestedSet);
}

// Make sure a ParametrizedMapFn doesn't get denied until it exceeds its instance count
cache.addNestedSetToFingerprint(new IntParametrizedMapFn(1), new Fingerprint(), nestedSet);
cache.addNestedSetToFingerprint(new IntParametrizedMapFn(2), new Fingerprint(), nestedSet);
Expand Down

0 comments on commit c70e072

Please sign in to comment.