Skip to content

jextract/jni: Overhaul generic specialization to prevent cache contamination#741

Merged
ktoso merged 9 commits into
swiftlang:mainfrom
sidepelican:refine_specialization
May 13, 2026
Merged

jextract/jni: Overhaul generic specialization to prevent cache contamination#741
ktoso merged 9 commits into
swiftlang:mainfrom
sidepelican:refine_specialization

Conversation

@sidepelican
Copy link
Copy Markdown
Contributor

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 as currentJavaIdentifiers.
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 ImportedFunc is typically used within a single context, the problem surfaced during my attempt to support label overloading in JNI. In that case, the same ImportedFunc is 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 ImportedFunc and instead have each specialized class maintain its own dedicated ImportedFunc instance.
By doing this, we can reduce many of the branches currently used for specialization within JNISwift2JavaGenerator.

Copy link
Copy Markdown
Collaborator

@ktoso ktoso left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like killing off the varsity and getters there; overall nice cleanup, thanks

@ktoso ktoso merged commit b9200ad into swiftlang:main May 13, 2026
64 checks passed
@sidepelican sidepelican deleted the refine_specialization branch May 13, 2026 07:02
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants