jextract/jni: Overhaul generic specialization to prevent cache contamination#741
Merged
Merged
Conversation
ktoso
approved these changes
May 13, 2026
Collaborator
ktoso
left a comment
There was a problem hiding this comment.
I like killing off the varsity and getters there; overall nice cleanup, thanks
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.
Motivation
This PR refactors the generic specialization logic to address a cache contamination issue in
JNISwift2JavaGenerator.The results of
translatedDecl(for:)are not constant; they change based on the internal state (Context), such ascurrentJavaIdentifiers.If
translatedDecl(for:)is called after the context has changed, it may return incorrect results.While this hasn't been a major issue so far because
ImportedFuncis typically used within a single context, the problem surfaced during my attempt to support label overloading in JNI. In that case, the sameImportedFuncis reused across multiple specialized contexts, making the cache contamination problematic.Solution
Ideally, the correct approach would be to clear the cache whenever the context changes.
However, with the current implementation, I think it difficult to accurately track these context changes.
I am not confident that I can correctly identify and cover all states that are implicitly relied upon.
Instead, I have decided to change the implementation strategy for specialization.
I will stop reusing the same
ImportedFuncand instead have each specialized class maintain its own dedicatedImportedFuncinstance.By doing this, we can reduce many of the branches currently used for specialization within
JNISwift2JavaGenerator.