this post was submitted on 13 Nov 2024
168 points (98.3% liked)

Programmer Humor

32469 readers
443 users here now

Post funny things about programming here! (Or just rant about your favourite programming language.)

Rules:

founded 5 years ago
MODERATORS
 
you are viewing a single comment's thread
view the rest of the comments
[–] [email protected] 2 points 9 hours ago (1 children)

I agree that they fit different niches! My point was that with modern CPU architectures, imperative languages make it much easier to write fast&efficient code just because the hardware was pretty much engineered with C in mind. IMHO Rust offers the best of both worlds when it comes to systems/low-level dev.

[–] [email protected] 3 points 9 hours ago (1 children)

That's not actually true, modern architecture works much closer to the way functional languages work https://queue.acm.org/detail.cfm?id=3212479

[–] [email protected] 2 points 5 hours ago* (last edited 5 hours ago) (1 children)

Modern C compilers are a fascinating blend of functional and imperative, that's true; and I didn't say that C is "close to how the modern architectures work". However, mainstream modern architectures are almost always engineered with C in mind primarily, and this is also acknowledged in the article you've linked. Rust, having a lot of similarities to C in terms of its underlying memory model, calling conventions, and control flow primitives, can often benefit from those hardware patterns and optimizations in a way that's more difficult to replicate with a functional language (especially so given most of them are GC-d due to their memory model). The closest I've seen in terms of easy-to-write-quick-code is OCaml, but even there the fast paths are often written in a very much imperative style. Idris2 also seems promising if they manage to get a GC-less mode working. Maybe also Roc, but I've not taken a look at it yet.

Note that I write all of this as someone spending a lot of their work time programming in a functional language (Haskell), with Rust being mostly for hobby stuff. It just always surprises me how much easier it is to write fast code in Rust, and yet also how much of my Haskell intuition was applicable when I was learning it.

[–] [email protected] 1 points 5 hours ago (1 children)

Typically this is true, but it's certainly possible to get comparable performance with functional style. Carp, which I linked above, basically uses the same approach to memory management as Rust. It doesn't rely on GC.

I also find that for most cases it really doesn't matter all that much unless you're in a specific domain like writing drivers, making a game engine, etc. Computers are plenty fast nowadays, and ergonomics tend to be more important than raw performance.

[–] [email protected] 2 points 3 hours ago (1 children)

Typically this is true, but it’s certainly possible to get comparable performance with functional style

It's possible, but you have to specifically write code that's fast, rather than idiomatic or ergonomic, and you have to know what you're doing. At that point, you may have been better off writing it in something else. I feel like OCaml is good at this because it allows you to write abstractions and main control flow in a functional way and hot paths in an imperative way without switching language, but so is Rust.

Carp, which I linked above, basically uses the same approach to memory management as Rust. It doesn’t rely on GC.

I'll take a look, thanks!

I also find that for most cases it really doesn’t matter all that much unless you’re in a specific domain like writing drivers, making a game engine, etc. Computers are plenty fast nowadays, and ergonomics tend to be more important than raw performance.

I mostly agree with you, e.g. Haskell and Clojure, despite being "slow", are plenty fast for what they're used for. On the other hand, I'm very much annoyed when "user-facing" software takes way too long to load or do simple tasks. Java in particular is pretty bad at this: JOSM (Java OpenStreetMap editor) takes longer to load than my entire desktop startup, including a window manager and browser. Unfortunately it's also the best editor around, so I pretty much have to use it to edit OSM, but it still annoys me to no end. Unnecessary computations, IO inefficiencies and layers of wrapping also affect the power consumption quite noticeably.

[–] [email protected] 2 points 2 hours ago

Functional programming tends to get conflated with static typing and immutability, but it's a much broader paradigm than that. I find it really depends on the specific language. It's very easy to do local mutation in Clojure for example. There are constructs like transient specifically designed for doing that. There are also languages like Janet that are functional, but use mutable constructs by default and can embed in C with very low footprint.

I generally get annoyed with software being slow to startup and hogging resources as well, but that's a separate discussion. The JVM is designed primarily for servers where you have long running processes, and startup time isn't really a big concern. So, it's not the best illustration. A CL app written using something like clog will be very snappy.

Joe Armstrong made a good point a while back as well. He pointed out that it's better to focus on making the compiler smarter so that it can do the optimize performance, and optimize languages for human readability. I very much agree with that sentiment. We can see an example of that being done with GraalVM which can compile JVM bytecode into optimized native apps that have very fast startup time and use far less resources than the JVM. At the moment it's not comprehensive, but you can make GUI apps with it, and they'll even run on RPi.