jbonofre commented on code in PR #1334:
URL: https://github.com/apache/arrow-java/pull/1334#discussion_r4207177122


##########
memory/memory-ffm/src/main/java/org/apache/arrow/memory/ffm/FfmMemoryAccessor.java:
##########
@@ -0,0 +1,181 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.arrow.memory.ffm;
+
+import java.lang.foreign.MemorySegment;
+import java.lang.foreign.ValueLayout;
+import java.nio.ByteBuffer;
+import org.apache.arrow.memory.util.MemoryUtilAccessor;
+
+/**
+ * {@link MemoryUtilAccessor} backed by {@code java.lang.foreign} ({@link 
MemorySegment} and the
+ * foreign {@link java.lang.foreign.Linker}). Does not use {@code 
sun.misc.Unsafe} or reflection
+ * into {@code java.nio} internals, so it requires neither {@code --add-opens} 
nor {@code
+ * sun.misc.Unsafe} availability.
+ *
+ * <p><b>Required JVM flag.</b> This accessor calls the restricted methods 
{@link
+ * MemorySegment#reinterpret(long)} and {@link 
java.lang.foreign.Linker#downcallHandle}, the latter
+ * for {@code malloc} and {@code free}. That is a real, current requirement, 
not a caveat that this
+ * module already handles for you: classpath (unnamed-module) consumers must 
pass {@code
+ * --enable-native-access=ALL-UNNAMED} and module-path consumers must pass 
{@code
+ * --enable-native-access=org.apache.arrow.memory.ffm} on the JVM command 
line. Without it the JVM
+ * prints a warning on every restricted call today, and a future JDK that 
enables restricted-method
+ * enforcement by default will turn that into a hard {@link 
IllegalCallerException}. The {@code
+ * Enable-Native-Access: ALL-UNNAMED} entry in this module's jar manifest does 
<em>not</em> cover
+ * this: the JVM only honours that attribute in the manifest of the jar it was 
launched with via
+ * {@code java -jar}, never for a jar that is merely a classpath dependency.
+ *
+ * <p>{@link #allocateMemory}/{@link #freeMemory} call {@code malloc}/{@code 
free} directly, like
+ * the {@code sun.misc.Unsafe}-backed accessor, so {@link #freeMemory} accepts 
any address returned
+ * by {@code malloc}.
+ */
+public final class FfmMemoryAccessor implements MemoryUtilAccessor {
+
+  public static final MemoryUtilAccessor INSTANCE = new FfmMemoryAccessor();
+
+  private FfmMemoryAccessor() {}
+
+  private static MemorySegment segment(long address, long byteSize) {

Review Comment:
   Every scalar `get`/`put` goes through this helper, which creates a new 
`MemorySegment` via `reinterpret`. `getByteBufferAddress` also builds a 
duplicate buffer and a segment per call. These are hot paths for vectors.
   
   Just curious if you have JMH numbers agains the Unsafe accessor? I would 
like to know whether escape analysis removes the allocations in practice.



##########
memory/memory-ffm/src/main/java/org/apache/arrow/memory/ffm/NativeMemory.java:
##########
@@ -0,0 +1,83 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.arrow.memory.ffm;
+
+import java.lang.foreign.FunctionDescriptor;
+import java.lang.foreign.Linker;
+import java.lang.foreign.MemorySegment;
+import java.lang.foreign.ValueLayout;
+import java.lang.invoke.MethodHandle;
+
+/**
+ * Native memory from the C library's {@code malloc} and {@code free}, called 
through the foreign
+ * {@link Linker}.
+ *
+ * <p>An {@link java.lang.foreign.Arena} per buffer is avoided on purpose: 
closing a shared arena
+ * handshakes with every JVM thread, which makes freeing orders of magnitude 
slower than {@code
+ * sun.misc.Unsafe}, and its allocations are always zeroed.
+ */
+final class NativeMemory {
+
+  private static final Linker LINKER = Linker.nativeLinker();
+  private static final MethodHandle MALLOC =
+      LINKER.downcallHandle(
+          find("malloc"), FunctionDescriptor.of(ValueLayout.ADDRESS, 
ValueLayout.JAVA_LONG));
+  private static final MethodHandle FREE =
+      LINKER.downcallHandle(find("free"), 
FunctionDescriptor.ofVoid(ValueLayout.ADDRESS));
+
+  private NativeMemory() {}
+
+  private static MemorySegment find(String name) {
+    return LINKER
+        .defaultLookup()
+        .find(name)
+        .orElseThrow(() -> new IllegalStateException("C library function not 
found: " + name));
+  }
+
+  /**
+   * Returns the address of {@code bytes} of uninitialized native memory.
+   *
+   * @throws OutOfMemoryError if {@code malloc} cannot allocate them, like 
{@code
+   *     sun.misc.Unsafe#allocateMemory}
+   */
+  static long allocate(long bytes) {

Review Comment:
   A negative `bytes` is passed to `malloc` as a huge `size_t`, so it returns 
NULL and we throw `OutOfMemoryError`. `Unsafe.allocateMemory` throws 
`IllegalArgumentException` here.
   
   Could we add an explicit `bytes < 0` check so callers see the same exception 
type?



##########
memory/memory-ffm/src/main/java/org/apache/arrow/memory/ffm/FfmMemoryAccessor.java:
##########
@@ -0,0 +1,181 @@
+/*
+ * Licensed to the Apache Software Foundation (ASF) under one or more
+ * contributor license agreements.  See the NOTICE file distributed with
+ * this work for additional information regarding copyright ownership.
+ * The ASF licenses this file to You under the Apache License, Version 2.0
+ * (the "License"); you may not use this file except in compliance with
+ * the License.  You may obtain a copy of the License at
+ *
+ *    http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+package org.apache.arrow.memory.ffm;
+
+import java.lang.foreign.MemorySegment;
+import java.lang.foreign.ValueLayout;
+import java.nio.ByteBuffer;
+import org.apache.arrow.memory.util.MemoryUtilAccessor;
+
+/**
+ * {@link MemoryUtilAccessor} backed by {@code java.lang.foreign} ({@link 
MemorySegment} and the
+ * foreign {@link java.lang.foreign.Linker}). Does not use {@code 
sun.misc.Unsafe} or reflection
+ * into {@code java.nio} internals, so it requires neither {@code --add-opens} 
nor {@code
+ * sun.misc.Unsafe} availability.
+ *
+ * <p><b>Required JVM flag.</b> This accessor calls the restricted methods 
{@link
+ * MemorySegment#reinterpret(long)} and {@link 
java.lang.foreign.Linker#downcallHandle}, the latter
+ * for {@code malloc} and {@code free}. That is a real, current requirement, 
not a caveat that this
+ * module already handles for you: classpath (unnamed-module) consumers must 
pass {@code
+ * --enable-native-access=ALL-UNNAMED} and module-path consumers must pass 
{@code
+ * --enable-native-access=org.apache.arrow.memory.ffm} on the JVM command 
line. Without it the JVM
+ * prints a warning on every restricted call today, and a future JDK that 
enables restricted-method
+ * enforcement by default will turn that into a hard {@link 
IllegalCallerException}. The {@code
+ * Enable-Native-Access: ALL-UNNAMED} entry in this module's jar manifest does 
<em>not</em> cover
+ * this: the JVM only honours that attribute in the manifest of the jar it was 
launched with via
+ * {@code java -jar}, never for a jar that is merely a classpath dependency.
+ *
+ * <p>{@link #allocateMemory}/{@link #freeMemory} call {@code malloc}/{@code 
free} directly, like
+ * the {@code sun.misc.Unsafe}-backed accessor, so {@link #freeMemory} accepts 
any address returned
+ * by {@code malloc}.
+ */
+public final class FfmMemoryAccessor implements MemoryUtilAccessor {
+
+  public static final MemoryUtilAccessor INSTANCE = new FfmMemoryAccessor();
+
+  private FfmMemoryAccessor() {}
+
+  private static MemorySegment segment(long address, long byteSize) {
+    return MemorySegment.ofAddress(address).reinterpret(byteSize);
+  }
+
+  private static int checkedInt(long value) {
+    if (value < 0 || value > Integer.MAX_VALUE) {
+      throw new IllegalArgumentException("value out of int range: " + value);
+    }
+    return (int) value;
+  }
+
+  /** Allocates {@code bytes} of uninitialized native memory with the C 
library's {@code malloc}. */
+  @Override
+  public long allocateMemory(long bytes) {
+    return NativeMemory.allocate(bytes);
+  }
+
+  /** Frees native memory with the C library's {@code free}. */
+  @Override
+  public void freeMemory(long address) {
+    NativeMemory.free(address);
+  }
+
+  @Override
+  public byte getByte(long address) {
+    return segment(address, Byte.BYTES).get(ValueLayout.JAVA_BYTE, 0);
+  }
+
+  @Override
+  public void putByte(long address, byte value) {
+    segment(address, Byte.BYTES).set(ValueLayout.JAVA_BYTE, 0, value);
+  }
+
+  @Override
+  public short getShort(long address) {
+    return segment(address, Short.BYTES).get(ValueLayout.JAVA_SHORT_UNALIGNED, 
0);
+  }
+
+  @Override
+  public void putShort(long address, short value) {
+    segment(address, Short.BYTES).set(ValueLayout.JAVA_SHORT_UNALIGNED, 0, 
value);
+  }
+
+  @Override
+  public int getInt(long address) {
+    return segment(address, Integer.BYTES).get(ValueLayout.JAVA_INT_UNALIGNED, 
0);
+  }
+
+  @Override
+  public void putInt(long address, int value) {
+    segment(address, Integer.BYTES).set(ValueLayout.JAVA_INT_UNALIGNED, 0, 
value);
+  }
+
+  @Override
+  public long getLong(long address) {
+    return segment(address, Long.BYTES).get(ValueLayout.JAVA_LONG_UNALIGNED, 
0);
+  }
+
+  @Override
+  public void putLong(long address, long value) {
+    segment(address, Long.BYTES).set(ValueLayout.JAVA_LONG_UNALIGNED, 0, 
value);
+  }
+
+  @Override
+  public void setMemory(long address, long bytes, byte value) {
+    segment(address, bytes).fill(value);
+  }
+
+  @Override
+  public void copyMemory(long srcAddress, long destAddress, long bytes) {
+    MemorySegment.copy(segment(srcAddress, bytes), 0, segment(destAddress, 
bytes), 0, bytes);
+  }
+
+  @Override
+  public void copyToMemory(byte[] src, long srcIndex, long destAddress, long 
bytes) {
+    MemorySegment.copy(
+        src,
+        checkedInt(srcIndex),
+        segment(destAddress, bytes),
+        ValueLayout.JAVA_BYTE,
+        0,
+        checkedInt(bytes));
+  }
+
+  @Override
+  public void copyFromMemory(long srcAddress, byte[] dest, long destIndex, 
long bytes) {
+    MemorySegment.copy(
+        segment(srcAddress, bytes),
+        ValueLayout.JAVA_BYTE,
+        0,
+        dest,
+        checkedInt(destIndex),
+        checkedInt(bytes));
+  }

Review Comment:
   `checkedInt` throws `IllegalArgumentException` when `bytes` or the index 
exceeds `Integer.MAX_VALUE`, while the Unsafe accessor takes `long`. Since the 
other side is a `byte[]`, this probably can't happen in practice.
   
   Could you add a short comment saying so, so the difference is deliberate?



-- 
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.

To unsubscribe, e-mail: [email protected]

For queries about this service, please contact Infrastructure at:
[email protected]

Reply via email to