IntelliTrace Resources

I enjoyed giving my talk on IntelliTrace at the December CRineta meeting. While doing my research, I saw and read a lot of articles, reviewed a number of presentations and watched a few videos. Some good information and a lot of noise out there. Below is a list of those references that I found useful.

Debugging Applications with IntelliTrace

http://msdn.microsoft.com/en-us/magazine/ee336126.aspx

IntelliTrace Events

http://blogs.msdn.com/b/ianhu/archive/2009/06/18/diagnostic-events-in-historical-debugging.aspx

Navigating your source code with IntelliTrace debugging

http://blogs.msdn.com/b/ianhu/archive/2010/02/26/navigating-your-source-code-with-intellitrace-debugging.aspx

Collecting IntelliTrace Information

http://msdn.microsoft.com/en-us/library/dd264944.aspx

IntelliTrace and CollectionPlan.xml

http://blogs.msdn.com/b/ianhu/archive/2010/02/10/intellitrace-and-collectionplan-xml.aspx

Collecting IntelliTrace Data By Using the Stand-Alone Collector

http://msdn.microsoft.com/en-us/library/hh398365(v=vs.110).aspx

Modifying an IntelliTrace Collection Plan for the Stand-Alone Collector

http://blogs.msdn.com/b/visualstudioalm/archive/2011/09/15/modifying-an-intellitrace-collection-plan-for-the-stand-alone-collector.aspx

VS 2010 : Customize IntelliTrace Events

http://blog.qetza.net/en/2010/03/08/vs-2010-personnalisation-des-vnements-de-lintellitrace/

AOL MailBlogger PostDeliciousDiggFacebookGoogle ReaderGoogle GmailGoogle BookmarksHotmailLiveJournalLinkedInMySpaceSlashdotTwitterYahoo BookmarksWordPressYahoo MailYahoo MessengerShare

SQL Prompt Pro 4.0

I had a nice surprise Monday evening. Checked one of my email accounts and found a note from RedGate Software saying I’d won a license to SQL Prompt Pro for filling out their Community survey. I’ve used their free 1.0 version in the past and it was helpful, though inconsistent in its performance. Both the DBAs on my team, however, have the latest version (4.0) and it absolutely rocks. I’m glad to have it installed. It integrates with both SSMS and Visual Studio so I can use it in both places.

The first thing I did was set the formatting and intellisense options to match our standards. That was simple. Then I created a few code snippets in its snippet manager for some of my most commonly typed query segments. Easy to do and simple to use once entered. It’s intellisense prompts quickly and helpfully. What a time savings in typing SQL!

So far its working as expected. RedGate’s tools are always solid. Besides this tool, I also use their SQL Compare (both my DBA’s have the full SQL Developer Bundle) and their ANTS Profiler as my primary performance profiling tool.

AOL MailBlogger PostDeliciousDiggFacebookGoogle ReaderGoogle GmailGoogle BookmarksHotmailLiveJournalLinkedInMySpaceSlashdotTwitterYahoo BookmarksWordPressYahoo MailYahoo MessengerShare

DynamicMethod speeds up reflective code

Many before me have discovered DynamicMethod. I read about them some time back but only recently finally got a chance to dig into them and apply them to a section of a system I work on that made heavy use of reflection.

The heavily reflective code in question is part of a generated code block from an ORM system that was then customized by some members of our team that are long since gone. The code builds FK relationships among loaded entities (entity = object representing 1 row from a table). The code could, if looked at from many perspectives, be considered artful. It interrelates these objects using only a small amount of very generic code, using reflection and recursion. However, the reflection significantly affects its performance as shown via use of profiling (specifically Red-Gate Software’s ANTS).

In the past I’ve sped the code up by caching some reflected information – arrays of PropertyInfo objects and MethodInfo objects. This helped, but still didn’t alleviate the overhead of using Invoke to call those Getters, Setters and Methods.

Finally I had a chance to look into DynamicMethods. A DynamicMethod, introduced in .NET 2.0, is a lightweight way to create a method at runtime, encapsulating emitted IL code without having to create a dynamic assembly or type. The JITed code is reclaimed when the DynamicMethod is reclaimed. Further, the DynamicMethod can be hosted anonymously or associated with an existing type.

Cool! So how can DynamicMethods help with reflection?

The code was using reflection to call property getters, setters and other methods, both instance and static. A typical call looks like this:

MethodInfo methodInfo =
typeof(Test).
GetMethod(
"InstanceMethodWithParameters",
BindingFlags.Instance | BindingFlags.Public);
methodInfo.Invoke(m_Test, new object[] {100});

// Property Get
PropertyInfo propertyInfo =
typeof(Test).
GetProperty(
"InstanceProperty",
BindingFlags.Instance | BindingFlags.Public);
propertyInfo.GetValue(m_Test, null);

Of course, as I said earlier, I was already caching the MethodInfo and PropertyInfo objects, which helps save on reflection in getting the “info” objects, but not in the Invoke and GetValue.

So in looking around to avoid reinventing the wheel, I found an article on CodeProject with code to generate DynamicMethods that encapsulate constructors, property gets and sets. The author had gone to the trouble to figure out the necessary IL code to emit to essentially convert a PropertyInfo object into a getter or setter wrapper in a DynamicMethod and the IL code to emit to encapsulate creating an object of a specified type, i.e. calling its constructor.

I looked further and found another set of code that could generate IL needed to call any method, using only a MethodInfo as input. Unfortunately, I lost my source URL for this one and can’t seem to find it again.

I tested all this code out first in a simple console app. It worked great. Looking back at my requirements I saw that I needed to reference static properties. The code from CodeProject didn’t support that. Finally, time to get my hands dirty!

Now, I don’t know IL. I’ve read a bit and I’ve written my share of assembly code (6502, 8088 and Macro-11), so this doesn’t scare me, but it’s been awhile. The easiest thing seemed to be to write code to do what I needed in C#, compile it and then use Reflector to see what the IL is for it. So first, some simple code:

public class Test
{
public static string StaticProperty
{
get { return "StaticPropertyHello"; }
set { string x = value; }
}
}
private static void GetStaticProperty()
{
string test = Test.StaticProperty;
}

Viewing the source for GetStaticProperty in Reflector and switching the output language to IL I saw:

.method private hidebysig static void GetStaticProperty() cil managed
{
.maxstack 1
.locals init ([0] string test)
L_0001: call string StaticProperty.Test::get_StaticProperty()
L_0006: stloc.0
L_0007: ret
}

Looking at the “Get” generator code from the CodeProject article, I saw what it was emitting:

getGenerator.Emit(OpCodes.Ldarg_0);
getGenerator.Emit(OpCodes.Call, getMethodInfo);
BoxIfNeeded(getMethodInfo.ReturnType, getGenerator);
getGenerator.Emit(OpCodes.Ret);

Distinctly different is the first instruction, Ldarg_0. This instruction “Loads the argument at index 0 onto the evaluation stack”. Its purpose is loading the instance (“this”) onto the stack. Obviously for a static property, there is no instance. If you look at the reflected IL code for the sample GetStaticProperty method, there is a NOP (no operation) instruction in place of the Ldarg_0. The NOP instruction actually does nothing and can be eliminated when we emit code for retrieving a static property.

So I was able to easily modify the emitted code to eliminate the Ldarg_0 when the property is static (testing PropertyInfo.IsStatic). Compiled, tested, worked, shipped it.

I’ve tried doing the same for the field get / set generator code, but as of yet have been unsuccessful at getting it right for static fields. Works great with instance fields though.

So how much faster is it? The measurements have been done and published before but I’ll share my measurements here. I included a couple of small timings in the unit tests.

Getting an instance property1,000,000 iterations using reflection took 1.0570 s

1,000,000 iterations using DynamicMethod took 0.0150 s

Setting an instance property

1,000,000 iterations using reflection took 1.3280 s

1,000,000 iterations using DynamicMethod took 0.0170 s

Calling an instance method

1,000,000 iterations using reflection took 1.7180 s

1,000,000 iterations using DynamicMethod took 0.3750 s

After incorporating the techniques in the actual code I was targeting, the measurements I took in my total process, which contained much more processing than just these calls, reduced in execution time by 50%. A huge benefit to a heavily used process.

Many thanks to those who’ve worked so much of this out before me. I’ve taken your code (whoever you are), combined it into a single library, added my static property modifications, written unit tests around it and added some comments and will share the compilation for download here CWI.Library.Reflection.zip.

References:

AOL MailBlogger PostDeliciousDiggFacebookGoogle ReaderGoogle GmailGoogle BookmarksHotmailLiveJournalLinkedInMySpaceSlashdotTwitterYahoo BookmarksWordPressYahoo MailYahoo MessengerShare

Presentations on SlideShare.com

I’ve recently uploaded some of my presentations on SlideShare.com. It has a very nice way of handling and displaying slide decks. You can find the 4 I uploaded on my space there http://www.slideshare.net/gregsohl.

The presentations are:

  • Analyzing .NET Memory Usage
  • Application Security Part I
  • .NET Recommended Resources
  • Object Oriented Programming in .NET

The recommended resources presentation is a bit outdated now. I’m hoping to update it soon, and am also working on a “tools I use” page for my blog site.

AOL MailBlogger PostDeliciousDiggFacebookGoogle ReaderGoogle GmailGoogle BookmarksHotmailLiveJournalLinkedInMySpaceSlashdotTwitterYahoo BookmarksWordPressYahoo MailYahoo MessengerShare

Gateway support snubs me, then comes through

I had a simple question. I couldn’t find in my owners manual or on the Gateway web site what the maximum memory (or any memory specs for that matter) are for my laptop (a Gateway MX6961, which I’m very happy with).  So I clicked on the link that allowed me to email support and filled in the form with all my details and my question. What followed was interesting, it went like this:

Me

Subject:  What is the max memory supported in my MX6961 laptop?

Can you tell me what the max memory supported by my laptop is?  The hardware manual doesn’t seem to list it. Your specs page lists:

2048 MB 533 MHz DDR2 Dual Channel memory (2 × 1024 MB)
Total slots: 2 DDR2 slots | Available slots: 0 DDR2 slots

Is this what it comes with or what the max is? I’m interested in upgrading it to 4GB if it supports it.

Gateway

Thank you for contacting Gateway.I would be happy to assist you with the

However, with the serial number RL1014081R7050104. We are unable to pull up the system details.  Please send the correct serial number. The serial number is a 22 or 13 character alphanumeric or 11 digit SNID number which you could find next to or under a bar code in several locations.

On the bottom of a notebook

On the back or side of a desktop

Under the front sliding door on a desktop

On the original computer box

Me

Thanks for the quick reply. About the serial number – I’m not sure what else to give you. That is what it says on the sticker next to the S/N marking on the bottom of the laptop (model MX6961 – refurbished). It is under a barcode.

Can you tell me based on the model number?

Gateway

Thank you for contacting Gateway.I would be happy to assist you with the memory issue

Please note that as per your information your system is a refurbished system. Please call 1-866-xxx-xxxx for support.

Me

Thank you.

You know, whether or not you are supposed to provide me with support on my refurbished system, I’ll bet that in less time than you spent on the two responses to me you could have answered my simple question and had a MUCH HAPPIER Gateway customer.

Gateway

Thank you for contacting Gateway. I’ll be happy to assist you with the memory upgrade issue.
The maximum memory up to which you can expand is 4 GB. Total slots available: 2 DDR2 slots.

