swiftlang / swiftlang/swift-java

jextract-swift: allow $direct access to properties when we know their offset/layout, and no side effects

Open
#30 2 comments 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

feature:jextract good first issue help wanted performance
Dominant language
Swift
Stars
1.2k
Forks
123
Avg merge
1d 7h
Merged PRs (30d)
16

Description

This is a follow up to the "call the accessors" https://github.com/swiftlang/swift-java/issues/24 part of properties.

We may consider implementing direct field access when we'd know the layout / offset / size of a field. These are direct memory access so could be more efficient than calling the Swift accessor methods, however they can only be safe to call when the fields have no.

This is likely a "later" task, as it'd only be a specific performance optimization vs. accessing by accessors, but it's worth exploring eventually.


    // --------------------------------------------------------------------------------------------------------
    // ==== len

    private static final OfLong len$LAYOUT = (OfLong)$LAYOUT.select(groupElement("len"));

    private static class len$property {
        public static final FunctionDescriptor DESC_GET = FunctionDescriptor.of(
                /* -> */ManualJavaKitExample.SWIFT_INT,
                /* self = */ ManualJavaKitExample.SWIFT_POINTER
        );
        public static final FunctionDescriptor DESC_SET = FunctionDescriptor.ofVoid(
                /* self = */ ManualJavaKitExample.SWIFT_POINTER,
                ManualJavaKitExample.SWIFT_INT
        );

        private static final String BASE_NAME = "$s14JavaKitExample12MySwiftClassC3lenSiv";
        public static final MemorySegment ADDR_GET = ManualJavaKitExample.findOrThrow(BASE_NAME + "g");
        public static final MemorySegment ADDR_SET = ManualJavaKitExample.findOrThrow(BASE_NAME + "s");

        public static final MethodHandle HANDLE_GET = Linker.nativeLinker().downcallHandle(ADDR_GET, DESC_GET);
        public static final MethodHandle HANDLE_SET = Linker.nativeLinker().downcallHandle(ADDR_SET, DESC_SET);
    }

    public static final OfLong len$layout() {
        return len$LAYOUT;
    }

    private static final long len$OFFSET = 8; // FIXME: we don't know yet

    public static final long len$offset() {
        return len$OFFSET;
    }

    public static FunctionDescriptor len$get$descriptor() {
        return len$property.DESC_GET;
    }
    public static MethodHandle len$get$handle() {
        return len$property.HANDLE_GET;
    }
    public static MemorySegment len$get$address() {
        return len$property.ADDR_GET;
    }

    public static long getLen(MemorySegment self) {
        var mh$ = len$property.HANDLE_GET;
        try {
            if (TRACE_DOWNCALLS) {
                traceDowncall("len$getter", self);
            }
            return (long) mh$.invokeExact(self);
        } catch (Throwable ex$) {
            throw new AssertionError("should not reach here", ex$);
        }
    }

    public static void getLen$direct(MemorySegment self) {
        // FIXME: we don't know the right offset yet, we need to get told in the .swiftinterface
        self.get(len$LAYOUT, len$OFFSET);
    }

    public static long getLen$direct(MemorySegment self) {
        return self.get(len$LAYOUT, len$OFFSET);
    }


    public static FunctionDescriptor len$set$descriptor() {
        return len$property.DESC_SET;
    }
    public static MethodHandle len$set$handle() {
        return len$property.HANDLE_SET;
    }
    public static MemorySegment len$set$address() {
        return len$property.ADDR_SET;
    }


    /**
     * Setter for field:
     * {@snippet lang = Swift :
     * var len: Int { set }
     * }
     */
    public static void setLen(MemorySegment self, long fieldValue) {
        var mh$ = len$property.HANDLE_SET;
        try {
            if (TRACE_DOWNCALLS) {
                traceDowncall("len$setter", self, fieldValue);
            }
            mh$.invokeExact(self, fieldValue);
        } catch (Throwable ex$) {
            throw new AssertionError("should not reach here", ex$);
        }
    }
    public static void setLen$direct(MemorySegment self, long fieldValue) {
        // FIXME: we don't know the right offset yet, we need to get told in the .swiftinterface
        self.set(len$LAYOUT, len$OFFSET, fieldValue);
    }

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

Read the jextract-swift property-generation entry point and the .swiftinterface information it consumes; the issue's example shows the generated Java API shape to compare against. Completion means direct getter/setter access is emitted only when layout, offset, size, and side-effect safety are known, with accessor behavior retained otherwise.

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
30/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.