Skip to content

Commit 5a942a6

Browse files
authored
chore(perf): refactor checking path for directory (#639)
- Instead of shelling out to test each path, the new code finds all of the directories under the target in one shell call. The result is then added to a set and the set is checked. - The starlark profile for the `soto_example` dropped from 350 seconds to 46 seconds. - Simplified the implementation for `repository_files.is_directory()`. Closes #582.
1 parent 73f28a1 commit 5a942a6

File tree

2 files changed

+10
-14
lines changed

2 files changed

+10
-14
lines changed

swiftpkg/internal/pkginfos.bzl

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -780,12 +780,19 @@ def _new_swift_src_info_from_sources(repository_ctx, target_path, sources):
780780
target_path,
781781
)
782782

783+
# Identify the directories
784+
directories = repository_files.list_directories_under(
785+
repository_ctx,
786+
target_path,
787+
)
788+
dirs_set = sets.make(directories)
789+
783790
# The paths should be relative to the target not the root of the workspace.
784791
# Do not include directories in the output.
785792
discovered_res_files = [
786793
f
787794
for f in all_target_files
788-
if not repository_files.is_directory(repository_ctx, f) and
795+
if not sets.contains(dirs_set, f) and
789796
resource_files.is_auto_discovered_resource(f)
790797
]
791798

swiftpkg/internal/repository_files.bzl

Lines changed: 2 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -162,8 +162,6 @@ def _copy_directory(repository_ctx, src, dest):
162162
],
163163
)
164164

165-
# GH582: Refactor _is_directory to be like _path_exists.
166-
167165
def _is_directory(repository_ctx, path):
168166
"""Determine if the provided path is a directory.
169167
@@ -174,20 +172,11 @@ def _is_directory(repository_ctx, path):
174172
Returns:
175173
A `bool` specifying whether the path is a directory.
176174
"""
177-
args = ["bash", "-c", "if [[ -d ${TARGET_PATH} ]]; then echo TRUE; else echo FALSE; fi"]
178175
exec_result = repository_ctx.execute(
179-
args,
180-
environment = {"TARGET_PATH": path},
176+
["test", "-d", path],
181177
quiet = True,
182178
)
183-
if exec_result.return_code != 0:
184-
fail("Failed while checking if '{path}' is a directory. stderr:\n{stderr}".format(
185-
path = path,
186-
stderr = exec_result.stderr,
187-
))
188-
if exec_result.stdout.find("TRUE") >= 0:
189-
return True
190-
return False
179+
return exec_result.return_code == 0
191180

192181
def _file_type(repository_ctx, path):
193182
"""Output the file type.

0 commit comments

Comments
 (0)