Skip to content

feat(minor): Disable jemalloc using package traits#347

Merged
supersonicbyte merged 10 commits intoordo-one:mainfrom
JaapWijnen:disable-jemalloc-using-package-traits
Mar 2, 2026
Merged

feat(minor): Disable jemalloc using package traits#347
supersonicbyte merged 10 commits intoordo-one:mainfrom
JaapWijnen:disable-jemalloc-using-package-traits

Conversation

@JaapWijnen
Copy link
Copy Markdown
Contributor

Description

This is a first attempt at enabling/disable jemalloc using package traits #316
This shows off what a trait based implementation could look like. But I'm not exactly sure what the expected behaviour would be on unsupported platforms. So that probably needs some more discussion.

How Has This Been Tested?

I performed a build and ran the tests with and without the trait enabled.
The build succeeds on Mac but running tests (with the trait enabled) fails with a segmentation error.
I'm not entirely sure where this is coming from but seems to fail once we add package-jemalloc to the package dependencies. Regardless of if it is actually used by package-benchmark this seems to crash running swift test --traits EnableJemalloc
This user experience should be improved ofcourse. Which I'm hoping to discuss in the PR.

Minimal checklist:

  • I have performed a self-review of my own code
  • I have added DocC code-level documentation for any public interfaces exported by the package
  • I have added unit and/or integration tests that prove my fix is effective or that my feature works

@JaapWijnen
Copy link
Copy Markdown
Contributor Author

This PR title should have been marked with feat: I can't seem to edit the PR title however. @hassila

@hassila
Copy link
Copy Markdown
Contributor

hassila commented Feb 9, 2026

Thanks @JaapWijnen - but we will soon remove the jemalloc dependency completely through #333 - although that will require a 6.3 toolchain - so maybe worth landing this for 6.0-6.2 support regardless - @JaapWijnen what is you earliest supported toolchain at this point? @supersonicbyte over to you

@supersonicbyte
Copy link
Copy Markdown
Contributor

Thanks @JaapWijnen - but we will soon remove the jemalloc dependency completely through #333 - although that will require a 6.3 toolchain - so maybe worth landing this for 6.0-6.2 support regardless - @JaapWijnen what is you earliest supported toolchain at this point? @supersonicbyte over to you

I agree this is probably worth landing for 6.0 - 6.2. Will do an extensive PR review soon.

@JaapWijnen
Copy link
Copy Markdown
Contributor Author

Oh that's great!
@hassila what do you mean exactly? The earliest supported toolchain of this approach? It still supports 5.9 but if you use 6.1 you can use the trait to enable jemalloc. (except it is segfaulting on mac which I'd like to understand why, but currently don't have a clue yet)

@hassila
Copy link
Copy Markdown
Contributor

hassila commented Feb 9, 2026

Yeah, when 6.3 lands we will bump this package to 6.1-6.2 for the old jemalloc and 6.3 for the new malloc interposer (that requires bug fixes from SPM that only exist in 6.3...)

@JaapWijnen
Copy link
Copy Markdown
Contributor Author

Ah I see. Awesome. Yeah we'll be moving to 6.3 as soon as it lands so that's great to hear :) Thanks!

@supersonicbyte supersonicbyte changed the title Disable jemalloc using package traits feat(minor): Disable jemalloc using package traits Feb 11, 2026
@supersonicbyte
Copy link
Copy Markdown
Contributor

@JaapWijnen everything looks good! just a quick comment. In order to fix the build for linux we should add @preconcurrency annotations in BenchmarkCommandPlugin.swift before the imports:

#if canImport(Darwin)
@preconcurrency import Darwin
#elseif canImport(Glibc)
@preconcurrency import Glibc
#elseif canImport(Musl)
@preconcurrency import Musl
#else
#error("Unsupported Platform")
#endif

Regarding the crash when running swift test --traits EnableJemalloc it a known issue:
jemalloc/jemalloc#2362
#60

It's a bug that happens when xctest runs with jemalloc.

@JaapWijnen
Copy link
Copy Markdown
Contributor Author

@JaapWijnen everything looks good! just a quick comment. In order to fix the build for linux we should add @preconcurrency annotations in BenchmarkCommandPlugin.swift before the imports:

@supersonicbyte Done!

@supersonicbyte
Copy link
Copy Markdown
Contributor

@JaapWijnen thanks!

I removed the Package.resolved in: #348. Could you please delete it also from the PR so we get the CI jobs passing.

One more thing, @hassila actually noticed that the trait EnableJemalloc will actually break for current users since the default behavior was that jemalloc was enabled by default and you have to pass the environment variable BENCHMARK_DISABLE_JEMALLOC in order to disable it.

In order to preserve backward compatibility could you flip the logic so:

  • we have the trait enabled by default
  • the user runs swift build/test --disable-default-traits in order to disable the trait

Additionally, we can also rename the trait to Jemalloc.

Sorry for the additional comment, I should have seen this in the first review pass.

@JaapWijnen
Copy link
Copy Markdown
Contributor Author

Good point! All done 👍

@JaapWijnen JaapWijnen force-pushed the disable-jemalloc-using-package-traits branch from 3b9d577 to 2566488 Compare February 13, 2026 23:50
@JaapWijnen
Copy link
Copy Markdown
Contributor Author

Anything else you need me to do? :) @supersonicbyte

@supersonicbyte
Copy link
Copy Markdown
Contributor

Anything else you need me to do? :) @supersonicbyte

I fixed the workflow files so our CI passes and also changed the README to reflect the changes, hope you don't mind! Thank you for your contribution, merging it right now!

@supersonicbyte supersonicbyte merged commit 1ce127f into ordo-one:main Mar 2, 2026
13 of 16 checks passed
@JaapWijnen
Copy link
Copy Markdown
Contributor Author

JaapWijnen commented Mar 2, 2026

Oh this is great. Thanks for taking that on! Now that this is merged. When do you think the breaking changes will be merged/tagged? (edit: Oh right that's only supported once 6.3 is release)

@JaapWijnen JaapWijnen deleted the disable-jemalloc-using-package-traits branch March 2, 2026 12:26
supersonicbyte added a commit that referenced this pull request Mar 3, 2026
## Description

With PR #347 we added trait support and we changed the
`#canImport(jemalloc)` to `#if Jemalloc`. Since the older toolchains
will execute the `Package@5.9` and in there we do not have a way to
enable the trait the code guarded with the #if will never compile, the
#else block will execute and we will get the stub implementation of the
malloc counter which return 0.

Quick fix is to revert back to use `#canImport`.

Related issue: #350

## How Has This Been Tested?

Please describe the tests that you ran to verify your changes. 

## Minimal checklist:

- [ ] I have performed a self-review of my own code 
- [ ] I have added `DocC` code-level documentation for any public
interfaces exported by the package
- [ ] I have added unit and/or integration tests that prove my fix is
effective or that my feature works
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

3 participants