r/programming • • May 15 '18

Demystifying functional programming (in a real company)

https://medium.com/building-nubank/demystifying-functional-programming-in-a-real-company-e954a2591504
127 Upvotes

51 comments sorted by

View all comments

4

u/matthieum May 16 '18

But here’s where the magic happens: you’re not actually changing our-immutable-map; you're creating a new one that shares structure with our-immutable-map, but has a different value to :foo. Because it's so easy to share structure on this immutable world, everything is still quite efficient, memory-wise.

What's the performance cycles-wise?

As far as I know, the biggest issue with persistent data-structures is that representing arrays efficiently is an exercise left to the reader. There are multiple possibilities (finger-trees?), however I've never found one with performance anywhere close to a plain dumb array.

And this is a big issue, because the most efficient data-structures are built on top of arrays, as arrays play so nicely with CPU caches.

3

u/yogthos May 16 '18 edited May 16 '18

You can see the matrix of performance guarantees for Clojure data structures here, bottom line is that you end up with O(log32n) complexity for most cases. The important thing to keep in mind is that immutability is just the default. Clojure provides transients as a way to opt into mutability locally in cases where you need the additional performance. Ultimately, you can even drop down to plain old Java if you really wanted to.

The key part here is that Clojure makes it natural to write code without shared mutable state, and it takes a lot less discipline on the part of the user to structure applications in a way where you can safely do local reasoning.

1

u/matthieum May 16 '18

Thanks for the links.

The transients notion seems great.

The near-constant however is completely unclear. The performance of a data-structure is often a matter of the number of cache lines that one needs access to (as soon as the working set exceeds the cache size); and it is unclear to me how O(log32 n)1 performs here.

I expect, perhaps naively, that log32 n is proportional to the number of pointer dereference to access the element when indexing, and therefore the maximum number of page faults... though with a tree structure one can expect at least the root node (array?) to be warm in the cache since it's read on every access. However it's still pretty unclear what the memory layout is here. If naively based on the JVM, I'd expect 1 node to hold 1 array thereby requiring 2 or 3 memory allocations per level...

What's your experience with the performance of vector compared to a traditional Java array? What's the performance hit of "near-constant"?

1 It's not clear either why the use of log32, instead of log2. From a big O perspective, both are just a constant apart, so log32 is logarithmic... My supposition is that arrays of 32 elements are used in some kind of B-Tree arrangements, and thus log32 is used to hint at the branching factor...

1

u/yogthos May 16 '18

You can see a benchmark for Clojure vector here, note that's a few years old and there have been some improvements in the implementation since.

In practical terms, I've had very few situations where I needed to use transients over the years. My team has been developing Clojure applications for around 8 years now, and we haven't found any performance problems compared to similar types of applications we've developed with Java previously while writing idiomatic Clojure.

1

u/matthieum May 18 '18

That's great to hear!

I guess I am just a tad too obsessed for performance, coming at it from the angle of HFT systems.

1

u/yogthos May 18 '18

It's always a case of using the right tool for the job, this talk might be of interest if you're working with HFT.