Skip to content

Commit 4f806ff

Browse files
lberkirules_java Copybara
authored andcommitted
Avoid quadratic time on string concatenation in _get_native_deps_helper.
PiperOrigin-RevId: 838641853 Change-Id: Ic2447584007194e7209b856bd97b318bfa34e14a
1 parent 4a9881b commit 4f806ff

1 file changed

Lines changed: 11 additions & 14 deletions

File tree

java/common/rules/impl/java_helper.bzl

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -170,18 +170,15 @@ def _get_shared_native_deps_path(
170170
method below is only ensured by validations in the CppLinkAction.Builder.build() method.
171171
"""
172172

173-
fp = ""
174-
for artifact in linker_inputs:
175-
fp += artifact.short_path
176-
fp += str(len(link_opts))
177-
for opt in link_opts:
178-
fp += opt
179-
for artifact in linkstamps:
180-
fp += artifact.short_path
181-
for artifact in build_info_artifacts:
182-
fp += artifact.short_path
183-
for feature in features:
184-
fp += feature
173+
fp = []
174+
175+
# join() is faster than concatenating many strings individually
176+
fp += [a.short_path for a in linker_inputs]
177+
fp.append(str(len(link_opts)))
178+
fp += link_opts
179+
fp += [a.short_path for a in linkstamps]
180+
fp += [a.short_path for a in build_info_artifacts]
181+
fp += features
185182

186183
# Sharing of native dependencies may cause an ActionConflictException when ThinLTO is
187184
# disabled for test and test-only targets that are statically linked, but enabled for other
@@ -190,9 +187,9 @@ def _get_shared_native_deps_path(
190187
# this, we allow creation of multiple artifacts for the shared native library - one shared
191188
# among the test and test-only targets where ThinLTO is disabled, and the other shared among
192189
# other targets where ThinLTO is enabled.
193-
fp += "1" if is_test_target_partially_disabled_thin_lto else "0"
190+
fp.append("1" if is_test_target_partially_disabled_thin_lto else "0")
194191

195-
fingerprint = "%x" % hash(fp)
192+
fingerprint = "%x" % hash("".join(fp))
196193
return "_nativedeps/" + fingerprint
197194

198195
def _check_and_get_one_version_attribute(ctx, attr):

0 commit comments

Comments
 (0)