<html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <p>Hi Gavin,<br>
      we have some documents on the foreign memory API here:</p>
    <p><a class="moz-txt-link-freetext" href="https://github.com/openjdk/panama-foreign/blob/foreign-memaccess%2Babi/doc/panama_memaccess.md">https://github.com/openjdk/panama-foreign/blob/foreign-memaccess%2Babi/doc/panama_memaccess.md</a></p>
    <p>They have not been updated against the latest version of the API,
      but should be relatively in good shape (at least good enough to
      get started).</p>
    <p>
      <blockquote type="cite">
        <div>The API is a fair bit wordier, but that's the price you pay
          in exchange for greater expressiveness and flexibility I
          suppose.</div>
      </blockquote>
      As for wordiness, I don't think the API is significantly more
      verbose than the ByteBuffer API. In fact, at a glance the methods
      on MemorySegment and ByteBuffer are quite similar, but there are
      two differences:</p>
    <p>* when you allocate native memory you _have_ to pass an extra
      argument, the temporal bounds of the memory segments
      (MemorySession), which signals when the segment memory is ready to
      be released<br>
      * the access methods, MemorySegment::get/set are parameterized by
      a layout argument, so instead of `buffer.getInt(offset)`, you need
      `segment.get(JAVA_INT, offset)`. This will give more extensibility
      (e.g. add support for more carriers, or value types, at a later
      point) as well as more control (e.g. alignment)<br>
    </p>
    <p>In my experience, and also looking at code written by developers
      that migrated projects from ByteBuffer, the MemorySegment API
      seems more or less on par with ByteBuffer in terms of verbosity.
      There are var handles which you can use (as pointed out elsewhere
      in this thread), and, depending on the use case, they can make
      your life simpler. But you don't _have_ to use them unless you
      want to.</p>
    <p>I hope this helps. From the looks of it, the Foreign Memory API
      seems like a great fit for the project you are working on.<br>
    </p>
    <p>Maurizio<br>
    </p>
    <p><br>
    </p>
    <div class="moz-cite-prefix">On 20/08/2022 18:35, Gavin Ray wrote:<br>
    </div>
    <blockquote type="cite" cite="mid:CAFtvWZPh4EmDetE9N+_RMNaOYdh6TwjZyYdKU_R2kPw-13+9zA@mail.gmail.com">
      
      <div dir="ltr">
        <div dir="ltr">Hiya, I hope it's okay that I ask questions here
          (if not, please direct me elsewhere)
          <div><br>
          </div>
          <div>I tried to ask on StackOverflow without much luck, and
            there aren't many resources</div>
          <div>available at the moment to read about the Foreign Memory
            API so I'm reaching out here.</div>
          <div><br>
          </div>
          <div>I'm implementing a basic database on the JVM and am
            trying to understand the</div>
          <div>technical implications of using either ByteBuffer or
            MemorySegment for heap page management and in-memory buffer
            allocation.</div>
          <div><br>
          </div>
          <div>Details are in this thread:</div>
          <div><a href="https://stackoverflow.com/questions/73423726/java-performance-of-bytebuffer-versus-jdk-incubator-foreign-panama-foreign-me" moz-do-not-send="true">database - Java: Performance of
              ByteBuffer versus jdk.incubator.foreign (Panama) Foreign
              Memory methods (MemoryLayout/Segment) - Stack Overflow</a></div>
          <div><br>
          </div>
          <div>Would appreciate any information folks would be willing
            to share =)</div>
          <div><br>
          </div>
          <div>Thank you,</div>
          <div>Gavin Ray</div>
          <div><br>
          </div>
          <div>(By the way, I'm not subscribed to the mailing list, will
            I be able to reply to responses?)</div>
        </div>
      </div>
    </blockquote>
  </body>
</html>