Skip to content

Commit 5284c70

Browse files
authored
Restore inadvertently reverted fallback event handler optimization. (#1607)
When I reverted our `RawSpan` adoption in #1541, I inadvertently reverted the optimizations I'd made to Jerry's interop work that reduce the overhead of calling the fallback event handler more than once. This PR restores those optimizations. ### Checklist: - [x] Code and documentation should follow the style of the [Style Guide](https://github.com/apple/swift-testing/blob/main/Documentation/StyleGuide.md). - [x] If public symbols are renamed or modified, DocC references should be updated.
1 parent 3cf162e commit 5284c70

File tree

1 file changed

+21
-19
lines changed

1 file changed

+21
-19
lines changed

Sources/Testing/Events/Event+FallbackEventHandler.swift

Lines changed: 21 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -109,11 +109,6 @@ extension Event {
109109
}
110110

111111
#if !SWT_NO_INTEROP
112-
/// The fallback event handler that was installed in the current test process.
113-
private static let _activeFallbackEventHandler: SWTFallbackEventHandler? = {
114-
_swift_testing_getFallbackEventHandler()
115-
}()
116-
117112
/// The fallback event handler to install when Swift Testing is the active
118113
/// testing library.
119114
private static let _ourFallbackEventHandler: SWTFallbackEventHandler = {
@@ -179,32 +174,39 @@ extension Event {
179174
/// `false`.
180175
borrowing func postToFallbackEventHandler(in context: borrowing Context) -> Bool {
181176
#if !SWT_NO_INTEROP
182-
guard let fallbackEventHandler = Self._activeFallbackEventHandler else {
183-
return false
177+
return Self._postToFallbackEventHandler?(self, context) != nil
178+
#else
179+
return false
180+
#endif
181+
}
182+
183+
/// The implementation of ``postToFallbackEventHandler(in:)`` that actually
184+
/// invokes the installed fallback event handler.
185+
///
186+
/// If there was no fallback event handler installed, or if the installed
187+
/// handler belongs to the testing library (and so shouldn't be called by us),
188+
/// the value of this property is `nil`.
189+
private static let _postToFallbackEventHandler: Event.Handler? = {
190+
guard let fallbackEventHandler = _swift_testing_getFallbackEventHandler() else {
191+
return nil
184192
}
185193

186-
let isOurInstalledHandler =
187-
castCFunction(fallbackEventHandler, to: UnsafeRawPointer.self)
188-
== castCFunction(Self._ourFallbackEventHandler, to: UnsafeRawPointer.self)
189-
guard !isOurInstalledHandler else {
194+
let fallbackEventHandlerAddress = castCFunction(fallbackEventHandler, to: UnsafeRawPointer.self)
195+
let ourFallbackEventHandlerAddress = castCFunction(Self._ourFallbackEventHandler, to: UnsafeRawPointer.self)
196+
if fallbackEventHandlerAddress == ourFallbackEventHandlerAddress {
190197
// The fallback event handler belongs to Swift Testing, so we don't want
191198
// to call it on our own behalf.
192-
return false
199+
return nil
193200
}
194201

195202
// Encode the event as JSON and pass it to the handler.
196-
let encodeAndInvoke = ABI.CurrentVersion.eventHandler(encodeAsJSONLines: false) { recordJSON in
203+
return ABI.CurrentVersion.eventHandler(encodeAsJSONLines: false) { recordJSON in
197204
fallbackEventHandler(
198205
String(describing: ABI.CurrentVersion.versionNumber),
199206
recordJSON.baseAddress!,
200207
recordJSON.count,
201208
nil
202209
)
203210
}
204-
encodeAndInvoke(self, context)
205-
return true
206-
#else
207-
return false
208-
#endif
209-
}
211+
}()
210212
}

0 commit comments

Comments
 (0)