Skip to content

Commit a426081

Browse files
committed
Improve efficiency of exclude_external_sources
Thanks @xinzhengzhang!
1 parent c6cd079 commit a426081

File tree

1 file changed

+8
-6
lines changed

1 file changed

+8
-6
lines changed

refresh.template.py

+8-6
Original file line numberDiff line numberDiff line change
@@ -467,8 +467,9 @@ def _get_headers(compile_action, source_path: str):
467467

468468
if {exclude_headers} == "all":
469469
return set()
470-
elif {exclude_headers} == "external" and compile_action.is_external:
470+
elif {exclude_headers} == "external" and not {exclude_external_sources} and compile_action.is_external:
471471
# Shortcut - an external action can't include headers in the workspace (or, non-external headers)
472+
# The `not {exclude_external_sources}`` clause makes sure is_external was precomputed; there are no external actions if they've already been filtered in the process of excluding external sources.
472473
return set()
473474

474475
output_file = None
@@ -783,7 +784,7 @@ def _convert_compile_commands(aquery_output):
783784
"""
784785

785786
# Tag actions as external if we're going to need to know that later.
786-
if {exclude_external_sources} or {exclude_headers} == "external":
787+
if {exclude_headers} == "external" and not {exclude_external_sources}:
787788
targets_by_id = {target.id : target.label for target in aquery_output.targets}
788789

789790
def _amend_action_as_external(action):
@@ -797,9 +798,6 @@ def _amend_action_as_external(action):
797798

798799
aquery_output.actions = (_amend_action_as_external(action) for action in aquery_output.actions)
799800

800-
if {exclude_external_sources}:
801-
aquery_output.actions = filter(lambda action: not action.is_external, aquery_output.actions)
802-
803801
# Process each action from Bazelisms -> file paths and their clang commands
804802
# Threads instead of processes because most of the execution time is farmed out to subprocesses. No need to sidestep the GIL. Might change after https://github.com/clangd/clangd/issues/123 resolved
805803
with concurrent.futures.ThreadPoolExecutor(
@@ -855,13 +853,17 @@ def _get_commands(target: str, flags: str):
855853
In a moment, Bazel will likely fail to parse.""")
856854

857855
# First, query Bazel's C-family compile actions for that configured target
856+
target_statment = f'deps({target})'
857+
if {exclude_external_sources}:
858+
# For efficiency, have bazel filter out external targets (and therefore actions) before they even get turned into actions or serialized and sent to us. Note: this is a different mechanism than is used for excluding just external headers.
859+
target_statment = f"filter('^//',{target_statment})"
858860
aquery_args = [
859861
'bazel',
860862
'aquery',
861863
# Aquery docs if you need em: https://docs.bazel.build/versions/master/aquery.html
862864
# Aquery output proto reference: https://github.com/bazelbuild/bazel/blob/master/src/main/protobuf/analysis_v2.proto
863865
# One bummer, not described in the docs, is that aquery filters over *all* actions for a given target, rather than just those that would be run by a build to produce a given output. This mostly isn't a problem, but can sometimes surface extra, unnecessary, misconfigured actions. Chris has emailed the authors to discuss and filed an issue so anyone reading this could track it: https://github.com/bazelbuild/bazel/issues/14156.
864-
f"mnemonic('(Objc|Cpp)Compile',deps({target}))",
866+
f"mnemonic('(Objc|Cpp)Compile',{target_statment})",
865867
# We switched to jsonproto instead of proto because of https://github.com/bazelbuild/bazel/issues/13404. We could change back when fixed--reverting most of the commit that added this line and tweaking the build file to depend on the target in that issue. That said, it's kinda nice to be free of the dependency, unless (OPTIMNOTE) jsonproto becomes a performance bottleneck compated to binary protos.
866868
'--output=jsonproto',
867869
# We'll disable artifact output for efficiency, since it's large and we don't use them. Small win timewise, but dramatically less json output from aquery.

0 commit comments

Comments
 (0)