Mutability, Linked Lists, and Enumerators (C# vs. Objective-C 2.0)

by Jon 8/30/2008 10:22:00 PM

A few job interviews ago, I was asked about the meaning and/or significance of the term "immutable". I knew that immutable means doesn't and cannot change, and that value types (numbers and strings) and structs in C# are immutable. I knew that structs being value types get stored on the stack, and, if small, tend to be more performant. But I didn't have the background to put two and two together, so I wasn't able to explain why it mattered.

I'm still trying to wrap my hands around how stack optimizations really work, actually. But meanwhile, another job interview asked me what I knew about data structures and, "if .NET had no arrays, how would you manage multiple objects"? I was shocked with myself. These were rediculously simple questions, going back to the basics of computer science. But, for that matter, these were among those rare questions that favor the value of college education, which I lack, over experience. But it takes neither college education nor much experience to suggest a "delimited list in a string" which I didn't even think about for some weird reason--although, technically, any such serialization requires an array of char or byte.

I could be wrong but I *think* the correct answer to "in the absence of .NET arrays, how could you manage a set of n objects" would be to create a linked list. Having been abstracted from how lists work for so long, I never even looked this simple thing up until now.

http://en.wikipedia.org/wiki/Linked_list

http://en.wikipedia.org/wiki/Stack_(data_structure)

Glad that's over. I feel so dumb (and no, darn it, I did NOT ride the short bus to school!!), but refreshing myself on that is finally behind me.

Anyway, back to the mutable vs. immutable discussion, the Cocoa API in the Mac OS X world makes a strong emphasis on isolating certain objects, most notably collection objects (NSSet, NSArray, NSDictionary) as either mutable (NSMutableSet, NSMutableArray, NSMutableDictionary) or immutable. They're completely different classes, that perform the same function but with different optimizations, so the immutable (unchanging) version, which is the default (lacking the "Mutable" in the name), is extremely performant.

C#'s APIs and compiler abstract this stuff so significantly that most C# developers (like me) would likely find all this really odd.

Here's the blog post that motivated me to post this. http://cocoadevcentral.com/articles/000065.php 

Incidentally, I noticed something else about Cocoa. In C#, the syntactically-ideal iteration loop for a collection looks like this:

foreach (string s in myArray) {
    Console.WriteLine(s);
}

The problem is that foreach typically has a performance hit versus using

for (int i=0; i<myArray.Length; i++) {
    string s = myArray[i];
    Console.WriteLine(s);
}

.. which happens to be fuglier. And, to be honest, it shouldn't be faster when working with lists because an indexer has to iterate through the list from the top to find the i node, whereas an enumerator on a linked list should require no reiteration. So I don't get it.

Meanwhile, though, C# 2.0 introduced List<string>, which offers the delegate-driven ForEach( delegate ( string s) { .. } );, which is far more performant.

C# 2.0:
myList.ForEach ( delegate (string s) {
    Console.WriteLine(s);
} );

C# 3.0:
myList.ForEach( s => Console.WriteLine(s); );

In C#, AFAIK, foreach internally just instantiates an IEnumerable object's IEnumerator object and then calls its MoveNext() method, but then so does List<T>.ForEach, so I don't know (haven't looked at the MSIL or Reflector) what it's doing different between the two.

But Objective-C 2.0 apparently went through a wholly different process of evolution, where its foreach equivalent is actually much faster than its enumerator MoveNext() equivalent.

http://developer.apple.com/documentation/Cocoa/Conceptual/ObjectiveC/Articles/chapter_8_section_1.html
http://cocoawithlove.com/2008/05/fast-enumeration-clarifications.html

Interesting stuff.

http://theocacao.com/document.page/510

Currently rated 3.3 by 3 people

  • Currently 3.333333/5 Stars.
  • 1
  • 2
  • 3
  • 4
  • 5

Tags: , , , , , ,

Software Development | Mac OS X

Related posts

Comments

8/31/2008 2:15:49 AM

Christian Vest Hansen

> an indexer has to iterate through the list from the top to find the i node,
> whereas an enumerator on a linked list should require no reiteration.
> So I don't get it.

You're mixing things here. Arrays are not implemented with linked lists. Instead, arrays are implemented as a continues slab of memory, and the size of the elements are always defined. This means that to get to the i'th element of an array, the CPU will calculate the memory address of that element as offset + elm_size * i, where offset is the memory address of the array object itself. This means that looking up an element in an array is an O(1) operation.
A similar lookup in a linked list really would require iterating the list to the i'th position, making it an O(n) operation.

Christian Vest Hansen

8/31/2008 2:27:47 AM

Christian Vest Hansen

Also, the most significant advantage of immutability, as I see it, is carefree concurrent usage. When things cannot change, then there's no risc in letting multiple threads access the same shared data structure. You don't have to think about memory fences, interleavings, locks or any of that crud, which can be a huge relief when writing programs that use a lot of threads.

Christian Vest Hansen

8/31/2008 3:34:41 AM

Jon

Got it. Immutability == no locks (no need for them), == faster multi-threaded performance.

Thanks Christian. Wink

Jon us

9/1/2008 3:38:45 AM

Jon Davis

>>
> an indexer has to iterate through the list from the top to find the i node,
> whereas an enumerator on a linked list should require no reiteration.
> So I don't get it.

You're mixing things here. Arrays are not implemented with linked lists.

<<

Actually, Christian, in that context I was thinking in terms of lists, not arrays.

Jon Davis us

Add comment


(Will show your Gravatar icon)  

  Country flag





Live preview

11/21/2008 6:39:43 AM


 

Powered by BlogEngine.NET 1.2.0.0
Theme by Mads Kristensen

About the author

Jon Davis Jon Davis (aka "stimpy77") has been a programmer, developer, and consultant for web and Windows software solutions professionally since 1997, with experience ranging from OS and hardware support to DHTML programming to IIS/ASP web apps to Java network programming to Visual Basic applications to C# desktop apps.
 
Software in all forms is also his sole hobby, whether playing PC games or tinkering with programming them. "I was playing Defender on the Commodore 64," he reminisces, "when I decided at the age of 12 or so that I want to be a computer programmer when I grow up."
 
Jon is currently in a temp-to-perm contract with a media corporation that primarily produces B2B magazines. The insanely complete and powerful Content Management System that they are switching to is SiteCore CMS, which is arguably the richest and most complete ASP.NET 3.5 based CMS on the planet.
E-mail me Send mail

Most Recent of Many Library Investments

Calendar

<<  November 2008  >>
MoTuWeThFrSaSu
272829303112
346789
10111213141516
17181920212223
24252627282930
1234567

View posts in large calendar

Pages

    Recent comments

    Authors

    Tags

    Disclaimer

    The opinions expressed herein are my own personal opinions and do not represent my employer's view in anyway.

    © Copyright 2008

    Sign in