A simple C# / LINQ trick shown with Console.ReadLine()

I haven’t been working on Paint.NET v4.0 at all lately, but I have been reading, learning, and prototyping about a whole sleuth of things related to functional and asynchronous programming. One such topic is that of monads, which I have a blog entry almost completed on. However, I wanted to get a quick blog post to show something simple yet powerful. LINQ brings a lot of power to IEnumerable<T>, and in this case we’re going to map the console input into an IEnumerable<T>. Then we’ll use standard LINQ methods to only display those lines of text that have ".dll" in them. It’s a contrived example, but it’s easy enough to find more useful applications of this. I have to go in a minute, so I’ll throw this code up on the blog and let the conversation be driven from the comments.

public static class Extensions
{
    public static IEnumerable<T> ToList<T>(this Func<T> sampleFn)
    {
        while (true)
        {
            T val = sampleFn();
            yield val;
        }
    }

    public static IEnumerable<T> While<T>(this IEnumerable<T> list, Func<T, bool> whileFn)
    {
        foreach (T item in list)
        {
            if (whileFn(item))
                yield return item;
            else
                yield break;
        }
    }

    public static void Execute<T>(this IEnumerable<T> list)
    {
        foreach (T item in list) { }
    }

    public static IEnumerable<T> Do<T>(this IEnumerable<T> list, Action<T> action)
    {
        foreach (T item in list)
        {
            action(item);
            yield item;
        }
    }
}

class Program
{
    public static void Main()
    {
        Func<string> inputSampleFn = Console.ReadLine;

        var input = inputSampleFn.ToList()
                                 .While(s => s != null);

        var query = input.Select(s => s.ToLower())
                         .Where(s => s.Contains(".dll"));

        query.Do(s => Console.WriteLine(s))
             .Execute();
    }
}

Now, compile this to something like "dllFilter.exe". Then run, at the command line, "dir c:\windows | filter.exe". Neat 🙂

Paint.NET just can’t satisfy an 8-core Opteron

Just to make sure, I went and downloaded the trial version of Windows Server 2008 from the Microsoft website. I installed it on this 8-core Opteron workstation with 8 GB of RAM that AMD sent me awhile ago for doing multithreading and performance scaling stuff. I installed version 3.22, then updated it to 3.30 Beta 2 to make sure that would work, and sure enough it does!


(click for full size, 1600×1200)

I’m not surprised, but I felt the need to verify this anyway 🙂 I’ll make sure the website lists Windows Server 2008 as a supported OS on the next update.

As an interesting side note, having access to systems with 4 and 8 cores has brought up an interesting performance problem in Paint.NET, and key insights for optimization and design going forward. In the past I have done a lot of work to ensure that certain key rendering code is multithreaded, and as such going from dual- to quad- to octa-core has yielded healthy performance gains. Even on these systems, quite often, I just can’t get CPU usage all the way up to 100%! Just look at the screenshot above: even on a very compute-intensive effect such as Julia Fractal, at highest quality, on a 16,000 x 9,000 pixel canvas (yes that’s 144 megapixels), the CPU usage is bouncing around and clearly not anywhere close to full performance. I guess the silver lining here is that power consumption is a little lower as a result, eh?

Even on my personal quad core desktop, I’m lucky if rendering a large gradient can hit 65% CPU usage. There’s a lot of wasted performance by having the rendering pipeline set up the way it us. Hopefully my talks with the .NET Parallels Extensions team will help me to improve this situation, while also putting some extra “real world” code in their laps to play with. With the current design, I wouldn’t be surprised if Paint.NET could barely keep a 64-core system more than 15% busy.

Also, I’d like to point out another exciting project related to things like concurrency and parallelism: Microsoft Live Labs Volta. I had a meeting with them last Tuesday and their approach to asynchronous, stream/event-based programming just blew my mind. Their project as a whole is actually targeted for some kind of simplified web cloud tier-distributed development*, but they have a concurrency runtime which could prove extremely valuable for the two rich client/desktop applications that I currently work on**. I’m very excited – anytime somebody sends me a meeting request titled, “Discuss monads and composition in the context of asynchronous computations,” I am reassured that there are still smart people out there working on cool things. I’m not sure how much I can discuss publicly about their stuff, but once I get any kind of green light I’m sure I’ll start gushing. I’ll leave you with one quote from the discussion that I actually made sure to write down, as it embodies something I’ve been struggling and searching for with respect to this type of coding:

“By construction, these things are correct.”

More on what that actually means later. I’ve got a lot of thoughts on parallel/asynchronous, concurrent, and functional programming and how it relates to multithreading, correctness, and performance. Hopefully I can put some of that into writing!

* This is not an exact definition 🙂
** That would be 1) Paint.NET, and 2) the other one that I work on at Microsoft (“my day job”).

Thoughts on Component Isolation for Paint.NET v4.0

I’ve been doing a lot of thinking about how Paint.NET needs to change or evolve before it hits version 4.0, and the big thing that keeps coming up is isolation. Right now, the control flow and error handling in Paint.NET lets one component crash or corrupt the whole program. This could be code sitting in a plugin that causes an unhandled exception in a way that my try/catch harnesses don’t handle, or it could be a COM component loaded by Windows common file Open/Save dialogs that crashes or locks up. Or maybe a piece of code doesn’t throw an exception but it does corrupt some other data, and then that corruption causes a crash later. And then that gets attributed to the wrong component. GDI+ seems to like doing that a lot.

Not only does Paint.NET need to be protected by crashing or "bad" plugins, but it needs to be protected from itself! Things like the Open/Save and Print dialogs are a good example, especially because they deal with external, "native" components. Some people have shell extensions installed that cause the Open/Save dialogs to fail, hang, or crash, and this breaks Paint.NET! This could be something as seemingly benign as a thumbnail provider for DivX videos. Yes, I get crash reports on the forum for that every so once in awhile. Why should Paint.NET have to crash if there’s a corrupted DivX video in a folder you’ve navigated to via File->Open? I’d rather crash just the Open dialog, detect the error without corruption in the main process, and then re-run the dialog in some kind of compatibility or reduced functionality mode. At least that way if it corrupts memory, then that corruption only affects the hosting process that it lives in.

So, there needs to be a really easy way for me to write a component in Paint.NET — whether it’s "built-in" or a "plug-in" — that can be isolated and not cause problems for the rest of the application. This means I want to host it in an external process and communicate with it, and then have a layer of abstraction in place for creating a "remote", isolated object.

The new System.AddIn namespace in .NET 3.5 provides a whole system for this, but honestly I find it to be way too complex, confusing, and involved. They have host views, add-in views, host-side adapter, add-in side adapters, etc. It’s a bunch of terminology that seems invented — why not just say client and server, proxy, tear-off, etc.? Every time you add a new interface or type you have to add code that spans at least 3 assemblies. Plus, they do not provide the ability for me to be running in a 64-bit process and have it communicate with an add-in that requires 32-bit hosting. This is important for certain plug-ins which are doing native interop to DLL’s that are only available in 32-bit (the VTF File Type Plugin comes to mind). They also do not provide the ability to activate an add-in that needs to run at a higher security level — for instance, if I want to require my Settings dialog to run with administrator privilege. I don’t want to have to deal with UAC at the code level, and would prefer it to be abstracted away. They also enforce a specific directory structure, and so on … anyway, it’s just too much and also too little.

.NET Remoting isn’t really an end-to-end solution for what I need, as it does not provide a hosting process. COM provides component hosting in an external process, but it seems like major overkill to register my .NET components as COM-proxied objects. Windows Communication Foundation is more for "enterprise" remoting and communication scenarios.

So it looks like I may have to roll my own solution, using System.IO.Pipes (actually I may prefer an HWND and WM_COPYDATA for marshaling) and borrowing heavily from the lessons put forth by System.AddIn (they have a very good story for versioning), and learned from my inbox full of crash logs. In any case it must be something that is easy to use by both the "host" and the "add-in" (or "built-in" :)). I’m thinking code generation will be very handy here in order to be able to declare an isolatable interface and then auto-generate the local tear-off/proxy, client stub, server stub, and registration glue.

