Sean Parent has a conference video out there about how undo in Photoshop is implemented by breaking the image into tiles then making a list of the history of each tile. This allows the “History Brush” to reach into the past and paint old pixels over new ones.
However, the copying becomes impractical from a performance point of view if the resources are big, so immutable resources offer an alternative that does not have the performance issues.
For example, if you have multiple threads computing sub-graphs of a graph and passing those sub-graphs around to other threads, copying the whole graph each time a thread needs to compute something that may potentially alter the graph is a killer for performance. You then have two choices:
Choice 1: make the graph thread-safe. Which is the usual route C++ takes.
Choice 2: make the graph out of immutable data structures.
Choice 1 is the practical choice when the scope of the work is limited, and therefore thread safety is easy to implement.
When the problem at hand becomes very complex though, and it's difficult to reason about the ordering of locks, immutable data structures is an easier choice.
Pretty impressive.
The same simplicity applies analogously to various other use cases which would be tedious and error prone to implement without immutable data structures.
Here's a document[1] explaining how to make a tree-like data structures persistent along with a motivating example of their use (at the end). The wikipedia page[2] for the problem also goes into other techniques and has some diagrams too.
tl;dr a naive approach to the point location query problem achieves O(N^2) space and O(logN) time per query, while simply modifying the data structure to be persistent achieves O(N) space while retaining O(logN) time per query.
[1] https://saco-evaluator.org.za/presentations/camp3_2017/persi... [2] https://en.wikipedia.org/wiki/Point_location
The linked document only mentions high-level examples, all of which seem to be trivially implementable via mutable data structures and a bit of being careful.
Does this buy anything beyond peace of mind that if you pass an immutable DS to another thread, they won't be able to change it?