<div dir="ltr"><div>Hi</div><div><br></div><div>First, I'm not sure if I am at the right place, let me know if I'm wrong.<br></div><div><br></div><div>I was trying to compare different sections of Memory and used MemorySegment.mismatch.</div><div>My program got unexpected results and failed.</div><div><br></div><div>The issue boiled down to that I compared the same MemorySegment with itself, but on different range. Example program, which demonstrates the issue:</div><div><br></div><div><div style="background-color:rgb(255,255,255);color:rgb(8,8,8)"><pre style="font-family:"JetBrains Mono",monospace;font-size:9.8pt"><span style="color:rgb(0,51,179)">public class </span><span style="color:rgb(0,0,0)">UnexpectedMismatchBehavior </span>{<br>    <span style="color:rgb(0,51,179)">public static void </span><span style="color:rgb(0,98,122)">main</span>(<span style="color:rgb(0,0,0)">String</span>[] <span style="color:rgb(0,0,0)">args</span>) {<br>        <span style="color:rgb(0,51,179)">byte</span>[] <span style="color:rgb(0,0,0)">exampleData </span>= <span style="color:rgb(0,51,179)">new byte</span>[]{<span style="color:rgb(23,80,235)">1</span>, <span style="color:rgb(23,80,235)">2</span>, <span style="color:rgb(23,80,235)">3</span>, <span style="color:rgb(23,80,235)">4</span>,<br>                -<span style="color:rgb(23,80,235)">1</span>, -<span style="color:rgb(23,80,235)">1</span>, -<span style="color:rgb(23,80,235)">1</span>, -<span style="color:rgb(23,80,235)">1</span>,<br>                <span style="color:rgb(23,80,235)">1</span>, <span style="color:rgb(23,80,235)">2</span>, <span style="color:rgb(23,80,235)">8</span>, <span style="color:rgb(23,80,235)">8</span>,<br>                -<span style="color:rgb(23,80,235)">1</span>, -<span style="color:rgb(23,80,235)">1</span>, -<span style="color:rgb(23,80,235)">1</span>, -<span style="color:rgb(23,80,235)">1</span>};<br>        <span style="color:rgb(140,140,140);font-style:italic">// In the real world, this is a large memory mapped file<br></span><span style="color:rgb(140,140,140);font-style:italic">        </span><span style="color:rgb(0,0,0)">MemorySegment memory </span>= <span style="color:rgb(0,0,0)">MemorySegment</span>.<span style="font-style:italic">ofArray</span>(<span style="color:rgb(0,0,0)">exampleData</span>);<br>        <span style="color:rgb(140,140,140);font-style:italic">// In the real app these are of course dynamic<br></span><span style="color:rgb(140,140,140);font-style:italic">        </span><span style="color:rgb(0,51,179)">int </span><span style="color:rgb(0,0,0)">searchTermOffset </span>= <span style="color:rgb(23,80,235)">0</span>;<br>        <span style="color:rgb(0,51,179)">int </span><span style="color:rgb(0,0,0)">len </span>= <span style="color:rgb(23,80,235)">4</span>;<br><br>        <span style="color:rgb(0,51,179)">int </span><span style="color:rgb(0,0,0)">checkOffset </span>= <span style="color:rgb(23,80,235)">8</span>;<br><br>        <span style="color:rgb(0,51,179)">long </span><span style="color:rgb(0,0,0)">missMatch </span>= <span style="color:rgb(0,0,0)">MemorySegment</span>.<span style="font-style:italic">mismatch</span>(<span style="color:rgb(0,0,0)">memory</span>, <span style="color:rgb(0,0,0)">searchTermOffset</span>, <span style="color:rgb(0,0,0)">searchTermOffset</span>+<span style="color:rgb(0,0,0)">len</span>, <span style="color:rgb(0,0,0)">memory</span>, <span style="color:rgb(0,0,0)">checkOffset</span>, <span style="color:rgb(0,0,0)">checkOffset</span>+<span style="color:rgb(0,0,0)">len</span>);<br>        <span style="color:rgb(0,51,179)">int </span><span style="color:rgb(0,0,0)">debug </span>= <span style="color:rgb(0,0,0)">Arrays</span>.<span style="font-style:italic">mismatch</span>(<span style="color:rgb(0,0,0)">exampleData</span>,<span style="color:rgb(0,0,0)">searchTermOffset</span>, <span style="color:rgb(0,0,0)">searchTermOffset</span>+<span style="color:rgb(0,0,0)">len</span>,<span style="color:rgb(0,0,0)">exampleData</span>, <span style="color:rgb(0,0,0)">checkOffset</span>, <span style="color:rgb(0,0,0)">checkOffset</span>+<span style="color:rgb(0,0,0)">len</span>);<br>        <span style="color:rgb(0,51,179)">if</span>(<span style="color:rgb(0,0,0)">missMatch </span>== -<span style="color:rgb(23,80,235)">1</span>){<br>            <span style="color:rgb(0,0,0)">System</span>.<span style="color:rgb(135,16,148);font-style:italic">out</span>.println(<span style="color:rgb(6,125,23)">"Unexpected match!!!"</span>);<br>            <span style="color:rgb(0,0,0)">System</span>.<span style="color:rgb(135,16,148);font-style:italic">out</span>.println(<span style="color:rgb(6,125,23)">"Array.mismatch gives: " </span>+ <span style="color:rgb(0,0,0)">debug</span>);<br>            <span style="color:rgb(0,51,179)">throw new </span>Exception(<span style="color:rgb(6,125,23)">"What, should not match?"</span>);<br>        } <span style="color:rgb(0,51,179)">else</span>{<br>            <span style="color:rgb(0,0,0)">System</span>.<span style="color:rgb(135,16,148);font-style:italic">out</span>.println(<span style="color:rgb(6,125,23)">"Missmatch, as expected"</span>);<br>        }<br>    }<br>}<br></pre></div></div><div><br></div><div>Overall, I would expect the same behavior as Arrays.missmatch.</div><div><br></div><div>The cuprid seems this check in the implementation: <br><div style="background-color:rgb(255,255,255);color:rgb(8,8,8)"><pre style="font-family:"JetBrains Mono",monospace;font-size:9.8pt"><span style="color:rgb(0,0,0)">AbstractMemorySegmentImpl.mismatch(...)<br>...<br></span></pre><div style="background-color:rgb(255,255,255);color:rgb(8,8,8)"><pre style="font-family:"JetBrains Mono",monospace;font-size:9.8pt"><span style="color:rgb(0,51,179)">if </span>(<span style="color:rgb(0,0,0)">dstImpl </span>== <span style="color:rgb(0,0,0)">srcImpl</span>) {<br>    <span style="color:rgb(0,0,0)">srcImpl</span>.checkValidState();<br>    <span style="color:rgb(0,51,179)">return </span>-<span style="color:rgb(23,80,235)">1</span>;<br>}<br><br></pre><pre style="font-family:"JetBrains Mono",monospace;font-size:9.8pt">The current behavior is in my opinion problementatic.<br>I consider it a bug. Because I get the same return value as if there would be no match, the program<br></pre><pre style="font-family:"JetBrains Mono",monospace;font-size:9.8pt">continues on with the wrong value.<br><br></pre><pre style="font-family:"JetBrains Mono",monospace;font-size:9.8pt">What would instead expect:<br></pre><pre style="font-family:"JetBrains Mono",monospace;font-size:9.8pt">- Best would be if it behaves like Arrays.missmatch<br></pre><pre style="font-family:"JetBrains Mono",monospace;font-size:9.8pt">- Second best option: It throws an exception.<br></pre><pre style="font-family:"JetBrains Mono",monospace;font-size:9.8pt">- Last option: Mention the limitation in the JavaDoc: But still many bugs will be written because <br></pre><pre style="font-family:"JetBrains Mono",monospace;font-size:9.8pt">  the 'fine' print isn't read.<br><br></pre><pre style="font-family:"JetBrains Mono",monospace;font-size:9.8pt">Best regards<br></pre><pre style="font-family:"JetBrains Mono",monospace;font-size:9.8pt">Roman Stoffel<br></pre><pre style="font-family:"JetBrains Mono",monospace;font-size:9.8pt"><br></pre></div></div></div></div>