Avoid quadratic time on string concatenation in _get_native_deps_helper.

PiperOrigin-RevId: 838641853
Change-Id: Ic2447584007194e7209b856bd97b318bfa34e14a
diff --git a/java/common/rules/impl/java_helper.bzl b/java/common/rules/impl/java_helper.bzl
index 683bf60..d54c8f6 100644
--- a/java/common/rules/impl/java_helper.bzl
+++ b/java/common/rules/impl/java_helper.bzl
@@ -170,18 +170,15 @@
     method below is only ensured by validations in the CppLinkAction.Builder.build() method.
     """
 
-    fp = ""
-    for artifact in linker_inputs:
-        fp += artifact.short_path
-    fp += str(len(link_opts))
-    for opt in link_opts:
-        fp += opt
-    for artifact in linkstamps:
-        fp += artifact.short_path
-    for artifact in build_info_artifacts:
-        fp += artifact.short_path
-    for feature in features:
-        fp += feature
+    fp = []
+
+    # join() is faster than concatenating many strings individually
+    fp += [a.short_path for a in linker_inputs]
+    fp.append(str(len(link_opts)))
+    fp += link_opts
+    fp += [a.short_path for a in linkstamps]
+    fp += [a.short_path for a in build_info_artifacts]
+    fp += features
 
     # Sharing of native dependencies may cause an ActionConflictException when ThinLTO is
     # disabled for test and test-only targets that are statically linked, but enabled for other
@@ -190,9 +187,9 @@
     # this, we allow creation of multiple artifacts for the shared native library - one shared
     # among the test and test-only targets where ThinLTO is disabled, and the other shared among
     # other targets where ThinLTO is enabled.
-    fp += "1" if is_test_target_partially_disabled_thin_lto else "0"
+    fp.append("1" if is_test_target_partially_disabled_thin_lto else "0")
 
-    fingerprint = "%x" % hash(fp)
+    fingerprint = "%x" % hash("".join(fp))
     return "_nativedeps/" + fingerprint
 
 def _check_and_get_one_version_attribute(ctx, attr):