Live data from Hacker News

Lessons from Mixing Rust and Java: Fast, Safe, and Practical

medium.com

21–30 of 45 posts

Re: Lessons from Mixing Rust and Java: Fast, Safe, and Practical

#21
post #17

I thought JNI was being deprecated in favor of the new FFM interface. https://openjdk.org/jeps/472

This says:

> It is not a goal to deprecate JNI or to remove JNI from the Java Platform.

It says they want to put a safety barrier in front of JNI, so you'll have to explicitly indicate that you want a module to use JNI.

Re: Lessons from Mixing Rust and Java: Fast, Safe, and Practical

#22
post #12

I agree that Java + native is the way to go. But does rust really give you an edge over C/C++? Here is how you do JNI with C++: http://move.rupy.se/file/jvm.txt So simple it's ridiculous! Then you can use RegisterNatives to give C++ API to the Java side instead of the stub (Java calls C++ .dll/.so) thing...

Your comment is exactly the reason why while I find Rust a cool language, I would be using C++ instead.

That is the systems language most JVM implementations make use of, alongside Java, and what is directly supported by JNI tooling, including on Java IDEs mixed language debugging.

And in what concerns Java, native is anyway the synonym for unsafe.

However to each their own.

Re: Lessons from Mixing Rust and Java: Fast, Safe, and Practical

#23

I found mixing Bun and Rust works pretty well. Bun has gotten many cool new things recently which feel great to use and it has a nice FFI API. So I have Next.js apps running in Bun runtime and anything CPU bound is written in Rust and called via FFI.

I would have expected Zig, given Bun, or using Deno instead, if Rust as extension language.

Re: Lessons from Mixing Rust and Java: Fast, Safe, and Practical

#24
post #23

I found mixing Bun and Rust works pretty well. Bun has gotten many cool new things recently which feel great to use and it has a nice FFI API. So I have Next.js apps running in Bun runtime and anything CPU bound is written in Rust and called via FFI.

I would have expected Zig, given Bun, or using Deno instead, if Rust as extension language.

> I would have expected Zig

You can use Zig instead if it meets your needs, but last time I checked, the Zig ecosystem was lacking compared to Rust.

> or using Deno instead, if Rust as extension language

Deno? There was a thread the other day that Deno is dead. I wouldn't even compare Deno and Bun because Bun is just waaay better. Even before Bun already felt very polished and nice to use, and now it even has native postgres and s3 clients. And it doesn't try to sell you a KV database or some shit.

Re: Lessons from Mixing Rust and Java: Fast, Safe, and Practical

#25
post #23

Earlier quoted context omitted.

I would have expected Zig, given Bun, or using Deno instead, if Rust as extension language.

> I would have expected Zig You can use Zig instead if it meets your needs, but last time I checked, the Zig ecosystem was lacking compared to Rust. > or using Deno instead, if Rust as extension language Deno? There was a thread the other day that Deno is dead. I wouldn't even compare Deno and Bun because Bun is just waaay better. Even before Bun already felt very polished and nice to use, and now it even has native…

The point I was making was using the same language the VM makes use of, instead of adding extra layers.

Re: Lessons from Mixing Rust and Java: Fast, Safe, and Practical

#26
post #25

Earlier quoted context omitted.

> I would have expected Zig You can use Zig instead if it meets your needs, but last time I checked, the Zig ecosystem was lacking compared to Rust. > or using Deno instead, if Rust as extension language Deno? There was a thread the other day that Deno is dead. I wouldn't even compare Deno and Bun because Bun is just waaay better. Even before Bun already felt very polished and nice to use, and now it even has native…

The point I was making was using the same language the VM makes use of, instead of adding extra layers.

I mean, I guess there would be some point if you, e.g., forked bun and wrote new APIs in Zig. But if you are using FFI it doesn't really matter because the interface is C ABI anyway; there's no extra layers in using a language different from the one your runtime uses.

Re: Lessons from Mixing Rust and Java: Fast, Safe, and Practical

#27
post #17

I thought JNI was being deprecated in favor of the new FFM interface. https://openjdk.org/jeps/472

FFM is the recommended alternative, but JNI is not being deprecated. There are some things that JNI can do that FFM can't, in particular - initiating calls from native code to arbitrary Java methods. FFM only supports upcalls to Java in the form of Java callbacks that are passed to the native code. However, this is enough for the vast majority of Java-native interactions, so FFM should be preferred in new code (it's much easier to use than JNI).

Re: Lessons from Mixing Rust and Java: Fast, Safe, and Practical

#28

1. The article seems kind-of shallow. I didn't see any concrete (qualitative or quantitative) remarks about the "fast" part. I don't doubt you have reasons to do this - but I expected some information on what component are you writing using Rust + JNI, and how it helped? Or is it just a demo? At some point, repeated calls into the JNI are counter-productive to performance, since the JIT can not optimize them. Pinning…

This article summarizes our experience from a commercial project that runs on an in-vehicle Android system. In this project, we needed to invoke Rust code(DB) from Java(App), so we couldn't directly use the project’s source code for demonstration. Instead, we created a demo project: https://github.com/GreptimeTeam/rust-java-demo 1. I agree that using Rust doesn't necessarily mean faster performance; it simply gives y…

Thanks for the clarifications. Good if you mention the background in the medium post. Otherwise it reads like a PoC demo.

5. How did you handle java local and global ref lifetimes in rust callee? Was it assumed that java caller owns all the refs and freed after the rust computation returns? Or did your calls mostly involve byte buffers and primitive types? That latter is a sweet spot but not always feasible.

Re: Lessons from Mixing Rust and Java: Fast, Safe, and Practical

#29

I found mixing Bun and Rust works pretty well. Bun has gotten many cool new things recently which feel great to use and it has a nice FFI API. So I have Next.js apps running in Bun runtime and anything CPU bound is written in Rust and called via FFI.

JS developer use tooling older than 3 years challenge. Level: impossible.

Re: Lessons from Mixing Rust and Java: Fast, Safe, and Practical

#30
post #21
post #17

I thought JNI was being deprecated in favor of the new FFM interface. https://openjdk.org/jeps/472

This says: > It is not a goal to deprecate JNI or to remove JNI from the Java Platform. It says they want to put a safety barrier in front of JNI, so you'll have to explicitly indicate that you want a module to use JNI.

I know that's the goal, but I was doing deeper reading and didn't understand the nuances here. It felt deprecated, and I remember reading that you should prefer FFM.

That's one of the reasons I posted it. A lot of knowledgeable people here can chime in more details. And a sibling comment did!

Post reply on HN