Skip to content

Add --semaphore#6851

Merged
mpilgrem merged 8 commits intocommercialhaskell:masterfrom
horriblename:sema
Feb 21, 2026
Merged

Add --semaphore#6851
mpilgrem merged 8 commits intocommercialhaskell:masterfrom
horriblename:sema

Conversation

@horriblename
Copy link
Copy Markdown
Contributor

@horriblename horriblename commented Feb 20, 2026

implements #6131

Please include the following checklist in your pull request:

  • Any changes that could be relevant to users have been recorded in ChangeLog.md.
  • The documentation has been updated, if necessary

Please also shortly describe how you tested your change. Bonus points for added tests!

(I'm not sure what kind of test I can add for this?)

a quick benchmark matches claims in https://well-typed.com/blog/2023/08/reducing-haskell-parallel-build-times/ regarding compiling lens (112s vs 140s, ~20% improvement).

$ time stack exec -- stack build --work-dir .temp-work lens
  298.37s user 43.14s system 242% cpu 2:20.69 total

# with --semaphore
$ time stack exec -- stack build --semaphore --work-dir .temp-work lens
  349.93s user 47.79s system 353% cpu 1:52.50 total

rm -r $(stack exec -- stack path --snapshot-pkg-db) was deleted before each run

Some notes about the implementation

  • this uses semaphore-compat, which is also used by cabal

@horriblename horriblename changed the title Sema Add --semaphore Feb 20, 2026
Comment on lines +1149 to +1150
Uses the semaphore feature in Cabal/GHC to build multiple modules from the same
package in parallel. (see GHC's documentation on `-jsem`)
Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

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

probably better if I can refer to cabal's docs for the --semaphore flag, but I couldn't find any (aside from the extremely short description in --help). There are some docs for cabal-install's --semaphore but that's not quite the same thing

@horriblename horriblename force-pushed the sema branch 2 times, most recently from d66961a to 26128a1 Compare February 20, 2026 00:15
@mpilgrem
Copy link
Copy Markdown
Member

mpilgrem commented Feb 20, 2026

@horriblename, thanks! Don't worry about the CI for Linux with --alpine (that is waiting for Stack 3.9.3 to appear in Docker images).

I am wondering about the user experience if they accidently specify --semaphore when building with GHC < 9.8.

EDIT: --semaphore appears to have been introduced with Cabal-3.12.0.0, which was a boot package of GHC 9.10.1.

I had not paid attention to --semaphore, as the Cabal project has not yet documented its existence. However, I see that stack runghc -- Setup.hs build --help offers up the (largely unhelpful):

Flags for build:
...
--semaphore=SEMAPHORE          semaphore

I have opened an issue at Cabal's repository:

@mpilgrem
Copy link
Copy Markdown
Member

mpilgrem commented Feb 20, 2026

@horriblename, other than handling the Stack user experience when --semaphore=SEMAPHORE is not supported by Cabal, this looks good to me.

I've pushed a commit to better conform the online and in-app documentation to other documentation.

EDIT: I have also pushed a commit to ignore the flag with a warning when it is unsupported.

EDIT2: I have also pushed a commit to update a relevant unit test.

@mpilgrem
Copy link
Copy Markdown
Member

Tested on Windows 11 by building Stack itself: reduced from 83 s to 50 s (~40% less time).

@horriblename
Copy link
Copy Markdown
Contributor Author

horriblename commented Feb 21, 2026

EDIT: I have also pushed a commit to ignore the flag with a warning when it is unsupported.

I suppose there's nothing I need to do anymore? (thanks!)

@mpilgrem
Copy link
Copy Markdown
Member

@horriblename, many thanks. I'll likely merge this once the current CI passes.

@mpilgrem mpilgrem merged commit bac8a3d into commercialhaskell:master Feb 21, 2026
12 checks passed
@horriblename horriblename deleted the sema branch February 21, 2026 19:56
@mpilgrem
Copy link
Copy Markdown
Member

@horriblename, some disappointing news. I have learned that GHC's semaphore feature has major problems on Linux, because musl and non-musl semaphores are incompatible:

In the light of that, I am going to add something to Stack's documentation and label the feature as experimental.

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