<?xml version="1.0" encoding="utf-8" ?>

<rdf:RDF 
   xmlns:rdf="http://www.w3.org/1999/02/22-rdf-syntax-ns#"
   xmlns:admin="http://webns.net/mvcb/"
   xmlns:content="http://purl.org/rss/1.0/modules/content/"
   xmlns:dc="http://purl.org/dc/elements/1.1/"
   xmlns:slash="http://purl.org/rss/1.0/modules/slash/"
   xmlns:wfw="http://wellformedweb.org/CommentAPI/"
   xmlns="http://my.netscape.com/rdf/simple/0.9/">
<channel>
    <title>Matt's Amazing Developer Blog 3000</title>
    <link>http://www.devblog3000.com/</link>
    <description>Observations, commentary and rants on Software Development</description>
    <dc:language>en</dc:language>

    <image rdf:resource="http://www.devblog3000.com/templates/default/img/s9y_banner_small.png" />

    <items>
      <rdf:Seq>
        <rdf:li resource="http://www.devblog3000.com/archives/3-guid.html" />
        <rdf:li resource="http://www.devblog3000.com/archives/2-guid.html" />
        <rdf:li resource="http://www.devblog3000.com/archives/1-guid.html" />
      </rdf:Seq>
    </items>
</channel>

<image rdf:about="http://www.devblog3000.com/templates/default/img/s9y_banner_small.png">
        <url>http://www.devblog3000.com/templates/default/img/s9y_banner_small.png</url>
        <title>RSS: Matt's Amazing Developer Blog 3000 - Observations, commentary and rants on Software Development</title>
        <link>http://www.devblog3000.com/</link>
        <width>100</width>
        <height>21</height>
    </image>


