Hacker News new | past | comments | ask | show | jobs | submit
I didn't say there's a lot of unsafe (by whatever measure you consider that to be). I said there's a lot more than in Java.
The JVM is written in an unsafe language. That's where these unsafe calls occur. Clients of these APIs have no control there. Java code itself is safe.
> That's where these unsafe calls occur.

No, it's not. A relatively common case for unsafe in Rust is in data structures. The data structures in the Java runtime are all written in Java and has almost no use of unsafe (there might be a few, but not becsause of language limitation, but because of initialisation order).

As for the JVM, much of it is a compiler. It doesn't matter what language it's written in, as it generates machine code. Indeed, the vulnerability disclosed yesterday was in a compiler intrinsic, but those are just as common in Rust.

Other than that, the other big piece in the JVM is memory management, and it does, indeed, use C and C++ for memory management, but Rust also uses C (the various implementations of malloc/free in whatever runtime the Rust program is using; BTW, some popular malloc/free implementations are almost as big as Java's most complicated GC).

As for native C/C++ libraries (outside the JVM itself), Java programs do use some, but generally not more than Rust programs.

Not accurate. Most datastructures that use unsafe in Rust are primitives; `Box`, `Vec`, `Rc`/`Arc`, `HashMap` and `BTreeMap`. Of those, only the equivalent to `HashMap` and `BTreeMap` are written in Java, and they can be written in safe Rust as well if you're willing to give up some of the performance (e.g. always initializing - thing that happens in Java as well). More complex data structures (e.g. graphs) can and often are implemented without unsafe at all.
I know that you can write slow data structures in safe Rust, but the standard library's data structures are supposed to be efficient. Anyway, a quick look at how common unsafe is in Rust vs Java, both in the standard library and in applications, and you'll see that it's much more common in Rust.
> BTW, some popular malloc/free implementations are almost as big as Java's most complicated GC

I'm pretty sure that's incorrect by a wide margin, and that all non-nop GCs java has are as large as at least some popular allocators.

Then you're in for a surprise. Take a look at TCMalloc and jemalloc. They're almost the size of HotSpot's most complex GC, then non-pause ZGC.
Not all JVM implementations, there are multi-circular implementations in Java, starting with JikesRVM almost 20 years ago already, whose work with the MMtk memory allocator was later inspired a Rust version thereof.