How important is customer support today – or in any time period for that matter? The competition in our comoditized industry is such that you must differentiate yourself by your service. Glad Gateway came through end the end.

In retrospect, I should have just gone to Crucial’s great site and searched out the memory specs there.

AOL MailBlogger PostDeliciousDiggFacebookGoogle ReaderGoogle GmailGoogle BookmarksHotmailLiveJournalLinkedInMySpaceSlashdotTwitterYahoo BookmarksWordPressYahoo MailYahoo MessengerShare

Self Documenting Code?

I often hear arguments against code commenting. The opponents frequently say that code should be self documenting. That variable names, method names, etc. should be such that anyone reading the code can tell what is going on. To a degree, they are right of course – about the emphasis on good naming – not about code not needing to be commented because it is self documenting. I’ve written about this before in my short blogging career.

I did a code review today for a developer who was adding some functionality to a preexisting method. The method already had a good “self documenting” name that described its simple, single purpose. However the new code added “broke” that name by having the method do something else. He was aware of this, but the cost of refactoring required to break it out further, in this case, outweighed the benefit of doing so – it was that simple. So instead I had to call him on the now “broken” method name.

We pondered for a few moments, trying to cleverly come up with an appropriately descriptive name. We failed – at the cleverly part. Instead the new name that is fully self-documenting is:

RemoveFootnoteAreasAndOnlyUseFirstColumnHeaders

Maybe they were right? Of course, it still doesn’t tell you “WHY” the footnotes are being removed and “WHY” we would only use the first column headers.

AOL MailBlogger PostDeliciousDiggFacebookGoogle ReaderGoogle GmailGoogle BookmarksHotmailLiveJournalLinkedInMySpaceSlashdotTwitterYahoo BookmarksWordPressYahoo MailYahoo MessengerShare

JetBrains support for ReSharper helps me out

This last week we upgraded our team to ReSharper 4.5. We’d been on 3.1 since about the time it came out, which was last 2007 or so. So far we’re enjoying the enhancements and speed improvements. I’ve had several positive comments from our developers.

I wanted to point out to our developers all the cool new features since 3.1, since we skipped the 4.0 and 4.1 releases. I couldn’t find the old release notes on their site so I submitted a support request asking for them. They wrote back and the support rep said he didn’t have them handy but would work on it. The next day another rep replied to me and had updated the web site republishing the former release notes. So now you can find all the release notes from 2.0 on at http://www.jetbrains.com/resharper/features/newfeatures.html. Thanks JetBrains – ReSharper rocks!.

AOL MailBlogger PostDeliciousDiggFacebookGoogle ReaderGoogle GmailGoogle BookmarksHotmailLiveJournalLinkedInMySpaceSlashdotTwitterYahoo BookmarksWordPressYahoo MailYahoo MessengerShare

Azure at CRineta

I spoke at CRineta Monday night, using materials provided by Jeff Brand one of our Microsoft evangalists. This was basically a repeat of a session from the PDC aptly presented by Steve Marx. The talk covered creating basic Azure services and using the built-in storage mechanisms – blobs, tables and queues.

Azure is an interesting hosting platform. I dare say there’s not been anything like it available before, providing a familiar, consistent and integrated programming environment with an advanced hosting environment with built in scalability and failover. I definitely think it is worth watching as it matures. I’m anxious to see the pricing model when it is available, which is a sentiment echoed by several folks at the talk.

Now with that done, on to spring Iowa Code Camp!

AOL MailBlogger PostDeliciousDiggFacebookGoogle ReaderGoogle GmailGoogle BookmarksHotmailLiveJournalLinkedInMySpaceSlashdotTwitterYahoo BookmarksWordPressYahoo MailYahoo MessengerShare

Why you should comment the "Why"

