IEnumerable Performance Tip: Any() vs. Count()

I can’t count the number of times (unintentional pun) I’ve checked if an IEnumerable<T> sequence contains elements using Count().

static void Method(IEnumerable<Status> statuses)
{
if (statuses != null && statuses.Count() > 0)
// do something...
}

To get the count, the code has to traverse the entire sequence. On a long, lazy-executed sequences, this can take significant time. Since I only want to know if the sequence contains one or more elements, it’s computationally more efficient to use the Any() extension method.

static void Method(IEnumerable<Status> statuses)
{
if (statuses != null && statuses.Any())
// do something...
}

In this case, Any() will return after examining the first element in the sequence. It also reads a better (IMHO).

 

PDC09 – Day 1

I was going to give a summary of the keynote proceedings but Dan Rigsby has already posted a great summary here.

It’s clear that Microsoft has really scaled back this year’s PDC. There is not much hype (at least for a PDC), the freebies, food and swag are “minimal” and the whole thing feels like a giant corporate yawn. Part of the fun of going to these events is to pickup on the “buzz” of what others are excited about. So far it’s been missing.

That said, I went to some very good breakout sessions yesterday. This is the “other reason” to go to conferences like PDC and in this case PDC has delivered the goods. I want sessions that “hurt my brain” and make me think about how approach programming problems and two of the sessions did exactly that.

First breakout was Future Directions of C# and VB. Luca Bolognese is a disarmingly charming young Italian with a thick accent and a great sense of humor. He wowed everyone at last year’s F# presentation. I attended this session because of the speaker and he delivered the goods. The most interesting bit of news here is a new use for the “yield” keyword. Although experimental, the idea is to yield control of a thread during async operations. It’s purpose is to resolve the ”many threads not enough cores issue” for parallel processing. I’m thinking we’ll use it in our current project when it becomes available.

Next came ASP.NET Futures. There are just so many cool things that are happening in ASP.NET that one presentation really can’t cover it. My favorite, ActiveRecord Integration. And not just with Entity Framework but other data providers. It even sports a “code first” model where you write the classes and just run. The framework creates and wires up a database and you’re off and running. Very nice.

Microsoft ASP.NET 4 Core Runtime for Web Developers. Again, I’m just blown away at the amount of “new stuff” coming in ASP.NET 4. This session focused on new tooling to allow better management of server resources. Frankly, much of it was “over my head” but then that’s sort of the point. Perhaps most interesting is that there are new tools to help find the “bad application” in an app pool that is running multiple applications. If you ever have encountered this problem (I have) you’ll really appreciate this new tooling.

Manycore and the Microsoft .NET Framework 4: A Match Made in Microsoft Visual Studio 2010. Usually by the end of the day I’m burned out and the last session can be a dud for that reason alone. Not this time. I suspect this will be the best breakout session (for me at least) of the conference. I can’t do any justice to it with a summary. Just spend an hour and watch it. It’s that good.

 

Queued Ajax Calls in JQuery

When writing Ajax, you can sometimes get into situations where sending two Ajax calls close together will return results out of order. In other words, the first Ajax call will receive the second Ajax calls’ results. This seems to be a particular problem in IE and Windows Sidebar Gadgets that use IE to display.

Windows Sidebar gadgets are particularly vulnerable in that different instances of gadgets can get each others Ajax data. That’s becasue all gadgets run in the same process, namely sidebar.exe. Argggg! Sometimes I just want to strangle Microsoft. I mean come guys, no one ever thought that two Ajax calls might occur at the same time. LAME!

OK, enough flames. The first step is to at least cut down on the opportunity of “crosstalk” by queuing the Ajax calls within a gadget. I found a several, “Queued Ajax Plugins” for JQuery. Oddly, I could not get any of them to work in JQuery 1.3. The problem appears to be due to a change in the JQuery.queue() method. Apparently, in JQuery 1.2, queue automatically started a dequeue when the first item was added.

I took the simplest of these plugins and modified it to start a dequeue when the first item is added.

Updated: My earlier code was flawed. The code below appears to work much better

var currentAjaxRequest = null;

jQuery.ajaxQueue = function(ajaxOptions)
{
var success = ajaxOptions.success;
var error = ajaxOptions.error;
var complete = ajaxOptions.complete;

ajaxOptions.success = function()
{
if (success) { try { success.apply(this, arguments); } catch (e) { } }
};
ajaxOptions.error = function()
{
if (error) { try { error.apply(this, arguments); } catch (e) { } }
};
ajaxOptions.complete = function()
{
if (complete) { try { complete.apply(this, arguments); } catch (e) { } }
currentAjaxRequest = null;
$(window).dequeue("ajax");
};
$(window).queue("ajax", function()
{
currentAjaxRequest = jQuery.ajax(ajaxOptions);
});
if (currentAjaxRequest === null) { $(window).dequeue("ajax"); }
};


This works just like the JQuery.ajax and uses the same syntax. It’s not perfect, but it’s a start.

 

Setting File Permissions in Wix 3.0

I’m new to Wix and to Windows Installers in general so some of this is likely wrong or obvious. Still, I couldn’t find much information on how to do set file permissions for a particular user account so I thought I’d share.

I needed my installer to set the folder permissions for the App_Data folder in an ASP.NET site. Typically, you need to give the “Network Services” account write permissions to this folder. I scoured the Internet for a solution and posted a question on Stack Overflow before coming up with my own solution.

<CustomAction Id="PermissionAppData" Directory="TARGETDIR" 
ExeCommand="&quot;[SystemFolder]cacls.exe&quot;
&quot;[INSTALLDIR]\App_Data&quot;
/T /E /G &quot;NT AUTHORITY\Network Service:C&quot;"
Return="check" />

Add this custom action to the install sequence table and you’re golden.

There’s a util:PermissionEx custom action in Wix 3 but it seems to only work for “well known” accounts (like Administrators). Maybe there’s a better way but darn if I could come up with it. Feedback appreciated.

 

previous | next

powered by Bloget™