Library shared objects are relinked whenever a dependency is rebuilt, even when no object file changed

Open
#12,476 3 comments 7 reactions 0 assignees View on GitHub

Maintainers usually reply within 3 days

Nobody has claimed this yet.

Assessment

Difficulty
4/5
Estimated time
3-5 days
Newbie friendliness
48/100
Issue type
Bug
Clarity
Clearly specified
Activity status
Active
Tech stack
haskell

Research direction

Start with Distribution.Simple.GHC.Build.Link.linkLibrary and the Ar.createArLibArchive call named in the issue; trace how dependency rebuilds reach the library build step and how it decides whether to run ghc -shared. Reproduce with the two local packages and comment-only edit described above, then compare timestamps and command output. Done means unchanged object files no longer trigger shared-library linking or static re-archiving.

Written by the indexing model from the issue text.

Description

attention: pr-welcome type: bug

Problem

When a local package is rebuilt because one of its dependencies changed, Cabal relinks the package's shared library (ghc -shared) and re-archives its static library (ar) unconditionally. It does this even when GHC's recompilation check found nothing to recompile.

In a project with many local packages this dominates incremental builds. After a comment-only edit to one widely used library in our monorepo (126 local packages), cabal build all reports nothing to recompile in the 185 dependent components, yet it spends 387 seconds of CPU time, 40% of the whole build, running ghc -shared for 80 libraries whose inputs did not change.

invocation count total share
ghc --make (recompilation checks, nothing recompiled) 290 569s 59%
ghc -shared (library relinks) 80 387s 40%
ghc-pkg 185 8s 1%
ar 80 3s 0%

Wall time for that edit is 127 to 209 seconds on 16 cores. With --disable-shared the same edit takes 74 to 79 seconds.

I discovered this when comparing cabal against Simon Marlowe's cabal buck2 experiment, which doesn't have this problem. This issue is responsible for almost all the speedup of buck2 over cabal on our monorepo!

Reproduction

Two local packages, pkg-b depending on pkg-a, each with one module:

cabal-version: 3.0
name: pkg-a
version: 0.1
build-type: Simple
library
  hs-source-dirs: src
  exposed-modules: A
  build-depends: base
  default-language: Haskell2010

pkg-b is the same with exposed-modules: B and build-depends: base, pkg-a; B imports A. cabal.project lists both.

cabal build all
echo '-- a comment' >> pkg-a/src/A.hs
cabal build all -v2

The second build recompiles A (its source changed) and prints no Compiling line for B, since the interface hash of A is unchanged. For pkg-b it nevertheless runs

ghc ... -shared -dynamic ... -o .../libHSpkg-b-0.1-inplace-ghc9.12.4.so

and ar for libHSpkg-b-0.1-inplace.a, and both files get new timestamps.

Observed with cabal-install 3.14.2.0 (Cabal 3.14.2.0) and cabal-install 3.18.1.0 (Cabal 3.18.2.0), GHC 9.12.4, Linux x86_64. The default configuration applies: shared libraries on, executables static, -dynamic-too for libraries.

Cause

ghc --make only decides which modules to recompile, Cabal itself decides what to link. Distribution.Simple.GHC.Build.Link.linkLibrary runs Ar.createArLibArchive and ghc -shared for each enabled way unconditionally when the component's build step runs. cabal-install runs that build step whenever any dependency of the component was rebuilt, which after an edit to a leaf library is every component downstream of it.

For executables this problem does not arise, because GHC performs the link inside --make and skips it when nothing is newer than the executable. #1177 fixed the static-archive side effect of this (deterministic ar output so that downstream executables stop relinking), but the library links themselves still run.

Desired outcome

Skip the library link steps when none of the object files being linked is newer than the existing library (or, more robustly, when their contents are unchanged), the same way GHC skips an executable link. Alternatively, have GHC do the library link so its existing check applies.

Dominant language
Haskell
Stars
1.8k
Forks
766
Avg merge
5d 21h
Merged PRs (30d)
27

Getting set up

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

More from haskell/cabal

All issues in haskell/cabal

Similar issues

More Haskell issues

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.