Skip to content

Commit

Permalink
Optimize classpath pre-processing in java_stub_template.txt
Browse files Browse the repository at this point in the history
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
  • Loading branch information
rsalvador authored and copybara-github committed Sep 11, 2023
1 parent 735e59d commit 4e8f0bd
Showing 1 changed file with 16 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -306,29 +306,32 @@ function create_and_run_classpath_jar() {

OLDIFS="$IFS"
IFS="${CLASSPATH_SEPARATOR}" # Use a custom separator for the loop.
current_dir=$(pwd)
for path in ${CLASSPATH}; do
# Loop through the characters of the path and convert characters that are
# not alphanumeric nor -_.~/ to their 2-digit hexadecimal representation
local i c buff
local converted_path=""

for ((i=0; i<${#path}; i++)); do
c=${path:$i:1}
case ${c} in
[-_.~/a-zA-Z0-9] ) buff=${c} ;;
* ) printf -v buff '%%%02x' "'$c'"
esac
converted_path+="${buff}"
done
path=${converted_path}
if [[ ! $path =~ ^[-_.~/a-zA-Z0-9]*$ ]]; then
local i c buff
local converted_path=""

for ((i=0; i<${#path}; i++)); do
c=${path:$i:1}
case ${c} in
[-_.~/a-zA-Z0-9] ) buff=${c} ;;
* ) printf -v buff '%%%02x' "'$c'"
esac
converted_path+="${buff}"
done
path=${converted_path}
fi

if is_windows; then
path="file:/${path}" # e.g. "file:/C:/temp/foo.jar"
else
# If not absolute, qualify the path
case "${path}" in
/*) ;; # Already an absolute path
*) path="$(pwd)/${path}";; # Now qualified
*) path="${current_dir}/${path}";; # Now qualified
esac
path="file:${path}" # e.g. "file:/usr/local/foo.jar"
fi
Expand Down

0 comments on commit 4e8f0bd

Please sign in to comment.