Paint.NET forum is down … again :(

(D’oh — right before I hit "publish" on this post, the forums sprang back up)

I’m not sure what’s going on at Forumer lately, but it seems like the Paint.NET forums (not that the link works right now, mind you) have been up and down chaotically for the last few weeks. In fact, all of forumer.com is non-responsive, so I can’t even leave a troubleshooting ticket on their forum. This has been an ongoing problem lately, unfortunately. There was a time in January (if I remember right) where it was down for probably a full week. When this happens my inbox starts filling up with e-mails telling me about it and asking when things will be fixed. Unfortunately, it’s out of my control so there’s really nothing I can tell people.

I’ve actually been thinking about moving the forums for some time, so that I can host it myself. Problem is, the amount of time involved would be a lot, right? The thing would need a dedicated server and the ability to handle up to 200 users simultaneously (average right now is 30-50, so 200 gives room to grow). I wouldn’t be so worried about disk space (storage is practically free nowadays), but the CPU time needed would probably be high and keep increasing as more and more users joined. Then there’s the logistics of either migrating or deciding not to migrate things like the user database and content. Since Forumer has a policy of only giving you the database if they’re deleting your forum, it’d basically be a complete do-over.

The obvious benefits of self-hosting the forum are the ability to capture that incremental revenue boost by enabling AdSense and Kontera (maybe only for guest users though), and the ability to completely customize all forum elements. On the down side, I’d have full responsibility for the forum’s down time. I don’t know Unix very well so I’d probably run phpBB on a Windows server. Financially it makes sense, as the hosting costs would easily be covered by AdSense and Kontera. It’s the time thing that has kept me from pursuing this in the past. All this sporadic downtime is chipping away at my patience, however, and slowly changing my mind.

Anyway, thoughts on self-hosting the forum? (Please do not give me suggestions for other free forum host sites like Forumer, that’s not what I’m asking for.)

Paint.NET v3.30 Beta 2 is now available

It’s been 2 weeks and I’ve been busy incorporating the feedback I’ve received so far on the first beta. Phew! Anyway, please note that this is a beta that expires in 60 days on May 11th. You can get this update by downloading it from the website, http://www.getpaint.net/ (no need to uninstall your current version, it will figure it all out for you), or via the built-in updater. For the latter, go to Help -> Check for Updates… and make sure you are set up to receive betas by clicking on the Options button and ensuring that "Also check for beta releases" is checked. Then close the dialogs and click on Help -> Check for Updates… again.

Changes since Beta 1:

  • Changed: Most effects and adjustments, including plugins that use IndirectUI, have been visually refreshed. The new look is cleaner and more compact. These changes are shown in more detail at http://blog.getpaint.net/2008/03/10/cleaning-up-the-ui-for-paintnet-v330/
  • Fixed: Thumbnails for PDN images now include transparency, which improves their appearance in Windows Vista.
  • New: For developers, added a Color Wheel control to IndirectUI for use in effect plugins.
  • New: For developers, added ability to customize certain properties of the effect configuration dialog via IndirectUI.

Enjoy!

Cleaning up the UI for Paint.NET v3.30

While I was adding the color wheel control for IndirectUI, I decided that now was as good of a time as any to incorporate some other feedback from some of the more prominent plugin authors. Ed Harvey had suggested that the UI could be made more compact, as adding a bunch of properties often created a dialog that was large or spread out. BoltBait and pyrochild wanted the ability to change the text in the title of the dialog – perhaps to add a “version 2” tag or something similar. The rendering quality of the preview/thumbnail image in some of the dialogs was also a bit of a thorn in my side.

So, I’ve managed to address all of these, and a few others. The end result is a visually refreshed UI that is cleaner and less “noisy”. I think the difference is best seen by showing pyrochild’s Film effect plugin, before and after:

(Unfortunately I had to resize these to 65% of original size, otherwise my WordPress provider was complaining about the image sizes or something…)

At first glance it may not seem like much, but I think overall it results in a much more favorable user experience. The “tick marks” on the sliders are not shown by default (plugin authors may still enable them if they want though), and the dialog ends up being less spread out. The reset button is more subtle, favoring a small graphic instead of text. I may need to push it out another pixel or two so that it doesn’t accidentally get clicked instead of the up/down buttons. Or maybe I’ll move it to the left side of the text boxes. The thumbnail is also smoother, as it is using bicubic instead of bilinear resampling (although you can’t tell with the resized screenshots). For me, all this results in a less “noisy” dialog and it is much nicer to use.

And the great thing about IndirectUI is that all plugins written to use it will automatically get these benefits! 🙂 Hopefully this will help Paint.NET v3.30 further distance itself from being “a cowbell release“.

Nine Inch Nails and the New Online Business Model

As many people may already know, I am a big fan of the band Nine Inch Nails, aka NIN, which is basically Trent Reznor and whomever he hires to work and perform with him at any given time. Earlier in the week, Trent posted that a new album was available, titled “Ghosts I-IV” (volumes one through four). Not even “coming soon,” but available right now. Being a fan, I was naturally psyched about this and went to find out how I could get a copy right now.

Eventually I succumbed and bought the $300 super ultra deluxe edition that comes with four LP’s, two CD’s, one DVD, one Blu-Ray, an art book, ready-to-frame-and-hang art, and Trent’s signature. Plus it’s a limited edition with only 2500 copies. Hey, I need to use those Paint.NET donations for something, right?

The real story here is not that NIN has a new album, but rather in the way that it is being distributed. The music has been released under the Creative Commons Attribution Non-Commercial Share Alike license. Anyone who gets a copy of it has the freedom to make further copies and derivative works (e.g., remixes), as long as they give proper attribution and don’t make any money off of it. You can even download the album for free. They provide direct download links for 1/4th of the tracks, but with that license there’s nothing wrong with distributing or downloading the rest of the album if you find it elsewhere. Trent himself even uploaded those tracks to PirateBay* with a note saying:

“Undoubtedly you’ll be able to find the complete collection on the same torrent network you found this file, but if you’re interested in the release, we encourage you to check it out at ghosts.nin.com, where the complete Ghosts I-IV is available directly from us in a variety of DRM-free digital formats, including FLAC lossless, for only $5.”

Let’s take a step back though, first. The music industry is in a jam because 1) everyone is pirating music (PirateBay, LimeWire, BearShare, etc.), and 2) everyone hates DRM. Oh, and 3) all those lawsuits are making everybody hate them (the RIAA). The majority of people don’t care about paying for most of their music, but those who are willing to pay for it are getting punched in the face with the restrictions that DRM imposes. And even if you buy the CD, only a small percentage of what you pay actually reaches the artist who created the music in the first place. It’s really a lose-lose situation for everyone except the CEO’s of the record companies.

