r/programming • • Oct 23 '26

How close can pure FP get to C?

https://rtfeldman.com/fp-vs-c

Roc can achieve excellent performance as a pure FP language, I think people will enjoy reading this post which looks at a pure Roc implementation of libdeflate.

78 Upvotes

12 comments sorted by

6

u/Smallpaul Oct 23 '26

This is very cool and good luck on the next release.

2

u/bosyluke Oct 23 '26

Thank you

14

u/fiah84 Oct 24 '26

would you give up guaranteed memory safety, guaranteed lack of side effects, guaranteed deterministic behavior across all targets, and the security benefits of having no exploitable build scripts in packages...for an average speedup percentage in the single digits?

that's an excellent elevator pitch for this language and approach

23

u/youngbull Oct 24 '26

Really cool article and a pleasant writing style. A relief in the current slop economy. I just wanted to add that in rust, c and c++, even just a call stack overflow ends up in a segfault so it's a lot more difficult to write high assurance code than people think. There has also been exploits where std::sort was given a cyclical comparator which causes std::sort to exceed bounds.

10

u/-_one_-1 Oct 24 '26

In Rust, unless you're in no_std, stack overflow is guarded by the minimal runtime, so it doesn't segfault, it panics with clear message and a call stack.

Of course, it's still a crash, and I get your point.

8

u/youngbull Oct 24 '26

From what I understand, it sends the segfault signal then it prints the error message, so it's not a recoverable error. Not that one would design the program around recovering from a stack overflow, the only reasonable design for being fault tolerant in that case is to use separate processes.

5

u/-_one_-1 Oct 24 '26

Exactly. In fact I called it a crash, not a recoverable error.

To be noted, Rust is introducing a way to make tail calls explicit (becomes keyword). If one avoids recursion in the generated code, it's not that easy to encounter a stack overflow, and I would suggest that if the call stack is that deep, it's probably coming from a flawed design to begin with.

Of course, the more ways to make software impossible to crash, the better. Rust tries to strike a balance between predictable execution and performance, and as far as I can understand, all possible ways to have a growable stack come to the detriment of performance.

2

u/youngbull Oct 24 '26

Guaranteeing tail recursion is good. I think it's still difficult. There was a bug in SQLite with a quicksort implementation that had linear recursion behavior. Rolling your own stack on the heap helps, but you still have poor memory performance. The trick is to tail recurse on the largest partition, guaranteeing at most logarithmic amount of stack frames. https://youtu.be/V_qzqY1bb7I?is=ORwfRplC5KyTQLX2

4

u/-_one_-1 Oct 24 '26 edited Oct 24 '26

I'm not sure what you're talking about. The Rust feature I was talking about guarantees that if your function compiles, it's proven to be tail call recursive and guaranteed to be compiled to code that does not recurse, so it can't possibly lead to a stack overflow, ever.

This feature obviously only applies for tail recursion, and not for other kinds of recursion, where sometimes you roll your own stack as you described.

2

u/youngbull Oct 24 '26

Oh, I was just coming up with an edge case where it is not enough to guarantee tail recursion as you need to know to tail recurse on the right call (both calls cannot be tail recursed on).

-4

u/GoAwayLurkin Oct 23 '26

What Weissman score ?

2

u/bvimo Oct 24 '26

Big score my friend, one of the best scores. How Weissman's to a Chloe?