Research into Uniqueness and Reference Immutability for Safe Parallelism in C#
Some big names from Microsoft, Microsoft Research and the University of Washington have been working on a new variant of C# that introduces the concept of readable and immutable references at the language level. To this effect each reference has one of four permission qualifiers that modify variables and parameters.
Ordinary references in C# are considered to be “writable”. By that they mean anything the reference points to can modified using normal methods and property setters.
The next qualifiers is the “readable”. A readable reference is a read-only view of the object. This doesn’t mean the object cannot be modified, it only means it cannot be modified via this particular reference.
If the object cannot be modified by any reference, then references to it can be marked as “immutable”. “Immutable references may be aliased by read-only or immutable references, but no other kind of reference. All objects reachable from an immutable reference are also immutable.”
The final permission qualifier is “isolated”, which is “an external reference to an externally-unique object cluster.” This is a rather complex idea. Imagine you have a graph of mutable objects that may reference each other in simple or complex ways, but are not referenced directly by anything outside of the graph. The exception is one object that represents the aggregate root. Figure 1 from the research paper may make this clearer:
Much of the research paper focuses on how objects are converted between the writable, isolated, and immutable modes. This concept is incredibly important for building complex, immutable object graphs. In traditional functional programming languages immutable object graphs are built bottom up. Depending on the language, this can make internal cycles difficult if not impossible. There is also a potential for performance penalties, especially if the leaf nodes need to be modified during the construction process.
Under the proposed model, the object graph is first created from mutable objects just like any other OOP-style graph. The root of the graphs is referenced by single variable marked as isolated. This variable can then be used to “recover immutability” by using a function that effectively casts the value from an “isolated T” to an “immutable T”. (Keep in mind that isolated and immutable are really qualifiers on the variable’s type, not the object they reference.)
One of the main reasons for dealing with all this is that it allows for safer parallelism. The compiler can use the permission qualifiers to know whether or not it can safely access a given object without introducing a race condition. And while this cannot prevent dead locks, it can be used to reduce the frequency for which locks are required.
This design also opens some new opportunities for optimizing the compiler and runtime.
For example, the concurrent GC can use weaker read barriers for immutable data. The compiler can perform more code motion and caching, and an MSIL-to-native pass can freeze immutable data into the binary.
Uniqueness and Reference Immutability for Safe Parallelism was written by Colin S. Gordon, Matthew J. Parkinson, Jared Parsons, Aleks Bromﬁeld, Joe Duffy.
John Krewson, Steve Ropa and Matt Badgley Nov 24, 2014