Recently, Trent announced that he is now free and independent of any record label contract, after fulfilling his obligation for a certain number of albums with the “Year Zero Remixed” disc. So, he can now do whatever the heck he wants to, business-wise. He has decided that he will take advantage of the freshly-crowned, most popular method for distributing music: rampant piracy! Except in this case it isn’t pirating because he’s intending people to distribute it this way. He’s getting new fans for free this way, and those fans will find other ways to send him money: concert tickets, t-shirts, and other “non-pirateable” goods. And without a record label as an intermediary, more of that cash goes into his pocket, which is exactly what we want!

It will be interesting to see how well this business model and experiment works out. The real test, however, will be whether this model will work for any other band, especially one that isn’t so widely known.

Also worth noting is that this is basically the business model that Paint.NET has been using for the last few years 🙂 Except that I do not [yet?] have any cool deluxe or limited editions for sale. Hmm, I wonder how much an autographed CD of Paint.NET would go for …

* “Uploaded these tracks to PirateBay” is actually a strong misnomer with the way BitTorrent technology works. But you get the idea.

Ask and you shall receive … Color Wheel for IndirectUI

As part of the version 3.30 release of Paint.NET, I have finally marked all of the code in PaintDotNet.exe as “internal” (instead of public). This code was supposed to be treated as internal implementation details of Paint.NET, but plugin authors were using the code anyway. So, at some point or another I was going to have to do this, and I chose version 3.30 as the ship vehicle.

(See, in Visual Studio 2003, which was the original development environment for Paint.NET, you couldn’t add an EXE as a reference. Then they added this capability in VS2005, but I didn’t catch on until later. So I had always assumed that plugins couldn’t access this stuff and so I didn’t need to mark it all as internal. Oops!)

Well, as it turns out, one of the most popular controls that was being used by plugins was the color wheel. You know, this thing:

This is causing many plugins to break, and I’ve been unmoving in allowing plugins to continue access it. However, KrisVDM on the forums had a reasonable request:

Rick, I don’t know if you followed the discussion on http://paintdotnet.forumer.com/viewtopic.php?f=16&t=22881 , but basically they’re begging and calling me names because I dropped the color wheel from the Drop Shadow UI.

I know I said so before, but I believe many effects and adjustments could benefit from a color wheel (or even two for my duotones) in the effect UI.

Would you please consider adding IndirectUI support for colors?

I told him I would look into it, and tonight I was able to crank out a control for this in about an hour:

It’s in private testing as we speak, and should have no trouble making it into the next beta update for v3.30. It should be a welcome addition for plugin authors.