5 ms·
Yes. Check out a library like zstd-jni. You'll find native libraries inside it. It'll load from the classpath first, and then ask the OS linker to find it.
by zten 2y ago
Yes. Check out a library like zstd-jni. You'll find native libraries inside it. It'll load from the classpath first, and then ask the OS linker to find it.
- marginalia_nu 2y agoSounds promising. I have some extremely unwieldy off-heap operations currently implemented in Java (like quicksort for 128 bit records) that would be very nice to offload as FFI calls to the corresponding a single-line C++ function.
- neonsunset 2y agoWhy not give C# a try instead? It has everything you ask for and then some.
- neonsunset 2y agoWow, you all are sure mad enough to go out of your way and downvote my comments elsewhere. Stay in the swamp :)
- coldtea 2y agoBecause "some inconvenience/unmet requirement" from a language is not an invitation to "throw out the whole platform and your existing code and tooling, and learn/adopt/use an entirely different, single-vendor platform". Except if we're talking about some college student or hobbyist picking their first language and exploring the language space...
- imtringued 2y agoHe would still have to call out to the C++ function.
- neonsunset 2y agoAssuming it is "sort for 128bit records", that's something C# does really well - writing optimized code with structs / Vector128<T> / pointer arithmetic when really needed without going through FFI and having to maintain separate build step and project parts for a different platform. But even if it was needed, such records can be commonly represented by the same structs both at C#'s and C++'s sides without overhead. An array of such could be passed as is as a pointer, or vice versa - a buffer of struts allocated in C/C++ can be wrapped in a Span<Record128> and transparently interact with the rest of standard library without having to touch unsafe (aside from eventually freeing it, should that be necessary).
- maksut 2y agoI'd like to learn how they do it. Because last time I've looked at this, the suggested solution was to copy the binaries from claspath (eg: the jar) into a temporary folder then load it from there. It feels icky :)
- renewiltord 2y agoEDIT: Disregard. I am wrong. Original below. You can just load as a resource. We do this internally since much of network stack is C. But we use JNI because code is older than Java 22.
- maksut 2y agoYou made me search it again. And still I don't see how that's possible. `Runtime.load` requires a regular file with an absolute path[0]. Stackoverflow is full of "copy it into a temp file" solutions. ChatGPT keeps saying "sorry" but still insists on copying it into a temp file :) [0] - https://docs.oracle.com/en%2Fjava%2Fjavase%2F22%2Fdocs%2Fapi%2F%2F/java.base/java/lang/Runtime.html#load(java.lang.String) https://docs.oracle.com/en%2Fjava%2Fjavase%2F22%2Fdocs%2Fapi...
- renewiltord 2y agoEmbarrassing of me to give you wrong answer. I went and checked my old code and: new FileOutputStream(tmpFile) Apologies.
- zten 2y agoYep, you're right, they do exactly that. Apologies for the confusion. Decompiled class file: try { var4 = File.createTempFile("libzstd-jni-1.5.0-4", "." + libExtension(), var0); var4.deleteOnExit();
- sedro 2y agoThis wouldn't work on Windows, because you can't delete a DLL while it's in use