Ayende @ Rahien

Unnatural acts on source code

Dropping Data Structures into the Pit of Success

As part of the work that I am doing for Rhino ServiceBus, I am also creating a few data structures.

image No, I haven't gone mad and decided that it is a good idea to write my own linked list, but I have discovered that there are several patterns that I am using that require some potentially tricky coding, so I decided to gather them into a single place so I wouldn't have to do this again.

It is important to note that those are not actually data structures that I am using inside Rhino ServiceBus, rather, those are data structures that I expect users of Rhino ServiceBus to use.

Currently I have LRU Set and thread safe dictionary. Both of which, mind you, are following a completely different approach that the usual approach that you would see in standard collections.

Here is an example of how this can be used:

dic.Write((add,remove) =>
{
    add("a", 5);
    remove("b");
    add("c", 6);
});

It also try to provide an interface that is as small as possible, because I don't want to give flexibility here. There are very specific use cases for those data structures.

Comments

Frans Bouma
12/18/2008 08:35 AM by
Frans Bouma

Actually, the linked list in .NET is pretty bad, as it doesn't have an O(1) concat operation between two linked lists, because every element in the linked list has a reference to the linked list master object.

LeastRecentlyUsedSet sounds a bit like a reverse WeakHashmap in java?

Jason Whitehorn
12/18/2008 01:10 PM by
Jason Whitehorn

Internally are you just wrapping the .NET dictionary in thread safe locking, or are you actually implementing your own list?

configurator
12/18/2008 02:40 PM by
configurator

How does GetEnumerator work for threading scenarios? Does it provide a snapshot of the dictionary?

Ayende Rahien
12/18/2008 02:56 PM by
Ayende Rahien

No, it is just a bucket that can contain up to X items.

This allow you to maintain a fixed list without worrying it about grow too big

Ayende Rahien
12/18/2008 02:58 PM by
Ayende Rahien

Configuratior,

Yes, I return a snapshot

Ayende Rahien
12/18/2008 02:59 PM by
Ayende Rahien

Jason,

Wrapping, yes. No interest in trying to implement them myself

Haycheskay
12/18/2008 07:01 PM by
Haycheskay

This just about sums up software development: roll your own <whatever.

The state of the .NET framework, available .NET commercial libraries, and Open Source librariesmust be crap for you to do this.

Maybe you can add your stuff to http://www.codeplex.com/PowerCollections?> But then we also have another collection library that ships with NHibernate?

The Madness of Software Development.

Avish
12/19/2008 07:02 PM by
Avish

I'm not happy about the lambda syntax. While expressions and lambdas allow for richer APIs, they also make them less intuitive and less discoverable. This was also my experience when I first tried to use RhinoMocks 3.5.

Specifically for the above case, perhaps consider:

dic.Write(writer => {

writer.Add("a", 5);

writer.Remove("b");

writer.Add("c", 6);

}

or even using a child object rather than a lambda to record updates (a-la dict.GetWriter()).

This would be slightly better in terms of discoverability, I think.

Mike Brown
12/19/2008 07:58 PM by
Mike Brown

Avish, you're still using a lambda, I think what you're saying is that you're not comfortable with delegates as objects. It did make me do a double take for a second, but it makes sense to me.

But I do see how having a separate object and calling functions on that object might be a bit more intuitive.

Ayende Rahien
12/20/2008 07:15 AM by
Ayende Rahien

Avish,

That particular syntax actually exposes a bug in the C# compiler, so I am removing that.

Jason Jarrett
12/22/2008 02:53 AM by
Jason Jarrett

Would you mind sharing the threadsafe dictionary?

I'm trying to port (as much as I can) the Castle.DynamicProxy2 to silverlight.

I wanted to replace the Hashtables used with a Dictionary <key,value> but there is a case where it's using a Syncronized hastable...

Any thoughts?

Comments have been closed on this topic.