Uploaded image for project: 'Code Tools'
  1. Code Tools
  2. CODETOOLS-7903587

Revisit inlining of struct layouts into other layouts/descriptors

XMLWordPrintable

    • Icon: Bug Bug
    • Resolution: Fixed
    • Icon: P3 P3
    • None
    • None
    • tools
    • None

      Consider this code:

      ```
      struct Point2d {
          double x;
          double y;
      };

      struct Rectangle2d {
          struct Point2d topLeft;
          struct Point2d bottomRight;
      };

      double distance(struct Point2d point);
      ```

      For this, jextract generates the following code:

      ```
      public class Point2d {

          public static MemoryLayout $LAYOUT() {
              return constants$0.const$0;
          }
          ...
      }
      ```
      public class Rectangle2d {

          public static MemoryLayout $LAYOUT() {
              return constants$0.const$3;
          }
      }
      ```

      ```
      public class foo_h {

          public static MethodHandle distance$MH() {
              return RuntimeHelper.requireNonNull(constants$0.const$5,"distance");
          }
          /**
           * {@snippet :
           * double distance(struct Point2d point);
           * }
           */
          public static double distance(MemorySegment point) {
              var mh$ = distance$MH();
              try {
                  return (double)mh$.invokeExact(point);
              } catch (Throwable ex$) {
                  throw new AssertionError("should not reach here", ex$);
              }
          }
      }
      ```

      ```
      final class constants$0 {

          // Suppresses default constructor, ensuring non-instantiability.
          private constants$0() {}
          static final StructLayout const$0 = MemoryLayout.structLayout(
              JAVA_DOUBLE.withName("x"),
              JAVA_DOUBLE.withName("y")
          ).withName("Point2d");
          static final VarHandle const$1 = constants$0.const$0.varHandle(MemoryLayout.PathElement.groupElement("x"));

          static final StructLayout const$3 = MemoryLayout.structLayout(
              MemoryLayout.structLayout(
                  JAVA_DOUBLE.withName("x"),
                  JAVA_DOUBLE.withName("y")
              ).withName("topLeft"),
              MemoryLayout.structLayout(
                  JAVA_DOUBLE.withName("x"),
                  JAVA_DOUBLE.withName("y")
              ).withName("bottomRight")
          ).withName("Rectangle2d");

      static final FunctionDescriptor const$4 = FunctionDescriptor.of(JAVA_DOUBLE,
              MemoryLayout.structLayout(
                  JAVA_DOUBLE.withName("x"),
                  JAVA_DOUBLE.withName("y")
              ).withName("Point2d")
          );
          static final MethodHandle const$5 = RuntimeHelper.downcallHandle(
              "distance",
              constants$0.const$4
          );

      }
      ```

      Note how the layout for `Point2d` is inlined in all the layouts and descriptors that refer to it (e.g. `Rectangle2d`, and `distance`). This makes the generated code less readable (and also less optimal), as layouts should (ideally) be referred to by-name.

            mcimadamore Maurizio Cimadamore
            mcimadamore Maurizio Cimadamore
            Votes:
            0 Vote for this issue
            Watchers:
            2 Start watching this issue

              Created:
              Updated:
              Resolved: