r/programming • • 4d ago

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.

81 Upvotes

12 comments sorted by

6

u/Smallpaul 4d ago

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

2

u/bosyluke 4d ago

Thank you

13

u/fiah84 3d ago

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 4d ago

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 3d ago

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.

7

u/youngbull 3d ago

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 3d ago

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 3d ago

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/GoAwayLurkin 4d ago

What Weissman score ?

2

u/bvimo 3d ago

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