Detect double destruction and generalize $createDestroyFunction#760
Merged
Conversation
Contributor
Author
|
Gradle seems unhealthy again today 🤒 |
Collaborator
|
Yeah that's what we get for running 60 jobs... I'll work on getting less jobs to run soon; but we'll have to cut coverage of swift versions etc. At least less flaky fails then |
ktoso
approved these changes
May 20, 2026
| private final MemorySegment memoryAddress; | ||
| private final SwiftAnyType type; | ||
| private final Runnable markAsDestroyed; | ||
| private final AtomicBoolean statusDestroyedFlag; |
Collaborator
There was a problem hiding this comment.
We could move to atomic field updater here, but I'm happy to do this separately
ktoso
added a commit
to ktoso/swift-java
that referenced
this pull request
May 20, 2026
We can use an atomic field updater and avoid the AtomicBoolean allocation this way. This keeps semantics added by @sidepelican in swiftlang#760 just avoids the one heap alloc per SwiftInstance
ktoso
added a commit
to ktoso/swift-java
that referenced
this pull request
May 20, 2026
The atomic boolean was one extra heap allocation which we now avoid -- we just create one cleanup and inside it have an int, rather than creating the Bool separately and passing it to the cleanup. AFAICS this will avoid one alloc, which isn't much, but can add up since we do it for every wrapped swift object. This keeps semantics added by @sidepelican in swiftlang#760 just avoids the one heap alloc per SwiftInstance
ktoso
added a commit
that referenced
this pull request
May 20, 2026
…761) The atomic boolean was one extra heap allocation which we now avoid -- we just create one cleanup and inside it have an int, rather than creating the Bool separately and passing it to the cleanup. AFAICS this will avoid one alloc, which isn't much, but can add up since we do it for every wrapped swift object. This keeps semantics added by @sidepelican in #760 just avoids the one heap alloc per SwiftInstance
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
This PR introduces two improvements to the destruction process of Swift instances.
1. Detect double destruction
Currently, if a double destruction occurs due to an implementation error, the process simply crashes without any diagnostics.
To throw an error, this PR passes the
statusDestroyedFlagdirectly to the cleanup instance instead ofmarkAsDestroyed.2. Generalize
$createDestroyFunctionPreviously, a unique
destroyfunction was generated for each individual class.However, we have transitioned to a shared invocation of
SwiftObjects.destroy(in #582 ).Since there is no longer a reason to generate a separate
$createDestroyFunctionimplementation for every single class, this PR provides a default implementation to minimize the footprint of the generated code.Note: Unlike the FFM side,
$createDestroyFunctionis still required here because certain classes still implement their own customdestroylogic.