This article was first published on my blog.
When we build an application that embraces data immutability, we handle changes in data by creating a new version of data instead of mutating data in place, without compromising on performance. There are various techniques to achieve data immutability in an efficient manner.
After reading this article, you will:
- ⚠️ Be aware of the limitation of path copying
- 🤗 Be motivated to read my book about Data-Oriented programming
Remark: This article assumes that you are already aware of the benefits of data immutability.
Structural sharing provides an efficient way to share data between multiple versions of it, instead of copying the whole data.
It's kind of similar to the way git manages multiple versions of your source code: git doesn't copy all the files on each commit. Instead, the files that are not changed by a commit are shared with previous commits.
The same strategy could be applied with data. Let's take as an example a map that represents the data of a library in a library management system. In this map, we have 4 fields:
name: a string for the name of the library
address: a string for the address of the library
catalog: a map with the book information
users: a map for the user information
Here is an example of a tiny library, with two users and a single book: