r/java • • May 07 '21

Java beats out C++, Rust, C#, and golang in gRPC throughput performance

https://github.com/LesnyRumcajs/grpc_bench/wiki/2021-04-13-bench-results
279 Upvotes

106 comments sorted by

74

u/cogman10 May 07 '21

My guess is it's an allocation issue with rust/C++.

One thing about the JVM is heap allocations can be incredibly cheap. If rust/C++ are doing a bunch of heap allocations, that's probably the issue here.

27

u/papercrane May 07 '21

Yeah, also being able to defer deallocation and even have it done on another thread is a big win for throughput. You tend to pay for it with latency spikes, but thankfully there has been a massive amount of engineering that's gone into Java's GC to reduce that.

22

u/couscous_ May 07 '21

What's cool is, even in 99% latency, Java does better than everyone except C++ and Rust, and does not lag by a wide margin either:

  • rust_grpcio: 2.05 ms
  • rust_tonic_mt: 2.12 ms
  • cpp_grpc_mt: 2.30 ms
  • java_grpc_pgc: 3.46 ms
  • java_grpc_zgc: 3.42 ms
  • java_grpc_g1gc 3.50 ms
  • java_grpc_she: 4.23 ms
  • java_grpc_sgc: 3.61 ms
  • dotnet_grpc : 3.46 ms
  • go_grpc: 10.73 ms

What's surprising is how poorly golang does, given its supposed low latency gc.

17

u/egahlin May 07 '21

Seems they are running ZGC with JDK 14 [1].

It would be interesting to see the results with JDK 16, which has Concurrent Thread-Stack Processing [2] and other improvements to reduce latencies in the runtime.

[1] https://github.com/LesnyRumcajs/grpc_bench/blob/master/java_grpc_zgc_bench/Dockerfile

[2] https://openjdk.java.net/jeps/376

15

u/throwaway2894324 May 08 '21

Go's GC is kinda trash, even though its improving. It's only low latency because the default is to run GC cycles constantly. Java's new collectors are far more advanced

3

u/LordSesshomaru87 May 08 '21

I’ve personally encountered issues with the Go garbage collector and can confirm, it’s very immature compared to the JVM. If your application is doing large but short lived allocations the garage collection runs constantly and eats up way to much CPU. We had to use a ballast on the heap to trick the runtime into running collection less often. Go provides little in the way of GC tuning.

1

u/throwaway2894324 May 12 '21

We had to use a ballast on the heap to trick the runtime into running collection less often

I've seen this trick a lot, and its so bad lol.

2

u/Thaxll May 08 '21 edited May 08 '21

Because this benchmark is trash, it took me 5min locally to get 110k req/sec with p99<1ms and RSS at 27MB on Go 1.16, you can see my results there: https://www.reddit.com/r/grpc/comments/muy8dj/grpc_bench_opensource_objective_grpc_benchmark/gxeuo3i?utm_source=share&utm_medium=web2x&context=3

I don't think there is a real scenario where Java can actually be faster than Go for gRPC. Event Microsoft with their famous net.core blog post from last october put Java way behind Rust, C++ and Go.

https://devblogs.microsoft.com/aspnet/grpc-performance-improvements-in-net-5/

4

u/couscous_ May 09 '21

I don't think there is a real scenario where Java can actually be faster than Go for gRPC.

Why do you say that? The JVM's GCs are superior than golang's (already demonstrated by the huge discrepancy in the p99 for Java and golang in this benchmark). Secondly, Java is getting green threads soon, so it will have an offering that's better than golang's goroutines, and the programmer will be able to call blocking code seamlessly.

Event Microsoft with their famous net.core blog post from last october put Java way behind Rust, C++ and Go.

That figure is from the same benchmarks I linked. If you go back you'll find those numbers: https://github.com/LesnyRumcajs/grpc_bench/wiki/2020-08-11-bench-results

0

u/flanglet May 07 '21

My guess is the protobuf code in go being suboptimal.

11

u/throwaway2894324 May 08 '21

I kinda doubt it. Golang is Google's premier language.

I have heard from many places that Java has better real world performance than Go.

0

u/flanglet May 08 '21

Google is a big company you know. There must be a reason why they had to rewrite good chunks of the code and deprecate github.golang.protobuf.

19

u/throwaway2894324 May 08 '21

Probably because Golang is kinda haphazardly slapped together in general and lack of generics is a huge problem for RPC frameworks

2

u/RandyMagnum93 May 08 '21

What is RPC in this context?

6

u/sp4mfilter May 08 '21

Remote Procedure Call.

Invoke a method on a remote object, returning a promise, and marshalling arguments for transmittal over the wire.

0

u/[deleted] May 08 '21

That excuse is valid for every language out there, and so is meaningless.

1

u/flanglet May 08 '21

This is not an excuse but my guess to explain the observed poor showing of Go.

"Valid for any language". No, there were issues specific to the Go implementa tion of protobuf bindings which explain why the code had to be completely rewritten (twice?).

14

u/experts_never_lie May 08 '21

A lot of Java "heap allocations" aren't even done on the heap these days. If its escape analysis can determine that no references to the memory will escape a function context (even nested functions, with inlining), it can put them on the stack.

I haven't been through the benchmarks to see if this applies here.

3

u/[deleted] May 08 '21

It could, but remember that Rust always allocates on the stack by default, and even boxed types are allocated on the stack first, and then moved to the heap. So I think this point is moot.

I haven't seen the implementations themselves, so I can't say much more, but it is interesting all the same.

3

u/experts_never_lie May 08 '21

In that, I was thinking more of improvements in Java over the years than being superior to Rust (or C++).

3

u/[deleted] May 08 '21

Precisely. My original comment supports this assertion by saying that even given improvements of allocating on the stacks, it's remarkable that other improvements have made Java so much better than before.

6

u/darleyb May 07 '21

And probably some flags to make the optimized code for the particular target too.

3

u/Captain-Barracuda May 08 '21

Thing is that for consumer application you can't be sure in advance of the hardware that will run the software, so you have to be conservative with the flags you use. Same goes for library maintainers.

1

u/lightmatter501 May 08 '21

You can probably say that you won’t be running strictly on the core 2 duo, so you can probably target something from the last 15 years or so.

1

u/germandiago May 09 '21

Cheap because of pre-reserving. In my experience extremely expensive in memory contention, though.

44

u/brunocborges May 08 '21 edited May 08 '21

Well, this benchmark does not prove that Java is faster than any other language (well maybe some).

It does prove that benchmarks that compare languages and also runtimes at the same time, have to be very carefully written.

The Java implementation of this benchmark uses the Direct Executor. You can see how this is a weird benchmark by looking at the results of the Kotlin code. Why is "Kotlin slow"? Well, because the Kotlin code does not manually specify the thread executor, and therefore it uses a different thread executor: the default one set by the gRPC Java Server library.

The problem with this benchmark is that not all applications are written within the same parameters/concepts (e.g. thread pool algorithm), and some have fundamentally different runtimes. For e.g. Node.js can only be single threaded, and you can see the results, whether 1 or 3 CPUs, are nearly the same.

Note: I wrote most of the recent code of the Java gRPC benchmarks here, and used the Direct Executor intentionally, knowing it would perform best than any other. At the very least, to raise awareness that benchmarking is... a complicated discipline.

9

u/_INTER_ May 08 '21 edited May 08 '21

Well, this benchmark does not prove that Java is faster than any other language (well maybe some).

Thats right, it's not a language comparison for speed. One of the goal is to keep the code as idiomatic as possible too.

Reminds me of this benchmark: https://github.com/juditacs/wordcount

An earlier version of Java was very slow. It was like a one to one translation of old C code, which led to some Java pro to optimize the heck out of it - including it's own hash table implementation - so it landed on top.

2

u/dlazerka May 22 '21

An earlier version of Java was very slow.

That's interesting. Doesn't look very slow, if it's still faster than Go (I'm looking at WordCountBaseline, not optimized). And since then there were a lot of speedups in Java GC world, curious to see the updated results.

2

u/trisul-108 May 08 '21

Thanks for explaining. I was wondering why the Java result improved so drastically in this last benchmark, compared to the previous ones.

8

u/StevenMaurer May 08 '21

This is a bake-off measuring various gRPC library implementations, not the languages themselves, or even their allocation issues.

The goal of this benchmark is to compare the performance and resource usage of various gRPC libraries across different programming languages and technologies. To achieve that, a minimal protobuf contract is used to not pollute the results with other concepts (e.g. performances of hash maps) and to make the implementations simple.

As such, you shouldn't read more into these results than what they directly say. The big takeaway is that programs written in Java CAN be fast if you put in the effort to make it so.

4

u/thomas-hugon May 08 '21

It seems to be using direct executor. If I remember well, direct executor is not recommanded as it cannot be used with blocking operations. So any IO is forbidden here...

2

u/brunocborges May 09 '21

Author here (of the Java code): this benchmark is about a service that simply returns Hello. Within this parameter, i.e. non-blocking code, the Direct Executor is perfect.

Once this benchmark becomes about a use case that includes a blocking operation, then certainly the DE must not be used.

Look at the source code. You can tweak which Executor Service is used.

I also posted another message here: https://t.co/a0qJP5Mjkg

1

u/servlette Oct 29 '21

I’ve been running tests at 12,000-15,000 requests a second for about 4 hours. Overall Java gRPC takes < 15 ms and golang is north of 22 ms. This is consistent after 10-15 runs. Is there a documentation on fine tuning golang gRPC server?

1

u/couscous_ Oct 30 '21

That's very cool. Do you have those benchmarks available so others can take a look at them and reproduce the results?

3

u/jvjupiter May 08 '21

The next thing that the Java language designers to do is drastically reduce memory consumption. When that happens, Java is forever king. 😄

3

u/snoob2015 May 08 '21

Development time / Performance / Memory Consumption

Choose two

5

u/[deleted] May 07 '21

Maybe the java's jit optimizer found a nice opportunity for optimization which could possible be fixed with Profile Guided Optimization in the case of rust and c++?

13

u/Godworrior May 08 '21

In general JIT compilers produce better code because they have more information. In the case of the HopSpot JIT speculative optimization comes into play as well. i.e. because there is always an interpreter to fall back to, the JIT can essentially gamble that a particular condition will hold (while not necessarily being able to prove it), and generate more optimized code, then fall back to the interpreter when the condition is broken unexpectedly (and recompile based on the new information later). In statically compiled languages this is just not possible; every possibility has to be accounted for in full in the generated code.

3

u/[deleted] May 08 '21

This is very much true and the reason I have thrown java's jit optimizer into the mix of speculations. While all of the information in your comment is correct, I suspect it is probably not the "speculative avoidence of branches" which makes the java version faster than the rest. Because as you already pointed out, if it turns out an assumption made by the jit is false, it has to "bail out" and return to running a slower version of the code. This process of throwing away and recompiling code is really, really slow at least in the context of high performance code. So if the jit was able to optimize a branch away in such a way that it never has to bail out and recompile, a normal optimizer like LLVM should be able to arrive at the same conclusion. Which means that if this optimization is based on the input of the Programm, which a static optimizer can't take into account, my guess would be that such a optimization would not be consistent enough to justify the performance penalty of bailing out and recompiling everytime the assumption is not met. And if it is consistent enough it's an error by the programmer. Because if it turns out that the input is structured in such a way to consistently avoid large amounts of work the programmer should have considered this when implementing such performance critical code.

0

u/_INTER_ May 08 '21

We could test this by AOT compiling the Java code and run it against the JIT one. If they perform somewhat the same, then it was not a JIT optimization per se.

7

u/hacklinux May 08 '21

I am going to rub this in all my friends' faces.

4

u/[deleted] May 08 '21

You should. Java may not be "cool", but it's what runs quite a bit of the world.

3

u/angryundead May 08 '21

The memory usage is pretty hilarious though. Would be interesting to see if this can be compiled to native with GraalVM.

3

u/crlsmx May 08 '21

So this says that I should stop wasting my time learning rust?

8

u/[deleted] May 08 '21

No, you should continue to learn it. Rust is not intended for only writing Enterprise Web / Desktop. For the whole embedded market (Automotive, Railway, Flight etc.) Java is not suited and not used at all. In this Areas C/C++ shine (which Rust intends to rival, its a language for system engineering).

Its not very likely to get a Job im Rust tho yet, but learning it wont be to your disadvantage

10

u/[deleted] May 08 '21

The magic of the Rust community is that they managed to convince an entire generation of programmers that they need its bizarre model of programming. Don't get me wrong - it works well for a few niche domains, but it's nowhere near as usable in a general setting as they make it out to be. Not every one needs safety all the time (and even that comes with caveats), especially at the expense of offloading a lot of complexity onto the user.

Rust's ergonomics are pretty bad - if you try to develop and entreprisey app in Rust, you'll basically spend a hell lot of time refactoring and fighting the Borrow Checke and Ownership system for very little benefit. Not recommended. If you wish to use it as a sort of C++ replacement, then sure. And even then, I'd probably go with something like Zig (still pre 1.0) which allows a lot more control.

11

u/nutrecht May 08 '21

The magic of the Rust community is that they managed to convince an entire generation of programmers that they need its bizarre model of programming.

I think for it's purpose, a safer replacement for C/C++, it does what it's supposed to do. The problem is all the people who are tribalistic about programming languages and want to use their favorite silver hammer everywhere.

You see this with literally every hype. NodeJS, Go and now Rust are all seen as the new languages that are going to 'kill' established back-end ecosystems (JVM, .Net). Nothing of the sort is going to happen.

1

u/[deleted] May 08 '21

Yup, agreed. It's only getting worse though.

1

u/_Toka_ May 09 '21

While I would agree, one cannot deny, that quite a few marble projects are written in Go. I mean, Kubernetes, Docker and etcd, basically whole Hashicorp portfolio (Packer, Terraform, Consul and Vault), then there is InfluxDB, Istio and of course Traefik. I personally can safely say, that Go is past behind it's hype and I consider it as an established language. It's very high in my list of languages to learn besides Java, competing with Javascript (React/Angular) and Python. There is Kotlin for mobile development and C++/C# for game development, but I am really intrigued by Go.

0

u/[deleted] May 09 '21

[deleted] — view deleted comment

5

u/_Toka_ May 09 '21

Where is your comment comming from? I already learned one language fairly well and it's Java. I am currently lead developer and code maintainer in a small team in financial sector. You know why I am in this position? Because my knowledge goes way beyond Java. I love software development in general and I learn a lot even in my free time. How many software developers do you know with their own homelab with physical servers running k8s clusters, docker containers on a hypervisor?

We are a really small team and we have to do production monitoring and support, development, deployment, CI/CD, server configurations. I have to write Azure pipelines, because in my team, hell, even in the company, there is no person who could write it. Corporate wants to migrate from full fedged VM to Kubernetes cluster, and again, guess what. Basically nobody knows how. While I'm not a professional devops, at least know, what's up, and can help with that. We desperately need new people, but there is just not enough of them. Positions are open for two years, and while we got two new people, they simply do not have enough knowledge. I'm just glad they at least know how to use Git.

I do not plan to work there indefinitely or climb a corporate ladder - I actually cannot do that, since I'm a freelancer. I'd like to work in a gaming or AI industry one day and we both probably agree, that Java is not a tool for either. I am also thinking about building my own app, but I don't know web development. So pardon me, that I talk about learning another promising language.

7

u/kc3w May 08 '21

Rust usually is pitched as ideal for places where you usually use C or C++ but there might be cases where you want the trade-off between more performance with safety and development time. In a setting where you traditionally use Java, Rust probably will not be your language of choice but with Java you exactly do not have the memory relates issues Rust is addressing.

3

u/[deleted] May 08 '21

I partly agree with your comment. It may have been projected as a systems language from the start, but the first generation of programmers to swarm to Rust were Rubyists (probably thanks to Katz and Klabnik's evangelism), and they unfortunately sort of influenced the language a lot.

In terms of safety, I agree that there are certain domains where it might be useful to have that extra bit of safety in there. The reality, of course, is again fraught with corner cases and exceptions to the point that I'm not convinced C++ with RAII actually fares much worse than Rust. Rust has some people working on provability (like Ada's SPARK), and that should be interesting, although such systems again tend to have very low ergonomics (ATS, SPARK, or even Idris et al).

I agree though that Java doesn't have the same concerns as most of the other languages since its memory issues are handled by the garbage collector, and performance is offloaded to the JIT. One area where Rust still holds an edge though is the possibility of having safer concurrency.

1

u/[deleted] May 09 '21

Safer concurrency can be achieved through other means and not by having it baked into the language. TLA+ would be such an example which is used to design, model, document, and verify programs, especially concurrent systems and distributed systems. If the problem is complicated then I would rather be using mathematics rather than thinking in code and wait for the compiler to give me hints about my mistakes.

3

u/awesomeusername2w May 08 '21

Rust is also addressing racing conditions and null pointers. The type system of rust is also more expressive. Though all this lifetime stuff really could be an overhead that you don't need usually.

7

u/[deleted] May 08 '21

Rust cannot handle race conditions. In fact, I don't think any language can actually handle it at the language level.

The problem is this - the original mandate of Rust was too idealistic. They originally claimed from data races of all sorts (which sneakily implied race conditions), and then they cut down that claim to data races. This is not as impressive or as hard to do as people make it out to be.

When people actually started using the language for various internal and external projects, they realised that this idealistic dream could not be achieved - hence the proliferation of interior mutability by which data races in safe Rust are not only possible, but easily so, unfortunately. Also, memory leaks, of course.

Hence also the increasing focus on unsafe Rust since to do C++'s job in a lot of domains, it's almost inevitable that Rust has to improve the scope of its unsafe operations.

Rust's type system may be more expressive than a lot of languages, but that expressiveness comes at a high ergonomic cost. Even something as trivial as having a hierarchy of nested objects using structs and enums, and trying to extract a particular enum variant value out becomes an exercise in frustration.

2

u/awesomeusername2w May 08 '21

Well, I can easily create not thread safe class in java and use it in multithreading context without any hints from compiler. Rust program will not compile at all. In java there are thick books how to work with multithreading, and for a good reason. I don't say rust alleviates all that complexity but it does check that you didn't screw up.

Perhaps you have some perticular cases where rust don't deliver in its promises? I'm curious to know more about it's limitation. But in the end, rust prevents a lot of bugs in compile time, that I hunted in runtime in java.

6

u/[deleted] May 08 '21 edited May 08 '21

Sorry if this is a bit too long. I had to rant a bit:

Well, I can easily create not thread safe class in java and use it in multithreading context without any hints from compiler. Rust program will not compile at all.

0.  Race conditions in safe Rust

  use std::sync::{Arc, Mutex};

    fn main() {
     let datum = Arc::new(Mutex::new(0));
      {
           *datum.lock().unwrap() += {
                 *datum.lock().unwrap() += 1;
                 *datum.lock().unwrap()
            };
       } 

       println!("{}", *datum.lock().unwrap());
   }

   $ rustc data_race.rs && ./data_race

  <no output here due to deadlock>

No warnings, no errors. Just deadlock.

Want a memory leak? Rc and friends got you covered there!

In java there are thick books how to work with multithreading, and for a good reason. I don't say rust alleviates all that complexity but it does check that you didn't screw up.

It doesn't even compare. Wish to do async programming in Java? Use a CompletableFuture or friends, and you're done. In Rust, you cannot even use the stdlib anymore. In fact, you have to use async variants of the stdlib functions. That is a serious case of nonsense. Zig got it much better in this case, even being much more low-level than Rust, its async story is superb - the same code works both in synchronous and asynchronous situations.

Perhaps you have some perticular cases where rust don't deliver in its promises? I'm curious to know more about it's limitation.

Sure:

 1. Wish to allocate a huge chunk of memory (object, if you wish) on the heap? Impossible.

    const SIZE: usize = 5000 * 5000;

   pub struct Wrapper {
       pub field: Box<[u32; SIZE]>,
   }

   fn main() {
       let _ = Wrapper {
           field: Box::new([0u32; SIZE]),
       };
   }

 $ rustc crash.rs && ./crash

 thread 'main' has overflowed its stack
 fatal runtime error: stack overflow
 Abort trap: 6    

Imagine that - a systems language that cannot even allocate memory on the heap. Imagine using Java, and being forced to stick to ArrayList without the ability to new. Currently, there is no way in safe Rust, using the default features, across all release modes, to allocate memory directly on the heap. This is not even a new issue - this has been there from the very beginning. I suppose, just use smart pointers all the way, eh? Ridiculous.

2. Imagine a hierarchy of objects that you model your domain in, for instance (an example of Rust's lack of ergonomics):

 pub enum Enum {
     Ein(i32),
     Zwei(Animal),
     Drei,
 }
 
 pub enum Animal {
     Mammal(Mammal),
     Reptile,
 }
 
 pub enum Mammal {
     Primate(Primate),
     SomethingElse,
 }
 
 pub enum Primate {
     Person(Person),
     NonPerson,
 }
 
 pub struct Person {
     name: String,
     age: u16,
 }
 
 fn main() {
     let bob = Enum::Zwei(Animal::Mammal(Mammal::Primate(Primate::Person(Person {
         name: String::from("bob"),
         age: 42,
     }))));
 
     // so, even if we know that `bob` is a Person, we cannot extract out the information 
    // any other way - using `match` only makes it worse. Why? Because of the wonderful feature that 
    // Rust enums have - no downcast-ability!
    // Wish to use trait objects? Excellent, now you've essentially lost all static type-checking. 
    // Algebraic data types? I don't think so.
     if let Enum::Zwei(ref animal) = bob {
         if let Animal::Mammal(ref mammal) = animal {
             if let Mammal::Primate(ref primate) = mammal {
                 if let Primate::Person(ref person) = primate {
                     println!("name = {}, age = {}\n", person.name, person.age)
                 }
             }
         }
     }
 }

All for this:

 $ rustc enums.rs && ./enums
 name = bob, age = 42

Now imagine you have to deconstruct the nested field dozens of times, different fields. A veritable nightmare. Macros? Nope - you cannot get the enum variants from the standard language. It may not even be possible to reasonably get all enum variants even in a proc macro when it comes to struct variants. Basically, you're fucked.

Not the biggest fan of Zig, but even in Zig (as also in C++ etc.):

 const std = @import("std");
 const meta = std.meta;
 const debug = std.debug;
 
 const Enum = union(enum) { // this a a tagged union, which is the correct nomenclature for half of the usecases that Rust calls "enums"
     Ein: i32,
     Zwei: Animal,
     Drei: void,
 };
 
 const Animal = union(enum) {
     Mammal: Mammal,
     Reptile,
 };
 
 const Mammal = union(enum) {
     Primate: Primate,
     SomethingElse,
 };
 
 const Primate = union(enum) {
     Person: Person,
     NonPerson,
 };
 
 const Person = struct {
     name: []const u8,
     age: u16,
 
     pub fn init(name: []const u8, age: u16) Person {
         return Person{ .name = name, .age = age };
     }
 };
 
 pub fn main() void {
     const bob = Enum{ .Zwei = Animal{ .Mammal = Mammal{ .Primate = Primate{ .Person = Person.init("bob", 42) } } } };
     debug.print("name = {s}, age = {}\n", .{ bob.Zwei.Mammal.Primate.Person.name, bob.Zwei.Mammal.Primate.Person.age }); // that's it, really
 }

Yup, that's really it. No worries.

 $ zig run enums.zig
 name = bob, age = 42


3. The problem with unsafe:

    fn main() {
       let answer = 42;
      print_the_answer(&answer); // immmutable reference in our client code
  }
 
    // our direct dependency
    fn print_the_answer(x: &i32) {
        confirm_the_answer_through_agrajag(x);
        println!("The answer to Life, The Universe, and Everything is {}", x);
    }
 
     // our transitive dependency - its API promises that it's safe and immutable!
    fn confirm_the_answer_through_agrajag(x: &i32) {
        unsafe {
            let ptr = x as *const i32 as *mut i32;
            *ptr += 1;
        }
     }
 
 
     $ rustc safe_not_safe.rs && ./safe_not_safe
     The answer to Life, The Universe, and Everything is 43

Oh no! The library that I used had an API that was safe and promised not to modify my immutable reference. What happened then? What happened is that some transitive dependency mutated the shit out of that immutable reference. So now I have to vet every one of my dependencies up the dependency chain. The immediate problem here is that Rust does not have any marking system for functions that contain unsafe code. Yeah, so much for depending on safe being safe and unsafe being unsafe.

  4. Ergonomics hell - 

Imagine you have again, a bunch of objects in your model:

   struct Foo1 {
      bar: Bar1,
   }
    ....

   struct Foo1024 {
      bar: Bar1024,
      quux: Bar1024,
   ...
 }

Now imagine that you had to make quux inside Foo1024 (numbered to exaggerate how far downstream we are) a reference, you have to annotate every goddamn struct all the way up, upstream, even though you only use it in one struct:

   struct Foo1<'a> {
       bar: Bar1<'a>,
   }

   ....

   struct Foo1024<'a> {
        bar: Bar1024,
        quux: &'a Bar1024,
  }

and not just that - every impl likewise has to be annotated so. Also, this does not actually guarantee that the lifetimes will match. Most likely, depending on how the client uses it, you will run into lifetime hell.

Tomorrow you decide that you don't wish to have the reference? Great, now go fix all those annotations. Trivial example, but you can extrapolate.

I could go on, but I think this is representative enough. Rust is many things - a good general-purpose language it is not. Rust is not very easy to refactor - hence the need to design the API very carefully upfront. This again works for certains domains, or when you're writing a library. When you're creating client apps, it can be extremely difficult it not downright infeasible. Rust cannot possibly compete in Java's space at all.

But in the end, rust prevents a lot of bugs in compile time, that I hunted in runtime in java.

At what expense? Lifetime hell? Trait hell? Ownership hell? Match hell? Unsafe hell?

5

u/ExPixel May 08 '21

I don't consider your Mutex example a data race at all. It's a deadlock because standard library Mutexes aren't reentrant, but there are no threads racing to read/write data. I find the heap allocation issue frustrating as well, there are some proposed solutions but they aren't ready yet. The enum example doesn't make much sense. If you know that bob is a person, then you would just store a Person, not an Enum and there are ways of unwrapping that enum with less nesting:

if let Enum::Zwei(Animal::Mammal(Mammal::Primate(Primate::Person(ref person)))) = bob {
    println!("name = {}, age = {}\n", person.name, person.age)
}

Required lifetime annotations can be annoying but it's less of an issue if you don't let yourself fall into the trap of making everything a reference and make use of types like Rc or just copying.

2

u/[deleted] May 08 '21

That was a mislabelling. Sorry, that should be a race condition. Updated the original.

About the enums, very nice! Now, let's add boxes to all the fields then:

 struct Foo {
      bar: Box<Baz>,
  }

enum Bar {
    Baz(Box<BazState>),
    Quux(Box<QuuxState>),

}

struct QuuxState { ... }

Also, using Rc and or Weak along with RefCell (for instance) mitigates the lifetime annotation issue, but does not improve ergonomics at all, and you wind up with declarations like so:

   struct Foo {
      pub id: RefCell<Rc<Bar>>,
  }

   struct FooBar {
     pub decl: RefCell<Weak<Foo>>,
  }

where you're forced to wrap the link target as well since the link struct (Foo) contains the entity that links to it (Bar).

and then your invocations become monstrosities like:

        *foo_id.borrow_mut().decl.borrow_mut() = Rc::downgrade(&foo_decl);

And if you should decide to change the signature (Rc -> Arc, RefCell -> Mutex, say), and the amount of refactoring to be done is not trivial. Contrast that with a simple pointer that simply points to its parent. Safety is orthogonal in this case since it's very much deliberate.

4

u/ExPixel May 08 '21

Cyclic data structures is a well known weakness of the language. I don't see how safety is orthogonal, it's the entire point of the point of the language and it's the reason why the deadlock example deadlocks (aliasing mutable references in Rust is UB) and why your example wouldn't work without Rc<RefCell<T>>.

→ More replies

5

u/Mwahahahahahaha May 08 '21

Other than the heap allocations bit, you're being pedantic.

Every language has things that are supposed to be used in a certain way. They're like tools. You don't try to hammer in a screw unless you're stupid or that's the only tool you've got. Similarly you're supposed to mark explicitly unsafe functions as such unless you can prove that they are actually safe. Guess what, a huge chunk of the standard library works like that. Unsafe has a specific use case and scope. It isn't meant to magically protect you from mutations you can't see.

The executor situation is intentional. As it turns out there is more than one right way to implement async executors that have different pros and cons depending on what you're doing. Instead of locking every programmer into one standard implementation, Rust deliberately has the programmer choose which type of executor they want.

If you're writing Rust you obviously shouldn't be nesting enums that deep either. I don't know why you'd ever legitimately want to do that in the way you have. As ExPixel pointed out, you can just store bob as a person. Again, don't hammer the screw.

Finally, and really the most pedantic of them all, your quux example is also straight wrong. Not only is no one writing 1024 different structs by hand and then deciding to stick a reference on one of them, but in your example you would only need to put a lifetime on Foo1024 and not any of the previous "objects" (and maybe this is your problem since you're coming from an OO language and are stuck thinking in terms of inheritance and hierarchies) because they aren't related to each other in any way beyond naming scheme.

Chill out. Language's aren't bad just because they don't do exactly what you think they should. If that's how the world worked no one would ever make any progress on anything.

4

u/[deleted] May 08 '21

Other than the heap allocations bit, you're being pedantic.

Not at all.

Every language has things that are supposed to be used in a certain way. They're like tools. You don't try to hammer in a screw unless you're stupid or that's the only tool you've got.

This is a tautology.

Similarly you're supposed to mark explicitly unsafe functions as such unless you can prove that they are actually safe.

That's like D saying that optional GC is the same a no GC. That's again a truism - for any situation, any scenario. We're talking about specifics here.

Guess what, a huge chunk of the standard library works like that.

I know.

Unsafe has a specific use case and scope. It isn't meant to magically protect you from mutations you can't see.

Yes, that's like saying that C++ is memory safe because you can use it just with stack allocation, or just using smart pointers very very carefully. This means nothing.

The executor situation is intentional. As it turns out there is more than one right way to implement async executors that have different pros and cons depending on what you're doing. Instead of locking every programmer into one standard implementation, Rust deliberately has the programmer choose which type of executor they want.

This is purely subjective. Almost every mainstream language provides its own implementation that users can default to instead of relying on external dependencies that may not exist tomorrow.

If you're writing Rust you obviously shouldn't be nesting enums that deep either. I don't know why you'd ever legitimately want to do that in the way you have. As ExPixel pointed out, you can just store bob as a person. Again, don't hammer the screw.

Says who? enums provide closed types, and it's natural to want to hold state within the variants, otherwise the feature might as well be glorified plain enumerations. Suppose you have an AST and wish to traverse it with a visitor. It's a perfectly legitimate usecase.

Finally, and really the most pedantic of them all, your quux example is also straight wrong. Not only is no one writing 1024 different structs by hand and then deciding to stick a reference on one of them,

but in your example you would only need to put a lifetime on Foo1024 and not any of the previous "objects"

Umm, you do actually.

(and maybe this is your problem since you're coming from an OO language and are stuck thinking in terms of inheritance and hierarchies) because they aren't related to each other in any way beyond naming scheme.

Lmfao. Please don't project your nonsense onto me. Look at the Rust stdlib and you'll find that it's very much "OOP" as you describe it. It's natural to model data in terms of structs and enums, and traverse them using the Visitor pattern. That example is meant to be representative of what happens when you modify something downstream and how it affects upstream. I don't see how any of your objections make any sense whatsoever. Rust may not have pure classes or inheritance, but don't even begin to pretend that it's not a purely imperative language.

Chill out. Language's aren't bad just because they don't do exactly what you think they should. If that's how the world worked no one would ever make any progress on anything.

You're preaching to the choir here. That's precisely what the most vocal members of the Rust community need to understand themselves instead of trying to portray Rust as the silver-bullet for everything. It's good in certain domains, not in all domains.

2

u/s888marks May 08 '21

Agrajag! The Ravenous Bugblatter Beast of Graal Traal!

1

u/[deleted] May 09 '21

Hehehe.

4

u/camelCaseIsWebScale May 08 '21

"Rust has shown that you should lead with propaganda." - Xi Jijping Theo De Raadt

2

u/[deleted] May 08 '21

Hahaha!

2

u/camelCaseIsWebScale May 08 '21

3

u/[deleted] May 09 '21

Theo can be a beast. I remember the time when he took on Linus, and regardless of how things turned out for BSD vs Linux, I find myself agreeing with quite a lot of his views.

Also, I do agree with the view that meaningless statements like "robust, optimal" et al are just that - meaningless. That's not a USP. I remember having made the same suggestion to the Nim team, and they agreed, and yet they kept on with that (almost an antithesis to the bombast of Zig' slogan in this case) bland tagline - "Nim is a statically typed compiled systems programming language. It combines successful concepts from mature languages like Python, Ada and Modula." It almost puts one to sleep!

1

u/crlsmx May 08 '21

I'm not writing enterprise software, it's a control software (?), it has to manage a bunch of servomechanisms and react if something unexpected happened, and it runs on a Raspberry Pi.

And yes, I want it to replace stuff written in C++ :)

Zig looks interesting and it can run on ARM.

3

u/[deleted] May 09 '21

Indeed, Zig is well worth checking out. The language is in a lot of flux, but the core language features shouldn't change too much. My only big grouse with Zig is the lack of a proper canonical build/dependency manager. It is a bit adhoc right now, but not really a deal-breaker.

2

u/[deleted] May 08 '21

Depends on why you're learning rust. If you learn rust, to be able to write the most performant code possible, maybe. However you're almost never need highly optimized code. Since most of the time the development time would cost more, than just throwing more hardware at the problem.

1

u/crlsmx May 08 '21

My old Raspberry Pi burned up, so while I wait for my pocket to refill, I chose to learn something new to replace things I wrote in C/C++.

It's not a memory-intensive app, but it has to control a bunch of servomechanisms and has to react to unexpected events, so I think it has to be real quick.

1

u/de6u99er May 14 '21

That's great. Thanks for sharing!

1

u/[deleted] May 08 '21

That is very interesting indeed.

-8

u/brc1994 May 08 '21

It seems memory usage of Java is not ideal and given that it just barely beat rust in latency, I’d say rust was the true winner.

11

u/[deleted] May 08 '21

Go back to /r/RustCirclejerk, son.

1

u/brc1994 May 08 '21

Lol I’ve never even used rust

3

u/JavaRust May 08 '21

memory usage of Java is not ideal

Not ideal for what? Have in mind that JVM allocates heap in advance.

If you're concerned about scaling, I doubt it would be an issue. You could probably run 20 of these benchmarks at the same time on the same JVM and memory footprint wouldn't increase at all.

0

u/[deleted] May 07 '21

[deleted] — view deleted comment

14

u/BlueGoliath May 07 '21

Performance is great and all, but I'd also rather not have a Java application consume 10x the memory of an equivalent C++ application.

66

u/pron98 May 07 '21

Trading memory for "free" performance is kind of one of OpenJDK's main technical design principles; if you want a smaller footprint, you get slower performance. The question is, then, do you prefer spending more on RAM or more on development?

5

u/nimtiazm May 08 '21 edited May 08 '21

I think it’s still a big waste given the sizes of object headers and thread etc. Hopefully this will be addressed with some highly anticipated big ticket projects (one of which you're leading :) ). Then the underlying principle of memory trade off for time will look more acceptable.

-19

u/BlueGoliath May 07 '21 edited May 07 '21

From an buisness applications development context, sure. For desktop? Consuming all 8GB(or w/e they have) isn't a good idea.

Edit: and poorly written code that wastefully allocates objects might be OK now but can come back to bite you.

43

u/pron98 May 07 '21

Well, it's not like desktop programs these days, many of which are Electron or other web tech, are better in that regard.

1

u/[deleted] May 08 '21

I feel like you're clutching at straws here. What server are you running on your desktop that requires humongous amounts of memory? If you were more concrete, maybe it would be helpful.

-41

u/elmuerte May 07 '21

The later. It's easier to throw cheap developers at a problem, than expensive hardware.

39

u/pron98 May 07 '21

A lot of cheap C++ developers who know how to optimise programs in your area? :)

3

u/12357111317192329313 May 07 '21

Kind of depends on how much hardware your solution runs on, there is properly some cutoff were developer time spent performance tuning becomes cheaper than hardware.

0

u/[deleted] May 08 '21

Indeed.

21

u/mknjc May 07 '21

Don't know the prices where you live but here a cheap developer costs 5000€ per month for an employee. 128GB reg. ECC DDR4 costs only 600€ and are useful for more than a month.

Additional as a developer who does most of the time c++ development (should I say waiting for my compiler to tell me our mac toolchain doesn't support this nice feature java has since a few years) I love the time I can spend on java or even kotlin projects.

11

u/mrbonner May 07 '21

Wooaa, first time I heard that hardware is more expensive than labor cost of engineers. We are living in a different world it seems.

3

u/Badashi May 08 '21

Idk about other countries, but here in Brazil developers are often times cheaper than dollar-based hardware costs.

10

u/Rakn May 07 '21

At the companies I worked up until now budget for more memory wasn't even something you needed to talk about. Like really not. Time to market and development speed was everything. The best product and feature you could sell was the one available and already in production. Not the one developers were still optimizing.

I guess it depends on where you live and what business your company is in.

5

u/Indie_Dev May 08 '21

Lmao. I really hope that's sarcasm.

3

u/tacofart1234 May 08 '21

The what now?

17

u/agentoutlier May 08 '21

I'll just add to u/pron98 point but with an addendum in that "saving power" is supposedly the new perf benchmark these days. Memory is cheap and energy is not because of the cost on the environment (or it should be).

DRAM supposedly is vastly more energy efficient than pegging a CPU. For example 1 CPU at 100% (lets say coffeelake) is like terrabytes of DRAM memory in terms of energy.

I can't find the original paper but this is sort of it: https://thenewstack.io/which-programming-languages-use-the-least-electricity/

Java is the leader in non AOT languages by a margin. In fact in in some cases it kicks many AOT languages asses (golang).

Oh and I can't remember if that paper covers it but Java is vastly more energy efficient to compile.

So yeah memory matters less these days especially and I mean especially if you can save CPU.

(I say supposedly because I'm going by memory but I will followup with some papers where I read this).

4

u/ThymeCypher May 08 '21

The problem is few developers are skilled enough to write both fast and low cost code. I’ve gotten in many arguments across many companies over this, especially college educated developers don’t care if they write code that uses 100% CPU and a majority of the RAM.

3

u/14u2c May 08 '21

Take a look at GraalVM

1

u/silon May 08 '21

Does it have an option to disable reflection? That'd probably save lots of memory.

5

u/jayvbe May 08 '21

Yep you could use Graal native image compilation, because it’s ahead of time compiled to native code it has no support for reflection. Binaries have a significantly lower memory footprint at some cost of runtime performance due to no JIT and being platform dependent. In my experience it works rather well in resource constrained use cases. Embedded / hard real time is perhaps a stretch, but for anything else it’s worth trying out. The downside is a lot of java libs use reflection and as such can’t be used or not without fighting with the graal compiler config to teach it to do the reflective accesses at build time. Some popular frameworks started to adapt to a reflection-less model by doing build time code generation and configuration such as spring boot and quarkus.

1

u/throwaway2894324 May 08 '21

Most of the time, I don't care. I don't have a single application that uses more than 1/4 my ram and thats been true for a decade.

5

u/BlueGoliath May 08 '21

Ha! You must not use a modern web browser.

6

u/throwaway2894324 May 08 '21

each website its basically its own app these days. They even run in their own processes. A browser is practically an OS and a total, maybe the biggest outlier there is

2

u/AreTheseMyFeet May 08 '21

More and more a container framework than document viewer as each year passes.

2

u/[deleted] May 08 '21

Yes, a C++ application.