Skip to content

Commit 4e8f0bd

Browse files
rsalvadorcopybara-github
authored andcommitted
Optimize classpath pre-processing in java_stub_template.txt
The classpath pre-processing in this `java_stub_template.txt` loop: https://github.com/bazelbuild/bazel/blob/fcfcb929366dd3faac9643302b19c88bcf871ec6/src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt#L309 is slow for long classpaths. For example for classpaths with ~250,000 and ~700,000 entries the loop takes 28 and 50 seconds, respectively, on an intel MacBook. This change reduce the times to 1 second or less. Fixes #19480 Closes #19481. PiperOrigin-RevId: 564491123 Change-Id: Id4be898c3f800d5390dd8bf997535a5e71a76ba3
1 parent 735e59d commit 4e8f0bd

File tree

1 file changed

+16
-13
lines changed

1 file changed

+16
-13
lines changed

src/main/java/com/google/devtools/build/lib/bazel/rules/java/java_stub_template.txt

Lines changed: 16 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -306,29 +306,32 @@ function create_and_run_classpath_jar() {
306306

307307
OLDIFS="$IFS"
308308
IFS="${CLASSPATH_SEPARATOR}" # Use a custom separator for the loop.
309+
current_dir=$(pwd)
309310
for path in ${CLASSPATH}; do
310311
# Loop through the characters of the path and convert characters that are
311312
# not alphanumeric nor -_.~/ to their 2-digit hexadecimal representation
312-
local i c buff
313-
local converted_path=""
314-
315-
for ((i=0; i<${#path}; i++)); do
316-
c=${path:$i:1}
317-
case ${c} in
318-
[-_.~/a-zA-Z0-9] ) buff=${c} ;;
319-
* ) printf -v buff '%%%02x' "'$c'"
320-
esac
321-
converted_path+="${buff}"
322-
done
323-
path=${converted_path}
313+
if [[ ! $path =~ ^[-_.~/a-zA-Z0-9]*$ ]]; then
314+
local i c buff
315+
local converted_path=""
316+
317+
for ((i=0; i<${#path}; i++)); do
318+
c=${path:$i:1}
319+
case ${c} in
320+
[-_.~/a-zA-Z0-9] ) buff=${c} ;;
321+
* ) printf -v buff '%%%02x' "'$c'"
322+
esac
323+
converted_path+="${buff}"
324+
done
325+
path=${converted_path}
326+
fi
324327

325328
if is_windows; then
326329
path="file:/${path}" # e.g. "file:/C:/temp/foo.jar"
327330
else
328331
# If not absolute, qualify the path
329332
case "${path}" in
330333
/*) ;; # Already an absolute path
331-
*) path="$(pwd)/${path}";; # Now qualified
334+
*) path="${current_dir}/${path}";; # Now qualified
332335
esac
333336
path="file:${path}" # e.g. "file:/usr/local/foo.jar"
334337
fi

0 commit comments

Comments
 (0)