Transient Data Structures

Transient 

:our-clojure:

Immutability is GOOD

Performance is NECESSARY

If a tree falls in the woods, does it make a sound?
 

If a pure function mutates some local data in order to produce an immutable return value, is that ok?

Clojure data structures use mutation every time you call, e.g. assoc, creating one or more arrays and mutating them, before returning them for immutable use thereafter.

  • Performance -  you simply can’t get as fast using only pure functions and immutable data.

  • Being immutable and persistent is essential to robust programs

  • Clojure mutates the internal nodes of its data structures - You'll never see this data

  • Call transient on your collection. This creates a transient copy.

 

  • Use the same operations you normally would, but using the ! variant, which is made for working with transients.

 

  • At the end, convert it back to an immutable collection with persistent!.

Main Use Case

Enable controlled and isolated mutation to and from persistent data structures, removing the overhead associated with creating many persistent copies that are never going to be shared. 

PERFORMANCE

Side Effects & Warn

 

 

 

 

  • Transient Data Structures suitable for single-threaded programs - Requires thread isolation

Use of same transient instance by multiple threads can lead to unpredictable results

 

 

 

 

 

  • Can’t use after returning a persistent version

  • Transients should only be used locally, where you have total trust in the code. Never let a transient collection slip out of your control.

Reference

https://clojure.org/reference/transients#:~:text=Transient%20data%20structures%20are%20always,existing%20persistent%20Clojure%20data%20structure.&text=You%20obtain%20a%20transient%20'copy,has%20the%20same%20performance%20characteristics.
 

https://hypirion.com/musings/understanding-clojure-transients

 

https://pt.slideshare.net/mohitthatte/a-deep-dive-into-clojures-data-structures-euroclojure-2015

https://titanwolf.org/Network/Articles/Article?AID=1de0c17d-876c-4dad-9fff-03200c1e4ff0#gsc.tab=0

https://groups.google.com/g/clojure/c/zdYsxH1K0B0/m/OGm5YSGKNZcJ?pli=1

https://livebook.manning.com/book/clojure-the-essential-reference/chapter-20/v-29/19

Thanks

Clojure - Transient Data Structures

By Tales Baz

Clojure - Transient Data Structures

Nu Lightning Tech Talk

  • 1