diff options
author | Googler <noreply@google.com> | 2018-08-07 07:20:57 -0700 |
---|---|---|
committer | Copybara-Service <copybara-piper@google.com> | 2018-08-07 07:22:49 -0700 |
commit | 4d8f31f47a92144dad620442bfedf4e772758b7e (patch) | |
tree | dd29da13dd42218e49c3e18d2f32ecb5c0566185 /src/main | |
parent | 2c9c05b3960914b9120566bf680f2280c1857f82 (diff) |
Improve implementation of filterDiscoveredHeaders. Instead of creating a huge
set of all declared headers, iterate over them and remove them from a (often
much smaller) set.
This should improve runtime as well as amount of garbage produced.
RELNOTES: None.
PiperOrigin-RevId: 207710867
Diffstat (limited to 'src/main')
-rw-r--r-- | src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java | 62 |
1 files changed, 32 insertions, 30 deletions
diff --git a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java index fd9ac8e36b..d8fb87d8ab 100644 --- a/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java +++ b/src/main/java/com/google/devtools/build/lib/rules/cpp/CppCompileAction.java @@ -387,46 +387,48 @@ public class CppCompileAction extends AbstractAction */ private Iterable<Artifact> filterDiscoveredHeaders( ActionExecutionContext actionExecutionContext, Iterable<Artifact> headers) { - // Get the inputs we know about. Note that this (compared to validateInclusions) does not - // take mandatoryInputs into account. The reason is that these by definition get added to the - // action input and thus are available anyway. Not having to look at them here saves us from - // requiring and ArtifactExpander, which actionExecutionContext doesn't have at this point. - // This only works as long as mandatory inputs do not contain headers that are built into a - // module. - Set<Artifact> allowedIncludes = - new HashSet<>(ccCompilationContext.getDeclaredIncludeSrcs().toCollection()); - allowedIncludes.addAll(additionalPrunableHeaders.toCollection()); - - // Whitelisted directories. Lazily initialize, so that compiles that properly declare all - // their files profit. - Set<PathFragment> declaredIncludeDirs = null; - Iterable<PathFragment> ignoreDirs = null; + Set<Artifact> undeclaredHeaders = Sets.newHashSet(headers); + + // Note that this (compared to validateInclusions) does not take mandatoryInputs into account. + // The reason is that these by definition get added to the action input and thus are available + // anyway. Not having to look at them here saves us from requiring and ArtifactExpander, which + // actionExecutionContext doesn't have at this point. This only works as long as mandatory + // inputs do not contain headers that are built into a module. + for (Artifact header : ccCompilationContext.getDeclaredIncludeSrcs()) { + undeclaredHeaders.remove(header); + } + for (Artifact header : additionalPrunableHeaders) { + undeclaredHeaders.remove(header); + } + if (undeclaredHeaders.isEmpty()) { + return headers; + } - // Create a filtered list of headers. - ImmutableList.Builder<Artifact> result = ImmutableList.builder(); - for (Artifact header : headers) { - if (allowedIncludes.contains(header)) { - result.add(header); - continue; - } - if (ignoreDirs == null) { - ignoreDirs = - cppConfiguration.isStrictSystemIncludes() - ? getBuiltInIncludeDirectories() - : getValidationIgnoredDirs(); - } + Iterable<PathFragment> ignoreDirs = + cppConfiguration.isStrictSystemIncludes() + ? getBuiltInIncludeDirectories() + : getValidationIgnoredDirs(); + ArrayList<Artifact> found = new ArrayList<>(); + // Lazily initialize, so that compiles that properly declare all their files profit. + Set<PathFragment> declaredIncludeDirs = null; + for (Artifact header : undeclaredHeaders) { if (FileSystemUtils.startsWithAny(header.getExecPath(), ignoreDirs)) { - result.add(header); + found.add(header); continue; } if (declaredIncludeDirs == null) { declaredIncludeDirs = ccCompilationContext.getDeclaredIncludeDirs().toSet(); } if (isDeclaredIn(actionExecutionContext, header, declaredIncludeDirs)) { - result.add(header); + found.add(header); } } - return result.build(); + undeclaredHeaders.removeAll(found); + if (undeclaredHeaders.isEmpty()) { + return headers; + } + + return Iterables.filter(headers, header -> !undeclaredHeaders.contains(header)); } @Nullable |