Skip to content

Parallelize source remapping for shadowed sources JAR - #2345

Open
Goooler wants to merge 3 commits into
mainfrom
g/20261025/parallel-shadowed-sources
Open

Goooler wants to merge 3 commits into
mainfrom
g/20261025/parallel-shadowed-sources

Conversation

@Goooler

@Goooler Goooler commented Oct 25, 2026 •

Copy link
Copy Markdown
Member

Follow-up to #2265, applying the same approach as #2302 to the shadowed sources JAR.

Source files are read on the calling thread and remapped on Dispatchers.Default, through a bounded sliding window (128 pending entries) that writes entries in their original order. When a resource comes up, the pending sources are written first and then the resource is written directly, so resources are never buffered and the output stays deterministic.

Benchmark Environment & Configuration

  • Project: ktfmt
  • Task: :ktfmt:shadowJar --rerun (enableAutoRelocation = true, generateSourcesJar = true)
  • Gradle Version: 9.6.1
  • Profiler Setup: Gradle Profiler (2 warm-ups, 5 measured iterations)
  • Versions: cf71351a (sequential baseline) vs 6551b877 (parallel source remapping)

The measured time includes the main JAR. Bytecode remapping for the main JAR is already parallel in both versions (#2302), so the difference comes from generating the sources JAR (~14.5 MB). Both versions produce byte-identical main and sources JARs.

Performance Comparison

Iteration cf71351a (Sequential Baseline) 6551b877 (Parallel Optimization) Difference
Warm-up 1 (Cold) 34,522.97 ms (~34.52s) 18,069.06 ms (~18.07s) -47.66%
Warm-up 2 (Warm) 29,089.41 ms (~29.09s) 12,601.45 ms (~12.60s) -56.68%
Measured 1 28,834.43 ms (~28.83s) 13,113.39 ms (~13.11s) -54.52%
Measured 2 28,830.81 ms (~28.83s) 13,034.65 ms (~13.03s) -54.79%
Measured 3 28,874.38 ms (~28.87s) 13,157.47 ms (~13.16s) -54.43%
Measured 4 28,668.95 ms (~28.67s) 13,323.53 ms (~13.32s) -53.53%
Measured 5 29,049.97 ms (~29.05s) 13,646.15 ms (~13.65s) -53.03%
Mean (Measured) 28,851.71 ms (28.85s) 13,255.04 ms (13.26s) -54.06%
Median (Measured) 28,834.43 ms (28.83s) 13,157.47 ms (13.16s) -54.37%

Summary

  • Speedup: ~2.18x faster overall.
  • Time Reduction: The mean time for :ktfmt:shadowJar dropped from ~28.9s to ~13.3s (~54.1% less).

Copilot AI left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

🟢 Approval recommended

The changes preserve ordered, single-threaded archive access while bounding parallel work, with no unresolved findings from static review.

No open issues 🎉

What changed in this PR

Extends the parallel remapping approach used for bytecode to shadowed sources JAR generation, while preserving deterministic entry order.

Changes:

  • Remaps sources on Dispatchers.Default through a bounded 128-entry queue, keeping file reads and ZIP writes on the calling thread.
  • Adds a regression test covering 500 sources and their companion resources.
  • Clarifies that relocators must support concurrent source remapping.
File Description
src/​test/​kotlin/​com/​github/​jengelman/​gradle/​plugins/​shadow/​internal/​ShadowedSourcesJarTest.kt Tests source and resource output across multiple queue windows.
src/​main/​kotlin/​com/​github/​jengelman/​gradle/​plugins/​shadow/​relocation/​Relocator.kt Extends thread-safety documentation to source remapping.
src/​main/​kotlin/​com/​github/​jengelman/​gradle/​plugins/​shadow/​internal/​ShadowedSourcesJar.kt Adds bounded parallel source remapping with ordered ZIP writes.

Review effort: Balanced

@Goooler
Goooler force-pushed the g/20261025/parallel-shadowed-sources branch from 90a14ef to 6551b87 Compare October 25, 2026 11:54
Apply the same sliding-window coroutine approach used for bytecode
remapping in ShadowCopyAction to source file remapping in
generateSourcesJar, keeping entry order deterministic. Resources are
written directly after draining the pending sources, so they are never
buffered.
Share the sliding-window ordered writer between ShadowCopyAction and
the shadowed sources JAR generation instead of duplicating the pending
entry queue in both.
@Goooler
Goooler force-pushed the g/20261025/parallel-shadowed-sources branch from 6551b87 to 939c356 Compare October 25, 2026 12:02
@Goooler
Goooler added this pull request to stack #2347 October 25, 2026 12:09

This branch has not been deployed

No deployments
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants