News for hackers. Stuff that matters.
Re:How to write slow Rust code by mamcx on Monday August 02, 2021 @12:08PM (#28038514)
>and never just a function of the language you chose.

I understand your point, but is important to know how NUANCED is this.

Performance is MOSTLY driven by the inherent design choices of each language(+ your own!). If you are on Gc, that choice is an impact for everything. Rust have Vectors as base, and lisp have GC-backed linked list. That is another deeeeep choice.

But IF you know (or hit the luck!) which are these designs, and understand that not matter which language everyone wanna gotta fast and most common operations (the ones for what THAT lang was designed for!) are fast enough... then all is ok, in average.

Is when you go against that design that you see how the illusion crack a little. Then, is all about how much you can workaround about it to recover most of the performance

Re:How to write slow Rust code by Zababa on Monday August 02, 2021 @01:49PM (#28040096)
That is very true, for example Java, even while being as fast as Rust and Lisp, takes way more memory than both of them.

My point was more for myself (and maybe other people that haven't heard a lot about performance), as I had very strict ideas about it, but thanks to articles like that I can understand how it's more nuanced. Software engineering and computer science are both really vast and deep fields, but a lot of information is here online, shared freely by awesome people. That means that for a lot of things, being aware of something is enough to make good decisions. These types of articles allows me to be more aware of the nuances of performance (and how complex it is as a topic).

For example, if I'm just trying to optimize the speed of my algorithm, and only know Java, Java might be enough. But if I'm trying to optimize my cloud spendings, considering the memory consumption of Java, it might not be the best choice.

Comments like yours are also a great way to learn more. I know that linked-lists are really good in Lisp (and OCaml too!), but with other languages there's not often the best choice.

Re:How to write slow Rust code by native_samples on Monday August 02, 2021 @05:13PM (#28042730)
We don't really know how much memory Java needs on this benchmark. The JVM will use as much memory as you give it. Its viewpoint is, if you tell me I can use 300mb then I'll just not do any GC work until I've used 300mb. Why work harder?

To know how much memory you really need, you'd need to keep shrinking the Xmx max heap size setting until performance becomes unacceptably poor. However, that isn't done here.

Re:How to write slow Rust code by nybble41 on Wednesday August 04, 2021 @09:37PM (#28068729)
> We don't really know how much memory Java needs on this benchmark. The JVM will use as much memory as you give it.

  ## /usr/bin/time java -Xmx48M -cp build/java Main2 ...
  90.14user 1.36system 1:29.07elapsed 102%CPU (0avgtext+0avgdata 147396maxresident)k
  0inputs+288outputs (36major+37251minor)pagefaults 0swaps


  ## /usr/bin/time ./phone_encoder ...
  29.91user 0.08system 0:30.01elapsed 99%CPU (0avgtext+0avgdata 16704maxresident)k
  0inputs+0outputs (0major+4940minor)pagefaults 0swaps
I used the default dictionary, and the input file with 10 million numbers was generated with "java -cp build/util util.GeneratePhoneNumbers 10000000 true".

So… I gave the Java version over 2.25x as much heap memory as the maximum resident size of the Rust version (./phone_encoder), but it actually used almost 100 MB more than that, and still took 3x as long to finish. I also tried with -Xmx40M:

  ## /usr/bin/time java -Xmx40M -cp build/java Main2 ...
  270.91user 4.30system 1:40.23elapsed 274%CPU (0avgtext+0avgdata 150588maxresident)k
  0inputs+480outputs (60major+35615minor)pagefaults 0swaps
Here we start to see a substantial performance hit due to excessive GC, and maximum resident size has actually increased slightly. Elapsed time is only about 11 seconds longer than with -Xmx48M due to parallelism, but total CPU time (user + system) has ballooned to 274 seconds—nine times the Rust version.

This is comparing against my optimized Rust implementation[0], not the one from the article. It employs the same algorithm but eschews heap allocation. I used /usr/bin/time rather than the included benchmark_runner because benchmark_runner makes use of what appear to be MacOS (Darwin) APIs (libproc::libproc::pid_rusage) to measure memory use and consequently doesn't build on my Linux system with this functionality intact.

[0] https://github.com/nybble41/prechelt-phone-number-encoding

Re:How to write slow Rust code by native_samples on Thursday August 05, 2021 @02:51PM (#28077903)
Well, that answers that then!
Re:How to write slow Rust code by Zababa on Monday August 02, 2021 @07:27PM (#28044003)
That's a fair point, however the other programs use less memory out of the box without any need for tuning, so that's a win for them.
Re:How to write slow Rust code by pjmlp on Tuesday August 03, 2021 @05:33AM (#28047238)
Optimizing for the sake of optimizing is wasted money.

If the customer has given you a 300MB budget and the application is able to perform its job with 180 MB, and keep the customer happy, there is no point paying for development costs to bring it down to 50 MB.

There are other projects to move into, instead of burning money with non-existent project delivery acceptance criteria.

Re:How to write slow Rust code by Zababa on Tuesday August 03, 2021 @11:23AM (#28050266)
The whole premise of this article was to optimize programs for speed, which is also pointless if your program is already fast enough. They also don't take into account programmer time spent to optimize the project, how easy it is to find programmers that work with that particular language and that can deliver performant software, etc.

Your criticism is valid, but I feel like it should be applied to this whole article rather than this particular point. I like how stuff like that is described on the Computer Language Benchmark Game: "… a pretty solid study on the boredom of performance-oriented software engineers grouped by programming language.". You could also probably substitute "boredom" with "ego".

Re:How to write slow Rust code by pjmlp on Tuesday August 03, 2021 @12:41PM (#28051278)
Indeed, it applies to any kind of optimization in general.
Re:How to write slow Rust code by AndrewDucker on Tuesday August 03, 2021 @04:36AM (#28046935)
Not using memory that's lying empty isn't a win. There's nothing better about "I didn't use a reusable resource that was lying about unused."
Re:How to write slow Rust code by Zababa on Tuesday August 03, 2021 @11:23AM (#28050275)
The same could apply to CPU usage, which is what this whole article is about.
Re:How to write slow Rust code by vips7L on Tuesday August 03, 2021 @08:08AM (#28048222)
IIRC it really depends on the chosen GC. ZGC will return memory back to the OS but older GCs will just hold on to the memory forever (up until -Xmx)
Re:How to write slow Rust code by native_samples on Wednesday August 04, 2021 @05:21AM (#28058931)
That's true but even modern GCs like G1 or ZGC won't do background GC work unless the program is idle. So for a benchmark like this it doesn't matter.
Re:How to write slow Rust code by pjmlp on Monday August 02, 2021 @03:46PM (#28041696)
Or when one fails to understand what is actually available to them on the toolbox and only look for what is visible at top level.

For example, yes Lisps have a GC backed linked list, but in the case of Common Lisp, they also have arrays, stack allocation and deterministic resource management macros.