Skip to content

Build the libraries in parallel stages - #868

Open
Dummgelaufen007 wants to merge 3 commits into
wader:masterfrom
Dummgelaufen007:parallel-stages
Open

Dummgelaufen007 wants to merge 3 commits into
wader:masterfrom
Dummgelaufen007:parallel-stages

Conversation

@Dummgelaufen007

Copy link
Copy Markdown
Contributor

Build the libraries in parallel stages

Almost none of the libraries needs another self-built library, yet each one waits for all the ones before it. The goal of this PR is a shorter build: libraries that do not depend on each other are built at the same time, so the runner's cores do work while one library is in a phase that uses only one of them.

Only where a library uses another self-built one, its stage builds on the stage of that library:

glib ──┬─► harfbuzz ──┬─► libass
       │              ▼
       └─► cairo ───► pango ───┐
                      dav1d ───┼─► librsvg
                      rust ──┬─┘
                             └─► rav1e
vmaf ──► aom
libudfread ──► libbluray
[libdvdcss] ──► libdvdread ──► libdvdnav
ogg ──┬─► theora
      └─► vorbis
highway ──┬─► libjxl
lcms2 ────┘
  • aom needs vmaf for -DCONFIG_TUNE_VMAF=1, librsvg needs dav1d for avif, libdvdread links libdvdcss when it is enabled.
  • cairo needs glib: librsvg requires cairo-gobject (meson.build#L71), which cairo builds only when it finds glib (meson.build#L587-L598). Today the order of the recipes meets this without saying so.

harfbuzz also stays on glib, which it uses when it finds it (meson_options.txt#L2), so it is built as today.

rust, cargo and cargo-c move from the package list of base into their own rust stage. Every chain starts at base, and only librsvg and rav1e need rust, so the others no longer wait for its install.

versions.json

The library versions are ARGs of their own stages now, so builder no longer sees them as env.X_VERSION. Instead each library that has a key in versions.json ends its RUN with one line that writes its version to a file named after that key, e.g.

echo "$VMAF_VERSION" > /usr/local/share/versions/libvmaf

The COPY --from lines bring these files into builder with the rest of /usr/local. The versions.json step keeps reading the apk packages and ffmpeg as before and adds one entry per file, with the file name as key:

jq -S -Rn '{ ... } + reduce inputs as $v ({}; . + {(input_filename | split("/")[-1]): $v})' /usr/local/share/versions/* > /versions.json

libfdk-aac and libdvdcss write their file only when ENABLE_FDKAAC or ENABLE_LIBDVDCSS is set, which replaces the two del(). The keys are the same as today; -S sorts them.

ffmpeg itself stays the same: the same libraries, built with the same recipes.

Research and wording by Claude (Anthropic), directed and reviewed by me.

@wader

wader commented Nov 9, 2026

Copy link
Copy Markdown
Owner

Interesting, is it possible to get some kind of reliable benchmark how much build time i saved by this? the CI build times are a bit all over the place, but judging by "best times" it seems to be around 2min saved?

@Dummgelaufen007

Copy link
Copy Markdown
Contributor Author

Most of the spread comes from the runners, not from the builds. ubicloud-standard-30-arm hands out two kinds of machine, and the top of each job log names them as "Machine name" and "CPUs":

  • vm… with 30 CPUs and 94 GiB: a serial build takes 32–37 min
  • ip-10-… with 32 CPUs and 123.5 GiB: a serial build takes 15–17 min

The arm64 "best times" all come from ip-10-…, while this PR ran on vm…, so they compare two different machines. On amd64, all recent jobs but one ran on vm….

Compared on the same machine type, vm… with 30 CPUs:

serial, recent PRs this PR saved
amd64 992–1168 s, mean 1085 s (9 runs) 732 s 33 %
arm64 1928–2228 s, mean 2063 s (4 runs) 937 s 55 %

On arm64 this PR takes as long on a vm… runner (15:37) as a serial build on an ip-10-… runner (15:09–16:54), so it no longer matters much which of the two the job gets.

There is only one run of this PR so far; a re-run that lands on an ip-10-… arm runner would show the gain on that machine too.

Runs used (job time from start to completion):

Run amd64 arm64
this PR 12:12 vm 15:37 vm
bump-libgme 18:07 vm 32:08 vm
libgsm-release 18:14 vm 35:58 vm
bump-alpine 19:28 vm 15:24 ip-10
bump-harfbuzz 18:48 vm 37:08 vm
bump-svtav1 18:26 ip-10 16:25 ip-10
add-sources 16:32 vm 15:11 ip-10
pinned-sources 17:22 vm 15:09 ip-10
satisfy-lib-requirements-by-apk-cairo 17:47 vm 15:18 ip-10
download-with-curl 18:24 vm 32:19 vm
satisfy-lib-requirements-by-apk-cairo 18:05 vm 16:54 ip-10

Research and wording by Claude (Anthropic), directed and reviewed by me.

@wader

wader commented Nov 12, 2026

Copy link
Copy Markdown
Owner

Thanks for testing, have not forgot about this and, still debating with myself if the gain is worth the complexity. I wonder how this will behave with build machines with different resources, ex how aggressive is docker build with parallelization? could you end up with OOM or cache thrashing? also wonder where the current build ends up waiting or under utilizing the cpu?

@Dummgelaufen007

Copy link
Copy Markdown
Contributor Author

Complexity

The complexity is not new: a build like this only works if you know the dependencies, serial or not. The serial Dockerfile keeps them implicit, in the order of the recipes and in one long package list, so each one has to be rediscovered by trial, as for #856 when dav1d had to move above librsvg. With stages, FROM and COPY --from name each dependency, libraries and tools alike: librsvg says it needs dav1d, and only librsvg and rav1e start FROM rust. The Dockerfile documents it, and nothing waits for what it does not need. The build log names each step after its library too: [x265 1/1] instead of [builder 55/69] followed by a long command where the library's name shows up somewhere.

It might also be a small step towards what #216 was after: each library keeps its parts together in one stage, and its dependencies are written down, like pango with FROM harfbuzz AS pango and COPY --from=cairo.

Machines with different resources

A plain docker build . starts every library as soon as it can, with nothing to hold it back. At the start almost all of them run at once; towards the end only a few.

Each library takes a little longer that way, but the whole build is done much sooner, since one library's single-core steps no longer leave the other cores idle. What grows is memory, since every library being built needs its own. On four of my machines, a plain docker build . of this PR, without any limit, ran up to 45 libraries at once:

Ryzen 7 5700X (WSL2), 8 threads, 7.8 GiB i7-7700HQ, 8 threads, 15.5 GiB Ryzen V1500B (NAS), 8 threads, ~10 GiB free Pentium G3220T, 2 cores, 11.6 GiB
master, serial 26:07 45:50 1:18:52 1:45:43
this PR 20:20 (22 % sooner) 38:43 (16 % sooner) 1:06:05 (16 % sooner) 1:42:43 (3 % sooner)

The more cores, the more idle time there is to fill; with two cores there is little, but the build does not get slower either. On your CI runners with 30 or more cores it takes half the time on amd64 and a third on arm64.

OOM and cache thrashing

On GitHub's default runners, 4 threads and 16 GB, it builds without any limit on both amd64 and arm64 (run). It needs about 2 GiB of RAM per thread; in a test VM with 1 GiB per thread it ran short, with 2 GiB it went through. The crowding costs each library time, x265 took three to four times as long as in the serial build, yet the build as a whole was still done first.

Should the crowding ever be a concern, make and ninja already have a switch for it: with -l$(nproc) they start no new job while the machine is busy, right in the Dockerfile.

Where the current build waits

About a third of the serial build is spent in steps that use one core: downloading, unpacking, configure, linking and installing. During those steps the other cores wait.

amd64 vm… arm64 vm… arm64 ip-10-…
downloading, unpacking, configure 17 % 18 % 24 %
compiling 70 % 69 % 60 %
linking and installing 13 % 14 % 16 %

On the faster ip-10-… runner the share is higher, since compiling gets quicker there and these steps do not.

Runs used

Run Runner amd64 Runner arm64
this PR, e925eee vm… vm…
this PR, 890d60a vm… ip-10-…
bump-libgme, serial vm… vm…
bump-libjxl-0.13.0, serial – ip-10-…

Research and wording by Claude (Anthropic), directed and reviewed by me.

Dummgelaufen007 added a commit to Dummgelaufen007/static-ffmpeg that referenced this pull request Nov 13, 2026
Answers the maintainer's questions on wader#868 in his order: complexity,
machines with different resources, OOM and thrashing, and where the
serial build waits, based on builds without limit on four of my hosts,
the CI runs and two colima runs with 4 and 8 GiB of RAM.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
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