Swift Testing: still no way to disable _swift_setWillThrowHandler?

a long time ago, when Swift Testing first came out, it was discovered that many types of parsers could not run within a Swift Testing suite, because Swift Testing attached some runtime hook to Swift’s error propagation (_swift_setWillThrowHandler), and this resulted in an unacceptable slowdown in parser performance, due to the parser using Swift errors internally for its control flow.

this is virtually unnoticeable in normal production builds, as it gets optimized out, but in a Swift Testing test suite, parsing slows to a crawl, as errors will be thrown and caught inside the parser on virtually every UTF-8 byte. this can cause parsing to become hundreds of times slower.

in some unit tests, this is tolerable, because payloads are small enough that even a 200x regression in parser performance still completes in a reasonable amount of time. but for more complex tests that involve loading some real-world data (like a 5 MB JSON file), this is a total blocker preventing Swift Testing adoption.

in Swift 6.3, is still no way to safely disable _swift_setWillThrowHandler for a single test suite?

You can set the environment variable "SWT_SWIFT_ERROR_BACKTRACING_ENABLED" to "0" to disable this functionality in your process. There is no high-level per-test interface for it though as the hook function is global state.

2 Likes

that’s perfect, it’s exactly what i needed. i vaguely remembered something like this existing from a long-lost Slack conversation, but couldn’t find any trace of it anywhere even after prodding a few LLMs. thank you so much!

1 Like

I was going to look up that Slack convo and link to it here, but was worried it would read as passive-aggressive.

Note that our environment variable use is not considered stable API surface! Especially those starting with "SWT", which we will someday migrate to "SWIFT_TESTING".

1 Like

yes… this is part of why i despise Slack… so much information i vaguely recollect, that does not exist anywhere else, lost forever because of Salesforce’s 90-day access horizon…

2 Likes