Skip to content

Commit

Permalink
Drop arrow-memory-core dependency
Browse files Browse the repository at this point in the history
It makes little sense to include the full dependency just to allow for
memory-mapped byte buffer which we can construct by hand.
  • Loading branch information
hubertp committed Dec 15, 2023
1 parent 9329b73 commit 28a06cb
Show file tree
Hide file tree
Showing 3 changed files with 53 additions and 5 deletions.
5 changes: 2 additions & 3 deletions build.sbt
Original file line number Diff line number Diff line change
Expand Up @@ -1396,8 +1396,7 @@ lazy val `runtime-language-arrow` =
inConfig(Compile)(truffleRunOptionsSettings),
instrumentationSettings,
libraryDependencies ++= Seq(
"org.graalvm.truffle" % "truffle-api" % graalMavenPackagesVersion % "provided",
"org.apache.arrow" % "arrow-memory-core" % apacheArrowVersion
"org.graalvm.truffle" % "truffle-api" % graalMavenPackagesVersion % "provided"
)
)

Expand Down Expand Up @@ -1886,7 +1885,7 @@ lazy val `engine-runner` = project
"com.sun.jna.internal.Cleaner",
"com.sun.jna.Structure$FFIType",
"akka.http",
"org.apache.arrow.memory.util.MemoryUtil"
"org.enso.interpreter.arrow.util.MemoryUtil"
)
)
.dependsOn(assembly)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,11 @@
import com.oracle.truffle.api.library.CachedLibrary;
import com.oracle.truffle.api.library.ExportLibrary;
import com.oracle.truffle.api.library.ExportMessage;
import java.nio.*;
import java.util.BitSet;
import org.apache.arrow.memory.util.MemoryUtil;
import java.nio.ByteBuffer;
import java.nio.ByteOrder;
import org.enso.interpreter.arrow.ArrowParser;
import org.enso.interpreter.arrow.util.MemoryUtil;

@ExportLibrary(InteropLibrary.class)
public class ArrowCastToFixedSizeArrayFactory implements TruffleObject {
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
package org.enso.interpreter.arrow.util;

import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.nio.ByteBuffer;

public class MemoryUtil {

private static Constructor<?> byteBufferConstr = null;

static {

ByteBuffer buffer = null;
try {
buffer = ByteBuffer.allocateDirect(1);
byteBufferConstr = buffer.getClass().getDeclaredConstructor(long.class, long.class);
byteBufferConstr.setAccessible(true);
} catch (NoSuchMethodException e) {
System.err.println("Unable to find a constructor for ByteBuffer created directly from a memory address: " + e.getMessage());
} finally {
if (buffer != null) {
buffer.clear();
}
}
}

/**
* Create a ByteBuffer directly from a (allocated) memory address and its size without copying.
*
* @param address address in memory to the start of the allocated chunk of memory
* @param capacity size in bytes of the allocated chunk of memory
* @return ByteBuffer instance
*/
public static ByteBuffer directBuffer(long address, long capacity) {
if (byteBufferConstr != null) {
try {
return (ByteBuffer) byteBufferConstr.newInstance(address, capacity);
} catch (InstantiationException e) {
throw new RuntimeException(e);
} catch (IllegalAccessException e) {
throw new RuntimeException(e);
} catch (InvocationTargetException e) {
throw new RuntimeException(e);
}
}
throw new RuntimeException("constructor for a ByteBuffer created from a memory address is missing");
}
}

0 comments on commit 28a06cb

Please sign in to comment.