<!DOCTYPE html><html><head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
  </head>
  <body>
    <div class="markdown-here-wrapper" data-md-url="" style="">
      <p style="margin: 0px 0px 1.2em !important;">On 30/10/2024 21:49,
        Maurizio Cimadamore wrote:</p>
      <p style="margin: 0px 0px 1.2em !important;"></p>
      <div class="markdown-here-exclude">
        <p></p>
        <blockquote type="cite" cite="mid:c95ed331-461e-46e2-a05f-1ec7e2ef0197@oracle.com">What
          I would like to do in the meantime is (when I have some
          cycles) to write a benchmark which tests memory segment in a
          loop with random access (e.g. using random offsets). And then
          evaluate the costs associated with the various approaches in
          isolation and see if anything pops up. I have a couple of
          ideas on how to possibly improve the story for the unbounded
          MAX_VALUE access -- but I'd like to see where we are w.r.t.
          random access in vanilla Java 24 first.
        </blockquote>
        <p></p>
      </div>
      <p style="margin: 0px 0px 1.2em !important;"></p>
      <p style="margin: 0px 0px 1.2em !important;">I find this email
        from a year ago or so interesting:</p>
      <p style="margin: 0px 0px 1.2em !important;"><a href="https://mail.openjdk.org/pipermail/panama-dev/2023-July/019478.html" class="moz-txt-link-freetext">https://mail.openjdk.org/pipermail/panama-dev/2023-July/019478.html</a></p>
      <p style="margin: 0px 0px 1.2em !important;">Some kind of issue <em>was
          indeed</em> identified in there. Surely, adding an extra <code style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace;margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); background-color: rgb(248, 248, 248); border-radius: 3px; display: inline;">< 0</code>
        check in all memory segment accessors shouldn’t do much, but the
        numbers suggested that the patched code was almost 2x as fast
        (although not as fast as Unsafe).</p>
      <p style="margin: 0px 0px 1.2em !important;">Then I had the other
        idea to adapt var handles (the one you are currently using):</p>
      <p style="margin: 0px 0px 1.2em !important;"><a href="https://mail.openjdk.org/pipermail/panama-dev/2023-July/019487.html" class="moz-txt-link-freetext">https://mail.openjdk.org/pipermail/panama-dev/2023-July/019487.html</a></p>
      <p style="margin: 0px 0px 1.2em !important;">Which, in retrospect,
        was perhaps going too far. Sure, the numbers in the synthetic
        benchmark looked very very good… but such an approach does add
        problems when it comes to the shape and simplicity of the
        generated code. There’s an allocation in every hot path, and a
        call to reinterpret (which is restricted, so there’s a check for
        that too). All that stuff is not normally on the critical path
        for memory access, but the trick of putting it inside the var
        handle adaptation code makes it part of the hot path, which I
        think solves some problems (it’s fast!) and creates some new
        ones (it’s brittle!).</p>
      <p style="margin: 0px 0px 1.2em !important;">Hence my suggestion
        to go back a little, and see what we can do to speed up access
        for a segment created with:</p>
      <pre style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace;font-size: 1em; line-height: 1.2em;margin: 1.2em 0px;"><code class="hljs language-java" style="font-size: 0.85em; font-family: Consolas, Inconsolata, Courier, monospace;margin: 0px 0.15em; padding: 0px 0.3em; white-space: pre-wrap; border: 1px solid rgb(234, 234, 234); background-color: rgb(248, 248, 248); border-radius: 3px; display: inline;white-space: pre; overflow: auto; border-radius: 3px; border: 1px solid rgb(204, 204, 204); padding: 0.5em 0.7em; display: block !important;display: block; overflow-x: auto; padding: 0.5em; color: rgb(51, 51, 51); background: rgb(248, 248, 248); -moz-text-size-adjust: none;">MemorySegment.NULL.reinterpret(Long.MAX_VALUE)
</code></pre>
      <p style="margin: 0px 0px 1.2em !important;">(which, as Ron
        correctly points out, might not mean <em>exactly as fast as
          Unsafe</em>)</p>
      <p style="margin: 0px 0px 1.2em !important;">Maurizio</p>
      <div title="MDH:PHA+PGJyPjwvcD48ZGl2IGNsYXNzPSJtb3otY2l0ZS1wcmVmaXgiPk9uIDMwLzEwLzIwMjQgMjE6
NDksIE1hdXJpemlvIENpbWFkYW1vcmUgd3JvdGU6PGJyPjwvZGl2PjxibG9ja3F1b3RlIHR5cGU9
ImNpdGUiIGNpdGU9Im1pZDpjOTVlZDMzMS00NjFlLTQ2ZTItYTA1Zi0xZWM3ZTJlZjAxOTdAb3Jh
Y2xlLmNvbSI+V2hhdCBJ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0aGluayBzb2x2ZXMgc29tZSBwcm9ibGVtcyAoaXQn
cyBmYXN0ISkgYW5kIGNyZWF0ZXMgc29tZSBuZXcgb25lcyAoaXQncyBicml0dGxlISkuPC9wPjxw
PkhlbmNlIG15IHN1Z2dlc3Rpb24gdG8gZ28gYmFjayBhIGxpdHRsZSwgYW5kIHNlZSB3aGF0IHdl
IGNhbiBkbyB0byBzcGVlZCB1cCBhY2Nlc3MgZm9yIGEgc2VnbWVudCBjcmVhdGVkIHdpdGg6PC9w
PjxwPmBgYGphdmE8YnI+TWVtb3J5U2VnbWVudC5OVUxMLnJlaW50ZXJwcmV0KExvbmcuTUFYX1ZB
TFVFKTxicj5gYGA8YnI+PC9wPjxwPih3aGljaCwgYXMgUm9uIGNvcnJlY3RseSBwb2ludHMgb3V0
LCBtaWdodCBub3QgbWVhbiBfZXhhY3RseSBhcyBmYXN0IGFzIFVuc2FmZV8pPC9wPjxwPk1hdXJp
emlvPGJyPjwvcD48cD48YnI+PC9wPg==" style="height:0;width:0;max-height:0;max-width:0;overflow:hidden;font-size:0em;padding:0;margin:0;">​</div>
    </div>
  </body>
</html>