Skip to content

Commit a982e40

Browse files
committed
perf: improve analysis performance of py_binary and py_test
1 parent 74d7698 commit a982e40

1 file changed

Lines changed: 15 additions & 23 deletions

File tree

python/private/py_executable.bzl

Lines changed: 15 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -380,9 +380,8 @@ def _create_executable(
380380
_create_zip_file(
381381
ctx,
382382
output = zip_file,
383-
original_nonzip_executable = executable,
384383
zip_main = zip_main,
385-
runfiles = runfiles_details.default_runfiles.merge(extra_runfiles),
384+
runfiles = runfiles_details.runfiles_without_exe.merge(extra_runfiles),
386385
)
387386

388387
extra_files_to_build = []
@@ -803,7 +802,7 @@ def _create_windows_exe_launcher(
803802
use_default_shell_env = True,
804803
)
805804

806-
def _create_zip_file(ctx, *, output, original_nonzip_executable, zip_main, runfiles):
805+
def _create_zip_file(ctx, *, output, zip_main, runfiles):
807806
"""Create a Python zipapp (zip with __main__.py entry point)."""
808807
workspace_name = ctx.workspace_name
809808
legacy_external_runfiles = _py_builtins.get_legacy_external_runfiles(ctx)
@@ -819,17 +818,17 @@ def _create_zip_file(ctx, *, output, original_nonzip_executable, zip_main, runfi
819818
_get_zip_runfiles_path("__init__.py", workspace_name, legacy_external_runfiles),
820819
),
821820
)
822-
for path in runfiles.empty_filenames.to_list():
823-
manifest.add("{}=".format(_get_zip_runfiles_path(path, workspace_name, legacy_external_runfiles)))
821+
822+
def map_empty_filenames(path):
823+
return "{}=".format(_get_zip_runfiles_path(path, workspace_name, legacy_external_runfiles))
824+
825+
manifest.add_all(runfiles.empty_filenames, map_each = map_empty_filenames, allow_closure = True)
824826

825827
def map_zip_runfiles(file):
826-
if file != original_nonzip_executable and file != output:
827-
return "{}={}".format(
828-
_get_zip_runfiles_path(file.short_path, workspace_name, legacy_external_runfiles),
829-
file.path,
830-
)
831-
else:
832-
return None
828+
return "{}={}".format(
829+
_get_zip_runfiles_path(file.short_path, workspace_name, legacy_external_runfiles),
830+
file.path,
831+
)
833832

834833
manifest.add_all(runfiles.files, map_each = map_zip_runfiles, allow_closure = True)
835834

@@ -850,21 +849,14 @@ def _create_zip_file(ctx, *, output, original_nonzip_executable, zip_main, runfi
850849
))
851850
inputs.append(zip_repo_mapping_manifest)
852851

853-
for artifact in runfiles.files.to_list():
854-
# Don't include the original executable because it isn't used by the
855-
# zip file, so no need to build it for the action.
856-
# Don't include the zipfile itself because it's an output.
857-
if artifact != original_nonzip_executable and artifact != output:
858-
inputs.append(artifact)
859-
860852
zip_cli_args = ctx.actions.args()
861853
zip_cli_args.add("cC")
862854
zip_cli_args.add(output)
863855

864856
ctx.actions.run(
865857
executable = ctx.executable._zipper,
866858
arguments = [zip_cli_args, manifest],
867-
inputs = depset(inputs),
859+
inputs = depset(inputs, transitive = [runfiles.files]),
868860
outputs = [output],
869861
use_default_shell_env = True,
870862
mnemonic = "PythonZipper",
@@ -873,13 +865,13 @@ def _create_zip_file(ctx, *, output, original_nonzip_executable, zip_main, runfi
873865

874866
def _get_zip_runfiles_path(path, workspace_name, legacy_external_runfiles):
875867
if legacy_external_runfiles and path.startswith(_EXTERNAL_PATH_PREFIX):
876-
zip_runfiles_path = paths.relativize(path, _EXTERNAL_PATH_PREFIX)
868+
zip_runfiles_path = path.removeprefix(_EXTERNAL_PATH_PREFIX)
877869
else:
878870
# NOTE: External runfiles (artifacts in other repos) will have a leading
879871
# path component of "../" so that they refer outside the main workspace
880-
# directory and into the runfiles root. By normalizing, we simplify e.g.
872+
# directory and into the runfiles root. So we simplify it, e.g.
881873
# "workspace/../foo/bar" to simply "foo/bar".
882-
zip_runfiles_path = paths.normalize("{}/{}".format(workspace_name, path))
874+
zip_runfiles_path = "{}/{}".format(workspace_name, path) if not path.startswith("../") else path[3:]
883875
return "{}/{}".format(_ZIP_RUNFILES_DIRECTORY_NAME, zip_runfiles_path)
884876

885877
def _create_executable_zip_file(

0 commit comments

Comments
 (0)