News for hackers. Stuff that matters.
Re:How to write slow Rust code by sharikone on Monday August 02, 2021 @09:51AM (#28036489)
Conclusion is sound. Java/Rust/C/C++... are all fast enough that the bottleneck does not reside with the choice of the language.

I wonder about how Common Lisp can be this fast however. I would like comparisons with languages like Python, JS and Lua.

Re:How to write slow Rust code by Hemospectrum on Monday August 02, 2021 @10:10AM (#28036729)
Common Lisp has always had pretty good support for compiling to fast machine code. Most people underestimate it, for at least two reasons. One is that it doesn’t have good standing in popular benchmarks relative to C, even with speedy implementations like SBCL, and the common takeaway from such benchmarks is that a language is either as fast as C or infinitely slow. The second reason is that the process of building standalone executables is less accessible than in other languages; even to people familiar with Lisp’s strengths, it’s kind of arcane, giving off the impression that deploying CL code in production is an unsolved problem. Also, Lisp is an old language family, and the quirks of Common Lisp in particular are weird and unfamiliar when coming from any modern language, even Clojure.
Re:How to write slow Rust code by tialaramex on Monday August 02, 2021 @10:51AM (#28037230)
> the bottleneck does not reside with the choice of the language.

I think that's too bold a claim. Your code might be too slow in any of these languages because you made poor algorithmic or data structure choices but once you have made the best possible algorithmic and data structure choices you may reach a point where you can't express the program with the choices you need and get the desired performance in some languages.

At that point, in the immediate present, you have to choose the tool that actually gets the job done. It doesn't matter if C++ could totally hit your numbers "once that LLVM optimiser bug gets fixed" or if Java could do it "when they port the new JIT to our platform", you need the thing that works now.

Over the medium term, language evolution makes an impact here. Once upon a time C++ didn't have move semantics. Obviously in some cases your compiler can move as an optimisation, but in other cases if you can't ask for one explicitly you're often out of luck. However C++ has a specific performance goal ("Leave no room for a lower-level language") that guides them to find a way to get great performance, and they changed the C++ language to fix this. If your ideal solution is slow in C++ then the C++ committee ostensibly wants to fix that. Just maybe not this year. In contrast for Java performance is not a primary goal and sometimes Java is OK with worse performance. In this particular essay it happens for RAM consumption. This problem shouldn't run out of RAM on your mid-1990s server at the scale demonstrated, but the Java solution would either run out of RAM or spin up the GC mid-solution (and thus ruin speed).

No argument that "I'll rewrite it in Rust/C++" is just as much the wrong first instinct as "I'll hand roll the assembler". But if you're under-performing with the right algorithm you need to either spend money on more (faster/ bigger) hardware or on rewriting in the language that might buy you that last drop of performance.

Re:How to write slow Rust code by pjmlp on Monday August 02, 2021 @10:21AM (#28036871)
Lisp was used to write full OSes, naturally it is fast.

Python, JS and Lua are not in the same league of AOT and JIT compiler technology available to Lisp, even with all the money spent on stuff like V8, JS JIT cannot compensate for the fact that they lack the ability to provide the required semantics to AOT code with good enough performance to write a Xerox PARC graphical workstation (see Interlisp-D).

It is no wonder that Julia, a Lisp like language with Algol syntax, is the only modern dynamic language on the HPC club.

https://www.hpcwire.com/off-the-wire/julia-joins-petaflop-cl...

Re:How to write slow Rust code by Banana699 on Monday August 02, 2021 @12:39PM (#28039007)
I think Julia being an HPC lang despite dynamicity has nothing to do with being close to lisp in some regards.

I didn't use the language but the overwhelming community consensus I get is that performance is out of the window once you truly embrace dynamic (type-unstable in julia parlance) code. Julia performance depends heavily on the compiler being able to infer a single type for variables in the hot loop so that it can specialize the used functions to it then get out of the way. Julia is not a Fortran-speed python, it's a language that can be _either_ Fortran _or_ python, depending on how you write it.

Lisp influences on julia are more for semantic power than performance.

Re:How to write slow Rust code by pjmlp on Monday August 02, 2021 @02:31PM (#28040748)
Depends on how one sees it,

https://www.researchgate.net/publication/267008293_Fast_floa...

The point being what dynamic homoiconic languages are able to achieve when the community actually embraces the push for performance instead of hand waving and rewriting everything in C.

If the JavaScript community had the same attitude as Python one, surely we would still be doing HTML 4 without pesky SPAs.

Re:How to write slow Rust code by bjoli on Monday August 02, 2021 @10:10AM (#28036727)
SBCL is fast. People seem to lump together dynamically typed languages, but the reality is that python and ruby forgot all the things learned during the 80s and early 90s.

My experience is that small programs like this are usually at least an order of magnitude faster in SBCL compared to python. At least if the python code spends some time in actual python instead of offloading to C.

Re:How to write slow Rust code by omginternets on Monday August 02, 2021 @10:28AM (#28036951)
>ruby forgot all the things learned during the 80s and early 90s

Out of curiosity, what things are you referring to?

Re:How to write slow Rust code by bjoli on Monday August 02, 2021 @12:07PM (#28038492)
From the top of my head before I get off the bus:

Things like proper static modules so that you know what is being called so you can elide lookups at call-time. That is a pretty big omission, TBH.

For a long time (still?) python had a horribly inefficient value representation in memory. Now, integer tagging is probably no fun to add as an afterthought, but it shouldn't have been an afterthought.

Exception handling is sloooow. And iterators use exceptions to signal that younhave exhausted the collection. Because iterating over a complete collection is exceptional.

I understand the need to keep cpython simple, but some of these things ARE simple and have been well understood since a decade before python 1.0.

Re:How to write slow Rust code by orf on Monday August 02, 2021 @12:30PM (#28038843)
Static modules with elided lookups is completely nonsensical in Python.

StopIteration is special-cased and has negligible overhead. See here (https://www.python.org/dev/peps/pep-0234/) for why exceptions are used.

Re:How to write slow Rust code by bjoli on Monday August 02, 2021 @05:08PM (#28042656)
On the contrary. Static modules would mean static classes with no possibility of changing a class. That would be another pretty nice optimization you could do, making class lookups a lot faster. I am not alone in thinking this would be a good idea. The Instagram folks wrote about it, iirc.

That rationale for using an exception is only valid because CPython procedure calls are expensive. A proper optimizing compiler will have a hard time rewriting that implicit try/catch into a goto.

I think Dart nailed iterators in this regard: no special values, and a protocol that is transparent to the compiler, which has the benefit of requiring no special cases at all. In the current state of cpython, of course, that falls into the "2 expensive procedure calls that can't be inlined because everything can be mutated at any time" type of a problem.

Re:How to write slow Rust code by omginternets on Tuesday August 03, 2021 @09:21AM (#28048800)
>Static modules would mean static classes with no possibility of changing a class. That would be another pretty nice optimization you could do, making class lookups a lot faster.

I think the whole point of python is to make everything dynamic. It seems like this trade-off is voluntary, i.e. it is a feature, not a bug.

Re:How to write slow Rust code by likeabbas on Monday August 02, 2021 @10:02AM (#28036639)
There’s a distinct difference between Java and Rust/C++ which is that Java has a stop-the-world-GC and rust/c++ don’t. And in large scale systems, Java’s GC is usually the limiting factor.
Re:How to write slow Rust code by nobleach on Monday August 02, 2021 @10:20AM (#28036852)
Newer versions of Java have more interesting GC implementations that reduce that "stop the world, swab the decks" performance. The fine engineers working on the JVM keep finding ways to incrementally garbage collect. It's getting to a point where it's imperceptible in some applications. Granted, if you're writing high-speed/high-frequency trading apps for Wall Street, Java still isn't there. Rust could be the logical next step in that arena. (Although, most C++ devs I know in that space don't hate C++)
Re:How to write slow Rust code by maccard on Monday August 02, 2021 @11:05AM (#28037460)
> if you're writing high-speed/high-frequency trading apps for Wall Street, Java still isn't there.

Stop-the-world GC doesn't stop you from using Java for high-speed apps, unpredictable stalls do. It doesn't matter if your GC takes 2 seconds to run fully as long as it doesn't kick off during your hot loop, and the opposite is true; there will never be a low enough overhead "magic" GC that can be run in the background that may cause a microsecond stutter on your finely tuned trading algotirhm, it has to be explicit.

Re:How to write slow Rust code by lordnacho on Monday August 02, 2021 @10:24AM (#28036899)
> if you're writing high-speed/high-frequency trading apps for Wall Street, Java still isn't there

A fair few trading system job ads seem to be for Java. I also wrote code that faced a Java server at various banks. Seems to be fast enough for certain market related applications.

Re:How to write slow Rust code by magila on Monday August 02, 2021 @11:45AM (#28038171)
The use of Java in trading systems is widely misunderstood. Java is typically used to implement the high level business logic where microsecond latency is not required. The output of the Java service is feed to another system which handles the actual trade execution. That part is typically done with C++, or FPGAs driven by C++.
Re:How to write slow Rust code by nobleach on Monday August 02, 2021 @03:58PM (#28041838)
This was the method used by my former employer. Are entire backend was Kotlin/Java/Scala... and a couple of NodeJS apps. Right up until the very edge where C++ handled the interface with the exchanges. We also did the typical thing of having our servers as physically close as possible (in New Jersey).
Re:How to write slow Rust code by tuxman on Monday August 02, 2021 @11:43AM (#28038142)
From my understanding of previous talks on this, a fair amount of HFT's and trading firms use Java. The trick is that the trading day is a well-defined window with clear starts and stops. So if you load up your server with huge amounts of memory, you can get away with never calling GC during the trading day. The GC can be paused until after hours or just kill the program and start it up before market open the next day.
Re:How to write slow Rust code by nobleach on Monday August 02, 2021 @03:56PM (#28041812)
One solution I had been dying to implement is the Erlang style of letting a Kubernetes POD OOM, and just respawning it. If the app performs well right up until it runs out of memory, I see no reason to have a GC turned on at all. (obviously there ARE reasons, I just wanted to try this approach)
Re:How to write slow Rust code by pjmlp on Monday August 02, 2021 @10:17AM (#28036820)
Yeah, like 1ms when handling heaps of 3TB sizes, when most people are still complaining about 16 GB on laptops, I can live with that.
Re:How to write slow Rust code by Diggsey on Monday August 02, 2021 @10:32AM (#28037005)
Firstly, 1ms for a 3TB heap size is very unrealistic even for modern JVMs.

Secondly, the figures that are often quoted for how long the program must be stopped are for the "happy path" where enough memory can be freed that the program can continue without issue.

In practice, what often happens with large heap sizes is that the GC starts lagging behind because there's not enough memory head-room. In these cases, the GC stops the world for much longer, because the program hangs on a memory allocation that cannot be satisfied until the GC completes.

This primarily affects programs that have high rates of allocation. You can avoid this in Java with effort, but you end up with very non-idiomatic code.

Re:How to write slow Rust code by pjmlp on Monday August 02, 2021 @10:36AM (#28037049)
Enjoy, https://malloc.se/blog/zgc-jdk16

I guess Oracle, IBM, Azul, PTC, Microsoft can provide the necessary data about their pauseless GC implementantions to any relevant customer.

Re:How to write slow Rust code by native_samples on Monday August 02, 2021 @05:12PM (#28042704)
Modern JVMs have a GC that has O(1) pause times, often of less than 1msec regardless of heap size. In ZGC and Shenandoah, GC is fully concurrent and pauses are basically imperceptible. Even walking the stacks to locate pointers into the heap is done whilst the application is still running.
Re:How to write slow Rust code by nlitened on Monday August 02, 2021 @10:17AM (#28036819)
If I understand correctly, modern Java has multiple GC engines available (that can be chosen with a command-line switch), some of which are either fully concurrent or with sub-millisecond stop times.
Re:How to write slow Rust code by divs1210 on Monday August 02, 2021 @10:24AM (#28036902)
SBCL and Chez Scheme are probably the most advanced dynamic language environments around.

Unfortunately, CL has started to show its age (case insensitive, different namespaces for variables and functions, weirdly named stuff, etc.) and Chez Scheme doesn't have a lot of libraries.

In my experience, Chez Scheme is very small, blazing fast, and VERY high level. It is a great platform to use as a target language for compilers / interpreters. Racket has chosen to go this route, for example.

Re:How to write slow Rust code by phoe-krk on Monday August 02, 2021 @10:49AM (#28037213)
Can't say that different namespaces are a sign of Lisp showing its age. At least in my mental model, it is convenient to know that a single symbol can name several different things in different namespaces at the same time - e.g. a variable, a function, a type, and a few more that are possibly defined by the user.

Enforcing the "strictly one meaning per symbol" paradigm seems pretty self-defeating to me, since even in a Lisp-1 users can - and eventually will - create their own namespaces, implementing a Lisp-N themselves.

Re:How to write slow Rust code by pjmlp on Monday August 02, 2021 @10:59AM (#28037328)
Kind of, SBCL doesn't have the tooling of Allegro or LispWorks.
Re:How to write slow Rust code by gameswithgo on Monday August 02, 2021 @01:02PM (#28039352)
fast enough for this workload. If you were trying to maintain 144fps with some kind of 3d interface with no frame drops, any garbage collector will be a challenge.
Re:How to write slow Rust code by ostenning on Monday August 02, 2021 @12:36PM (#28038965)
Not for embedded environments