Why are Roslyn Syntax Trees Immutable?

Skip to first unread message

Shay Friedman

Jan 30, 2013, 6:28:55 AM1/30/13
to altnet...@googlegroups.com

After yesterday's question during my talk, I passed the question from the title to the Roslyn PM and this is the answer I received:

The short answer to why syntax trees are immutable in Roslyn is that it makes parallel work much easier.  You can take a syntax tree and pass it to any thread and not worry that someone else will mutate it while you are in the middle of doing analysis.  This is useful in the command line compiler so that multiple trees can have their methods bound in parallel (which may need to occasionally access information from a different tree), but it’s EXTREMELY important for VS scenarios where we want to have an extensibility model that allows many extensions to analyze and transform the same tree in parallel, and it doesn’t really work to have a model that forces all those separate extensions to co-ordinate locking a single tree.  Similarly, providing each extension its own copy of the tree would be prohibitive from a memory overhead point of view.


Hopes it makes sense now...

Thanks all the speakers and attendees for yesterday! hope you had a good time!

Shay Friedman |  CodeValue Co-Founder, Dynamic Languages and Web Technologies Expert  | Microsoft Visual C# MVP | Author of IronRuby Unleashed
Phone: +972-54-6160671 | Email: shay.f...@gmail.com | Blog: http://IronShay.com | Twitter: http://twitter.com/ironshay 


Jan 31, 2013, 7:42:24 AM1/31/13
to altnet...@googlegroups.com
Some more in-depth information on how the Roslyn Syntax Trees were designed and implemented, right from the horse's mouth:

Nir Rozen

Jan 31, 2013, 9:26:01 AM1/31/13
to altnet...@googlegroups.com
10x for forwarding the question!! :)

I still think it's a strange decision, since it only takes into account the option of using roslyn concurrently.
For example, I'm currently writing a console app to refactor parts of the code before build (non concurrently) and the immutability of the trees makes my work sooo much harder..

I was actually starting to consider making an open source mutable version of Roslyn :P

They should have the best of both worlds, one class of immutable tree for the scenarios they mentioned and a mutable one for when you actually want to get stuff done quickly.

On Wednesday, January 30, 2013 1:28:55 PM UTC+2, Shay Friedman wrote:

Oren Eini (Ayende Rahien)

Jan 31, 2013, 10:42:53 AM1/31/13
to altnetisrael
Mutating AST is usually done as a visitor, and that easily support immutable stuff ,because you can only mutate current node (by creating a replacement).
Would that work for you?

You received this message because you are subscribed to the Google Groups "altnetisrael" group.
To unsubscribe from this group and stop receiving emails from it, send an email to altnetisrael...@googlegroups.com.
To post to this group, send email to altnet...@googlegroups.com.
Visit this group at http://groups.google.com/group/altnetisrael?hl=en.
For more options, visit https://groups.google.com/groups/opt_out.

Reply all
Reply to author
0 new messages