<item rdf:about="http://www.devblog3000.com/archives/3-guid.html">
    <title>Cache Coherent Data Structures in C++: A Real Page-Turner</title>
    <link>http://www.devblog3000.com/archives/3-Cache-Coherent-Data-Structures-in-C++-A-Real-Page-Turner.html</link>
    <description>
    &lt;p&gt;Once upon a time, cache coherency was a problem that, for the most part, was faced by game programmers and systems programmers.  The basic problem is this: given an application that requires a large amount of memory, an operating system that supports virtual memory, and a very small amount of physical memory, the application can experience &amp;quot;death by swapping&amp;quot;.  If the data that the application needs to operate on at any given time is scattered all over the app&#039;s memory space, rather than stored contiguously in memory, and physical memory can&#039;t store all the pages that the data is found on at the same time, then the operating system will constantly be swapping memory in and out of physical memory, causing major slowdown due to disk access.  If you happen to be &lt;em&gt;iterating&lt;/em&gt; on data that&#039;s in this situation--well, you&#039;re sunk.&lt;/p&gt;&lt;p&gt;Now days, with the growing presence of multi-core processors, the same problem is cropping up for everyone, albeit in a different context and on a different scale.  In a multi-core environment, access to main memory becomes a bottleneck.  When assigning work to different cores, you want to have all of the data a particular core is working on in the processor&#039;s on-chip caches, so the core isn&#039;t constantly waiting for memory to be moved between cache and main memory.  Given the size of caches on today&#039;s processors, it&#039;s not always possible to have &lt;em&gt;all&lt;/em&gt; your data in the cache at one time--but you can still minimize the amount of time spent moving memory.  Again, if your data is scattered all over the place, the core that&#039;s operating on it is likely to spend most of its time waiting for data to be fetched from main memory--kinda defeating the purpose of multiple cores.&lt;/p&gt;&lt;p&gt;Luckily, a good general solution to both of these problems is to use cache-coherent data structures, and it&#039;s generally pretty easy to do so.  Cache-coherent data structures are data structures that cause your data to be stored contiguously in memory--meaning that instead of scattering your data over thousands of virtual pages, you can ensure that it gets written to just a few.  This also optimizes memory prefetch on most modern processors, which fetch blocks of main memory into cache at a time rather than just the bytes you ask for.&lt;/p&gt;&lt;p&gt;With that in mind, here are some easy ways to use cache-coherent data structures to keep your app out of disk and processor cache-miss hell:&lt;/p&gt;&lt;p /&gt;&lt;h5&gt;Prefer Array-Based Data Structures Over Pointer-Linked Data Structures&lt;/h5&gt;&lt;p&gt;Yep, turns out everything they said was good for you in your college Data Structures class is bad for you.  When storing like data elements in a collection, the type of pointer-linked data structures in which you grow the collection by allocating one data element at a time and linking it to the rest of the collection are bad news.  Because you&#039;re allocating a large amount of small bits of memory over time, you&#039;re likely to spread your data over many, many pages.&lt;/p&gt;&lt;p&gt;Instead, it&#039;s best to implement your data structures using arrays as the underlying storage mechanism.  As you probably remember from that same Data Structures class, just about any data structure that can be implemented using pointers can be implemented using arrays--you just link between array indices rather than memory locations.  You can grow the array as necessary using realloc() (or your operating system&#039;s equivalent).  This way, all of your data is stored contiguously in memory and will span the minimum number of pages possible.&lt;/p&gt;&lt;p&gt;The downside to this is that implementing complex data structures on top of arrays is a big pain.  Ever try to implement a red-black tree on top of an array?  Not fun.  And of course you have to manage &amp;quot;holes&amp;quot; in your array caused by deleted elements--either by shifting array elements around and fixing up links, or by reusing array elements.  But for simple data structures, it&#039;s not such a bad thing--replacing that pointer-based linked list, stack or queue with an array-based implementation will give you a big cache win for not too much effort.  For more complex data structures, the STL can help you out a bit, as we&#039;ll see below.&lt;/p&gt;&lt;h5&gt;Let STL Vectors Help You Out&lt;/h5&gt;&lt;p&gt;STL Vectors are probably one of the most underrated classes in the Standard Library.  They&#039;re amazingly flexible, and especially handy for our purposes, since in the vast majority of STL implementations, they use an array as their underlying storage mechanism.&lt;/p&gt;&lt;p&gt;The standard library specification says that vectors have the following properties: O(1) insertion and deletion at the end of the vector, O(n) insertion and deletion anywhere else in the vector, O(1) element access, and of course O(n) search (unless you apply some tricks, as mentioned below).  The only exception to this is when the vector has to expand and reallocates the array; depending on your STL implementation and your operating system&#039;s realloc implementation, that operation may take O(n) when it happens.  If you&#039;re good about presizing the vector and managing its capacity, you can avoid this cost a lot of the time.&lt;/p&gt;&lt;p&gt;With that in mind, let&#039;s take a look at some advanced functionality you can implement with a vector, while still maintaining cache coherency:&lt;/p&gt;&lt;ul&gt;&lt;li&gt;&lt;strong&gt;Stack:&lt;/strong&gt; This comes for free--just use those O(1) push_back and pop_back methods.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Ordered List:&lt;/strong&gt; To insert an element, use push_back and then the STL sort algorithm.  This isn&#039;t a great example, though, since insert will cost O(n log n), delete will cost O(n), and search will cost O(n).  So how about...&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Binary Search Table:&lt;/strong&gt; When inserting, use push_back and then sort, as above, for O(n log n).  When searching, use the STL lower_bound algorithm for O(log n).  When deleting, find your element with lower_bound and then use the vector&#039;s usual erase, for O(n).  Now you have a search time comparable to a binary search tree, with the trade-off of increased insert time.&lt;/li&gt;&lt;li&gt;&lt;strong&gt;Keyed Binary Search Table:&lt;/strong&gt; Missing STL maps?  Try the Binary Search Table above, but store STL pairs populated with your key and value.&lt;/li&gt;&lt;/ul&gt;&lt;p&gt;Even more interesting possibilities open up in certain situations when you start to look at the STL heap algorithms, but, for the sake of brevity, I&#039;ll leave that as an exercise for the reader.&lt;/p&gt;&lt;p /&gt;&lt;h5&gt;Use Memory Pools&lt;/h5&gt;&lt;p&gt;Memory pools are a great way to ensure cache coherency in situations where you need to allocate a lot of small objects.  The basic idea of a memory pool is that you allocate a large number of fixed-sized objects (probably using an array or vector).  Then, as you need those objects, you request them from the memory pool, which gives you a pointer to an unused object in its object store.  When you&#039;re done with the object, you notify the memory pool and it &amp;quot;frees&amp;quot; that object.  Using this technique, you can use new/delete semantics &lt;em&gt;and&lt;/em&gt; maintain cache coherency.  It&#039;s almost like having your cake and eating it too!&lt;/p&gt;&lt;p&gt;There are a few requirements, of course.  Nothing&#039;s free--that&#039;s why this is only &lt;em&gt;like&lt;/em&gt; having your cake and eating it too.  First of all, the objects in your pool need the ability to return to a freshly initialized state.  Since they will most likely be reused, but you can&#039;t re-construct them, you&#039;ll have to write code to reset them to pristine condition.  In addition to this, you will need to keep track of which objects in your memory pool are in use and which aren&#039;t; this can be achieved in a number of ways, such as maintaining a free list or a bitfield indicating the state of each object in the pool.  You&#039;ll also have to decide whether your memory pool can grow or not; if it can, that implies some additional coding.&lt;/p&gt;&lt;p&gt;The good news is that it&#039;s relatively easy to implement a memory pool as a template class, and instantiate it on any class you want to pool that follows the guidelines above.  An actual implementation of that is beyond the scope of this article--but might make a good blog entry later on (or perhaps readers can point me to an existing, free implementation).&lt;/p&gt;&lt;h5&gt;What If I Really, Really, Really Want to Use STL?&lt;/h5&gt;&lt;p&gt;Simple: use vector!  Seriously, if you have a burning need to use the pointer-linked STL classes and want to ensure some measure of cache coherency, it can be done.  My suggestion is to employ memory pooling.  As mentioned above, memory pools preserve some of the new/delete semantic; therefore, it should be possible to write a custom STL allocator that uses a memory pool as storage.&lt;/p&gt;&lt;h5&gt;Conclusion&lt;/h5&gt;&lt;p&gt;There you have it--some ways to ease the pain of working on a system with low physical memory, or on a multicore system.  Of course, there are many other techniques for cache coherency, and I encourage all of you to comment with any that you know about/recommend.  &lt;/p&gt;&lt;p&gt;Before I send you forth into the world with your new-found knowledge, some advice: remember that there are situations where cache coherency is critical, and some where it&#039;s not.  If you&#039;ve got plenty of physical memory and/or aren&#039;t worried about your processor cache, using these techniques is overkill.  Linked lists, sets, maps, and pointers will do just fine.  Custom data structures are always more difficult to maintain then pre-existing data structures like those provided by STL.  So make your choices wisely.&lt;/p&gt; 
    </description>

    <dc:publisher>Matt's Amazing Developer Blog 3000</dc:publisher>
    <dc:creator>nospam@example.com (Matt Kimmel)</dc:creator>
    <dc:subject>
    Coding Tips, Game Development, </dc:subject>
    <dc:date>2006-09-29T02:27:00Z</dc:date>
    <wfw:comment>http://www.devblog3000.com/wfwcomment.php?cid=3</wfw:comment>
        <slash:comments>9</slash:comments>
        <wfw:commentRss>http://www.devblog3000.com/rss.php?version=1.0&amp;type=comments&amp;cid=3</wfw:commentRss>
    
    
</item>
<item rdf:about="http://www.devblog3000.com/archives/2-guid.html">
    <title>Practicing Programming</title>
    <link>http://www.devblog3000.com/archives/2-Practicing-Programming.html</link>
    <description>
    &lt;br /&gt;
&lt;p&gt;Lately, I&#039;ve been thinking a lot about practicing programming.  By that I don&#039;t mean the practices of programming--I mean actually programming for the purpose of honing my skills.  I used to think that I didn&#039;t need to &amp;quot;practice&amp;quot;; after all, I program all the time at my job, right?  I even do extra-curricular projects.  Isn&#039;t that &amp;quot;practice&amp;quot; in and of itself?&lt;/p&gt;&lt;p&gt;But recently I&#039;ve realized that that&#039;s not true.  When you work on a project, whether it&#039;s at work or at home, you&#039;re implicitly working within a very narrow scope.  When you work within a particular industry, you&#039;re also working within a narrow scope.  While you may gain a great deal of skill within that scope, you still need to maintain your skills with the &amp;quot;basics&amp;quot;: the languages, techniques, algorithms, and so on that make you a well-rounded programmer.  If you concentrate all of your efforts into one area, your skills in other areas will atrophy.&lt;/p&gt;&lt;p&gt;Practicing programming doesn&#039;t just help you maintain your existing skills, either.  It helps you to pick up and understand new skills that may be applicable to your current area of focus.   For example, when I first was learning Test-Driven Development, I didn&#039;t want to plunge into a huge project without ever having written an xUnit test; that seemed pretty overwhelming.  Instead, I found it very helpful to write a bunch of small practice programs and solve some exercises using TDD.  By the time I started to apply TDD to a large work-related project, I felt pretty confident.&lt;/p&gt;&lt;p&gt;That last bit about writing small programs ties into something that I think is essential to getting the most out of programming practice: &lt;strong&gt;making it practical&lt;/strong&gt;.  If practicing programming means you have to embark on a full-scale project in order to exercise one or two concepts, and you&#039;re lazy like me, you&#039;re not gonna get it done.  Even if you do get it done, you&#039;re going to spend more time worrying about the logistics of the project than boning up on a particular technique.  Programming practice should be focused and easy to do.  When I&#039;m practicing, I like to work on problems of a scale that I can solve in a few hours--bite-sized programs that I can write in an evening, or on my laptop while I&#039;m on a train ride or plane flight.&lt;/p&gt;&lt;p&gt;So where do you find these kinds of problems?  I suppose you could make them up.  I find, though, that the best practice comes from solving problems that come out of left field, not things that you&#039;ve already thought about.  I like to find exercises that somebody else, with a completely different perspective than my own, has come up with.  That maximizes the extent to which you think outside your own areas of focus, and leads you to a broader experience.&lt;/p&gt;&lt;p&gt;With that in mind, where can you find exercises to practice with?  I&#039;ve been doing quite a bit of research on this, and have found a lot of sources.  Here are a few big ones:&lt;/p&gt;&lt;p&gt;&lt;strong&gt;Code Katas.&lt;/strong&gt;  Those of you who read a lot of development blogs have probably encountered these; they&#039;ve become very popular in the last few years.  A code kata is a small problem or exercise that you are encouraged to code from scratch periodically.  The idea is that, similarly to a martial arts kata, every time you go through the motions of solving that problem, you&#039;ll gain new insights into it.  I think that&#039;s true, but I also think that there&#039;s value in doing code katas even if you only solve them once.  They provide small but interesting problems to solve.  I first encountered code katas when I went to an Agile Development seminar given by Bob Martin, in which he walked us through a couple of katas.  Indeed, &lt;a target=&quot;_blank&quot; href=&quot;http://butunclebob.com/&quot;&gt;Bob Martin&#039;s blog&lt;/a&gt; contains a number of interesting code katas.  Dave Thomas also has &lt;a target=&quot;_blank&quot; href=&quot;http://blogs.pragprog.com/cgi-bin/pragdave.cgi/Practices/Kata&quot;&gt;a great page of code katas&lt;/a&gt; at the Pragmatic Programmers site.  &lt;a target=&quot;_blank&quot; href=&quot;http://bossavit.com/cgi-bin/dojo.pl?CodingDojo&quot;&gt;The Coding Dojo&lt;/a&gt; also offers a number of interesting katas.  Many others exist on the net, including a lot of language-specific ones (all the katas I&#039;ve linked to here are language-neutral).  Your best bet is to Google &amp;quot;code kata&amp;quot;.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Coding Competitions.&lt;/b&gt;  There are a number of websites, such as &lt;a target=&quot;_blank&quot; href=&quot;http://www.topcoder.com/&quot;&gt;TopCoder&lt;/a&gt;, that host programming competitions.  These are typically timed matches during which programmers compete to solve a small problem in the most efficient/elegant way possible.  These sites provide you with an &amp;quot;arena&amp;quot; environment that includes a code editor, compiler, test data, and typically a way to view other contestants&#039; entries and critique them.  Some competitions even include cash prizes.  Even if you&#039;re not the competetive type, most of these sites have &amp;quot;practice arenas&amp;quot; in which you can solve problems from previous competitions.  Not a bad way to get in some quick programming practice; you don&#039;t even need a compiler on your local machine.&lt;/p&gt;&lt;p&gt;&lt;b&gt;Books.&lt;/b&gt;  Remember those exercises at the end of every chapter of your Computer Science textbooks from college?  Well, they&#039;re still there, and a lot of them pose interesting problems.  Dig out your old Algorithms book, or an Operating Systems text, or whatever seems interesting to you, and check them out!  Trust me, they&#039;re much more fun when you&#039;re not doing them for credit.  There are also some books of coding puzzles out there, but they seem to be few and far between.  One I did pick up recently is &lt;a href=&quot;http://www.amazon.com/gp/redirect.html?ie=UTF8&amp;location=http%3A%2F%2Fwww.amazon.com%2Fgp%2Fproduct%2F0387001638%2Fref%3Dpd%5Frvi%5Fgw%5F1%3Fie%3DUTF8&amp;tag=mattsamazinde-20&amp;linkCode=ur2&amp;camp=1789&amp;creative=9325&quot;&gt;Programming Challenges&lt;/a&gt;&lt;img width=&quot;1&quot; height=&quot;1&quot; border=&quot;0&quot; style=&quot;border: medium none  ! important; margin: 0px ! important;&quot; src=&quot;http://www.assoc-amazon.com/e/ir?t=mattsamazinde-20&amp;l=ur2&amp;o=1&quot; /&gt; by Steve Skiena and Miguel Revilla, a book that purports to prepare you for the aforementioned coding competitions.  It includes a lot of practice problems and access to a website that will &amp;quot;auto-judge&amp;quot; your answers.  Definitely worth a look.&lt;/p&gt;&lt;p /&gt;&lt;p&gt;Any thoughts on all this?  Got any other resources?  Please post them in comments to this entry!&lt;/p&gt; 
    </description>

    <dc:publisher>Matt's Amazing Developer Blog 3000</dc:publisher>
    <dc:creator>nospam@example.com (Matt Kimmel)</dc:creator>
    <dc:subject>
    Practices, </dc:subject>
    <dc:date>2006-09-16T15:47:46Z</dc:date>
    <wfw:comment>http://www.devblog3000.com/wfwcomment.php?cid=2</wfw:comment>
        <slash:comments>7</slash:comments>
        <wfw:commentRss>http://www.devblog3000.com/rss.php?version=1.0&amp;type=comments&amp;cid=2</wfw:commentRss>
    
    
