<!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 05/06/2024 18:25,
        Pedro Lamarão wrote:</p>
      <p style="margin: 0px 0px 1.2em !important;"></p>
      <div class="markdown-here-exclude">
        <p></p>
        <blockquote type="cite" cite="mid:CAC8PkgEtB+Uw4RwPsLDBeO-By_UZC_5mBmje6DKnkKjC_N7PQQ@mail.gmail.com">Also,
          as this discussion suggests, what appears to be a universal
          interface may actually not be universal at all, and perhaps it
          is best to have libraries define their own allocator
          interfaces and see what emerges from that.</blockquote>
        <p></p>
      </div>
      <p style="margin: 0px 0px 1.2em !important;"></p>
      <p style="margin: 0px 0px 1.2em !important;">This is the thing we
        keep (re)learning when thinking about allocators in FFM. It is
        tempting to hardwire something to cater specific needs, but in
        reality specific libraries will work with very specific set of
        constraints, so our best bet is to somehow support such things
        to be built on top.</p>
      <p style="margin: 0px 0px 1.2em !important;">Now, this is
        orthogonal to the original question which was about advertizing
        zeroing properties of an allocator. What we’re seeing there is
        the (unavoidable) consequence of the (unavoidable) decision of
        not exposing (by default, at least) non-zeroing memory
        allocation primitives. E.g. it is easy to write a zeroing
        allocator if you have a non-zeroing one (as the latter is
        clearly more primitive than the former). But it wouldn’t have
        been safe to expose non-zeroing allocators in the front door
        allocation API. This set of constraints has led us to
        SegmentAllocator being the way it is today, where we make no
        assumption on what the “correct” behavior for
        SegmentAllocator::allocate should be. For Arenas that are
        provided by the JDK, the answer has to be “zeroing”. But users
        are (and should!) be able to define allocators/arenas that do no
        zero, if they have compelling use cases where zeroing would add
        too much overhead.</p>
      <p style="margin: 0px 0px 1.2em !important;">Should we have an
        extra method in SegmentAllocator which states what’s the zeroing
        policy for the allocator?</p>
      <p style="margin: 0px 0px 1.2em !important;">It’s certainly cheap
        to add, but:</p>
      <ul style="margin: 1.2em 0px;padding-left: 2em;">
        <li style="margin: 0.5em 0px;">users will forget to override
          that (esp. given SegmentAllocator is a functional interface)</li>
        <li style="margin: 0.5em 0px;">it only addresses <em>one</em>
          dimension in which a given allocator is special - are there
          others? (e.g. disjointness of returned segments, etc)</li>
        <li style="margin: 0.5em 0px;">another approach could be for
          clients that do want guaranteed zeroing to always call
          MS::fill, and rely on C2 to eliminate the redundant memset
          (after all Unsafe::setMemory is an intrinsics). This is hard
          to achieve, but would probably lead to a better long-term
          path, where we can stop worrying about duplicate zeroing (as
          happens for on-heap arrays). At this point having the
          predicate on <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;">SegmentAllocator</code>
          woudn’t add too much.</li>
      </ul>
      <p style="margin: 0px 0px 1.2em !important;">For all these
        reasons, I’m slightly against adding new methods to
        SegmentAllocator. Of course if this requests turned out to be a
        very common one I could reconsider of course.</p>
      <p style="margin: 0px 0px 1.2em !important;">Last, note that
        there’s a workaround, even though it might not look too pretty.
        Let’s say that you know that you only allocate 1024 bytes tops.
        Then you can do like so:</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;"><span class="hljs-function">MemorySegment <span class="hljs-title" style="color: rgb(153, 0, 0); font-weight: bold;">allocateAndZero</span><span class="hljs-params">(SegmentAllocator allocator, <span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: bold;">long</span> size)</span> </span>{
     <span class="hljs-class"><span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: bold;">class</span> <span class="hljs-title" style="color: rgb(153, 0, 0); font-weight: bold;color: rgb(68, 85, 136); font-weight: bold;">Holder</span> </span>{
            <span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: bold;">static</span> MemorySegment ZERO = MemorySegment.ofArray(<span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: bold;">new</span> <span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: bold;">byte</span>[<span class="hljs-number" style="color: rgb(0, 128, 128);">1024</span>]);
     }
     <span class="hljs-comment" style="color: rgb(153, 153, 136); font-style: italic;">// todo: check that `size < 1024`</span>
     <span class="hljs-keyword" style="color: rgb(51, 51, 51); font-weight: bold;">return</span> allocator.allocateFrom(JAVA_BYTE, Holder.ZERO, JAVA_BYTE, <span class="hljs-number" style="color: rgb(0, 128, 128);">0</span>, size);
}
</code></pre>
      <p style="margin: 0px 0px 1.2em !important;">Note that this uses <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;">allocateFrom</code>
        to allocate a memory segment of given size, and then bulk copy
        (a part of) the “zero segment” on top of it. This won’t perform
        double zeroing, because <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;">SegmentAllocator::allocateFrom</code>
        tries to avoid that also. The only difference is that we’re
        using “Unsafe::copyMemory” instead of “Unsafe::setMemory” to
        zero the memory (but both should perform roughly the same, as
        they are both intrinsics that support vectorized instructions).
        It’s not pretty, but maybe could be ok while we wait for deeper
        “duplicate zero memset” avoidance?</p>
      <p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
dGUgemVybyBtZW1zZXQiIGF2b2lkYW5jZT88YnI+PC9wPjxwPk1hdXJpemlvPGJyPjwvcD48cD48
YnI+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>