Optimizing Read Transaction Startup Time: Racy Data Structures
Concurrency, multi-threaded behavior, the right level of abstraction, and more all help get your reads going fast. See how one team implements them.
Join the DZone community and get the full member experience.Join For Free
finding the appropriate image for this post was hard. you try searching for “racy pictures” in google image search, but you might not want to do it from work.
anyway, today at lunch we had a discussion about abstractions and at what level you should be working. the talk centered on the difference between working in low-level c and working with a high-level framework like c# and the relative productivity associated with it.
at one point the following argument was raised: “well, consider the fact that you never need to implement list, for example.” to which my reaction was: “i did just that last week.”
now, to forestall the nitpickers, pretty much any c developer will have an existing library of common data structures already in place, i know. and no, you shouldn’t be implementing basic data structures unless you have a really good reason.
in my case, i think i did. the issue is very simple. i need to have a collection of items that are safe for multi-threaded reads, but they are mostly only ever accessed from a single thread, and are only ever modified by a single thread. oh, and they are also extremely performance sensitive.
the reason we started looking into replacing them is that the concurrent data structures that we were using (concurrentdictionary and concurrentstack, in those cases) were too expensive. and a large part of that was because they gave us a lot more than what we actually needed (fully concurrent access).
so, how do we build a simple list that allows for the following:
- only one thread can write.
- multiple threads can read.
- no synchronization on either end.
- stale reads allowed.
the key part here is the fact that we allow stale reads.
here is the concrete scenario, we need to track all active transactions. a transaction is single threaded, but we allow thread hopping (because of async). so we define:
and then we have:
dynamicarray is just a holder for an array of nodes. whenever we need to add an item to the active transactions, we’ll get the local thread value, and do a linear search through the array. if we find a node that has a null transaction value, we’ll use it. otherwise, we’ll add a new node value to the end of the array. if we run out of room in the array, we’ll double the array size. all pretty standard stuff, so far. removing a value from the array is also simple, all you need to do is to null the transaction field on the relevant node.
why all of this?
well, only a single thread can ever register a transaction for a particular dynamicarray instance. that means that we don’t have to worry about concurrency here. however, we do need to worry about transactions that need to remove themselves from the list from other threads. that is why we don’t have any concurrency control here. instead, removing the transaction is done by setting the node’s transaction field to null. since only the owning transaction can do that, this is safe.
other threads, however, need to read this information. they do that by scanning through all the thread values, and then accessing the dynamicarray directly. now, that means that we need to be safe for concurrent reading. this is done by having the array more or less static on most scenarios. after it get full enough, it will never grow again, and the values will remain there, so effectively other threads will be reading an array of nodes. we do need to be careful when we expand the array to give more room. we do this by first creating the new array, copying the values to the new array, and only then setting it in the threaded instance.
this way, concurrent code may either see the old array or the new one, but never need to traverse both. and when traversing, it goes through the nodes and check their transaction value.
remember that the transaction is only being set from the original thread, but can be reset from another thread if the transaction moved between threads. we don’t really care, the way it works, we read the node’s transaction field, and then check its value (once we have a stable reference). the idea is that we don’t worry about data races. the worst that can happen is that we’ll see an older view of the data, which is perfectly fine for our purposes.
this is pretty complex, but the code itself is simple enough, and the performance benefit justifies it several times over.
Published at DZone with permission of Oren Eini, DZone MVB. See the original article here.
Opinions expressed by DZone contributors are their own.