</item>
<item rdf:about="http://www.devblog3000.com/archives/1-guid.html">
    <title>Hello World!</title>
    <link>http://www.devblog3000.com/archives/1-Hello-World!.html</link>
    <description>
    &lt;p&gt;Welcome to my development blog! My name is Matt, and I’ll be your enthusiastic, embattled, opinionated software developer during your stay. You can read more about me on the About page, but to summarize quickly, I’ve been working in the computer industry for about 15 years, primarily as a programmer or software engineer or whatever you want to call it, and also at times as a sysadmin, manager, author, and whatever else they’d let me do. I’ve spent the last nine years working in the video game industry, and while this isn’t specifically a game development blog, I’ll certainly be talking about it from time to time.&lt;/p&gt;&lt;br /&gt;
&lt;p&gt;In this blog, I’ll be posting my thoughts and opinions on all aspects of software development, computer science, and occasionally things unrelated. With any luck, some of these thoughts and opinions will be useful to other people. Please comment on my posts if you agree with me. Please comment on my posts if you disagree with me. Please comment on my posts if you think I’m a raving lunatic. Please do not comment on my posts if you’d like to offer your body-enhancing pills to my readers.&lt;/p&gt;&lt;br /&gt;
&lt;p&gt;Not too many rules here.  Let’s lay off the personal attacks.  I reserve the right to make fun of flame wars if they get silly.&lt;/p&gt;&lt;br /&gt;
&lt;p&gt;Above all, enjoy!&lt;br /&gt;
&lt;/p&gt;&lt;br /&gt;
 
    </description>

    <dc:publisher>Matt's Amazing Developer Blog 3000</dc:publisher>
    <dc:creator>nospam@example.com (Matt Kimmel)</dc:creator>
    <dc:subject>
    Administrivia, </dc:subject>
    <dc:date>2006-09-15T22:55:49Z</dc:date>
    <wfw:comment>http://www.devblog3000.com/wfwcomment.php?cid=1</wfw:comment>
        <slash:comments>0</slash:comments>
        <wfw:commentRss>http://www.devblog3000.com/rss.php?version=1.0&amp;type=comments&amp;cid=1</wfw:commentRss>
    
    
</item>

</rdf:RDF>
