swiftlang / swiftlang/swift-java

[JExtract] Consider optional control over enum copying

Open
#348 0 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature:jextract
Dominant language
Swift
Stars
1.2k
Forks
123
Avg merge
1d 7h
Merged PRs (30d)
16

Description

While implementing enums for JNI, we had to decide whether enum values should be copied on the Swift side or Java side. This had implications for whether we were able to use record or not to represent enum cases. We decided to eagerly copy the associated values from Java to Swift, instead of having a pointer to Swift. The latter approach would be more expensive for multiple accceses to the same field (as that would mean more JNI calls), but the former is more expensive for a single access (due to JNI reflection).

We could introduce options to customize this behaviour if the users wanted to have lazy copying for example (so just a pointer to Swift memory). It could be an annotation on the type.

Benchmarks

Here are some benchmarks we ran to compare Java vs Swift copying

Accessing two associated fields (no caching).
Benchmark                 Mode  Cnt     Score    Error  Units
EnumBenchmark.java_copy   avgt   30  1317,810 ± 16,443  ns/op
EnumBenchmark.swift_copy  avgt   30   326,424 ±  7,123  ns/op
Accessing two associated fields (cache JNI IDs in java_copy).
Benchmark                 Mode  Cnt    Score    Error  Units
EnumBenchmark.java_copy   avgt   30  505,537 ± 10,408  ns/op
EnumBenchmark.swift_copy  avgt   30  319,474 ±  5,692  ns/op
Accessing a single associated field (cache JNI IDs in java_copy).
Benchmark                 Mode  Cnt    Score    Error  Units
EnumBenchmark.java_copy   avgt   30  513,441 ±  8,825  ns/op
EnumBenchmark.swift_copy  avgt   30  437,693 ± 15,914  ns/op

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Begin with JExtract's enum generation path and the JNI enum implementation described in the issue. Read EnumBenchmark.java and compare the existing eager-copy behavior with the proposed configurable behavior. Done requires a settled option or annotation design and benchmarks or tests showing both modes.

Written by the indexing model from the issue text.

Assessment

Tech stack
java, swift
Domain
tooling
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
35/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.