We’d been having a discussion about code commenting on our CRineta.org discussion group. I’d been asserting that we need to comment “WHY” a piece of code is in place more so than “WHAT” it does. Chris requested for me to show what “WHYcomments would look like on a specific piece of code. I thought about it for awhile but soon was distracted by real life. I continued to think about it and realized why I was unable to easily come up with a “WHY” comment for it. The reason is exactly the reason I’ve been asserting the need to write theWHYcomments along with the code … because by the time I read this piece of code (the one Chris asked me to demonstrate on) theWHY” information wasn’t available to me. I could have made something up, but in truth, the person who wrote the block of code is the one who knew why it was written, its purpose for being.

While doing a code review today, I found a good example. Here’s the original code as I reviewed it – with the comments from a guy who generally does a good job of commenting his code.

// Retrieve the statement info so we have it.
StatementInfoCache.Instance.GetStatementInfo(statementID);

// Start the delete statement job on the server.
Guid jobID = Guid.NewGuid();

StatementUsageResponse response =
StatementListModelHelper.DeleteStatement(
this, statementID, jobID);

if (response.RequestResult == StatementUsageResponseResult.Successful)
JobManager.JobManager.Instance.AddJob(jobID);

return response;

Focus on the first line. I asked about it during the code review. I asked WHY do you have to get the statement info? Knowing what I did at the time, I already knew what “so we have it” meant. But why did we need it, and would we remember why we had to have it one month from now? How about 6 months from now?

So the developer revised the comment as follows:

Version 2:

// Retrieve the statement info because we can’t after the delete.

Ah – true. That is a reason we have to get it now, because we are doing a DeleteStatement operation and the Statement Info won’t be available after the delete. But still I asked – WHY do you need to have the statement info? The line isn’t just there for our health? So he modified the comment again with a bit more prompting.

Version 3:

// Retrieve the statement info because we can’t after the delete.
// This is so the notification box and job monitor can show the statement description.

Wonderful. Now I know WHY we are getting the Statement Info AND I know WHY we are getting it now.

This is the kind of information that one would have to do significant searching for at a later date when trying to understand the code, which was originally:

// Retrieve the statement info so we have it.
StatementInfoCache.Instance.GetStatementInfo(statementID);

I’ve read others opinions on code commenting such as Jeffery Palermo’s.  I think that many peoples perspective on commenting code, especially those that say that code should be written to be self documenting, aren’t necessarily wrong, they are just focused on the wrong nature of how comments should be applied.

So I say, “Tell me WHY“.

AOL MailBlogger PostDeliciousDiggFacebookGoogle ReaderGoogle GmailGoogle BookmarksHotmailLiveJournalLinkedInMySpaceSlashdotTwitterYahoo BookmarksWordPressYahoo MailYahoo MessengerShare

XAML Compilation?

The most interesting thing I learned in chapter 1 and 2 of Windows Presentation Foundation Unleashed yesterday was about how XAML is handled during compilation. Don’t tell your designers, but XAML is as much a programming language as it is simple markup. During compilation of a WPF application XAML is compiled and a BAML file is output. The closest corollary to BAML in the .NET world I see is IL, but clearly it is NOT IL.  BAML is a tokenized version of the original XAML file that is optimized for loading during runtime. So XAML is, at least partially, interpreted at runtime. The BAML file is eventually brought in as a resource file to your DLL or EXE, as can be seen here in Reflector for a simple WPF app’s EXE:

WPF App BAML Resource

WPF App BAML Resource

Also output is a corresponding VB or C# file with a partial class that contains the framework code to use the XAML file and member variables (internal visibility) for each named element.

So if someone asks you – “Is XAML compiled or interpreted?”, you can just respond with a simple, “Yes”.

Additional References:

http://msdn.microsoft.com/en-us/library/aa970678.aspx

AOL MailBlogger PostDeliciousDiggFacebookGoogle ReaderGoogle GmailGoogle BookmarksHotmailLiveJournalLinkedInMySpaceSlashdotTwitterYahoo BookmarksWordPressYahoo MailYahoo MessengerShare