A Developer's Diary

Feb 17, 2008

80-20 Rule

According to 80-20 Rule: Look for that 20% of the code where your program spends 80% of it's time

e.g. The HTTP specification is a 100 page document that describes all possible HTTP requests that a web server must handle.Most of the HTTP requests that traverse through the web are very simple. They contain only a small subset of the possible HTTP headers that a request can possibly contain.

Since Microsoft and Netscape have a dominating share of the browser market, all you need to do is peek at the request headers sent by these two browsers. This is yet another manifestation of the 80-20 rule—20% of your possible inputs will occur 80% of the time. An efficient use of programmer resources is to tune those 20% of the request types that appear 80% of the time.
The HTTP Accept header is part of an HTTP request. It specifies what document formats are acceptable to the browser. The HTTP specification allows for the Accept header to have any combination of upper and lowercase. When you read a string token and want to determine if it is the Accept header, it is not enough to perform

memcmp("Accept:", header, 7)

We need to perform a case-sensitive string compare. We implemented a home-grown version for a casesensitive string-compare:

int memCaseCmp(char*,char*, int) {...}

To be HTTP compliant, the correct action should be

Code
if ( 0 == memCaseCmp("accept:", header, 7) )
{
// This is the Accept header
}

However, memCaseCmp() is not cheap. It needs to uppercase both strings and then call memcmp(). This is where our domain expertise must come into play. Like we said, Microsoft and Netscape have a commanding share of the browser market. The Accept header they send happens to be "Accept:". This is only one of the many upper- and lowercase combination that HTTP allows, but it is the one we are going to receive 95% of the time, so it is the one we care about. The following test tries to take advantage of that:

Code
if ( (0 == memcmp("Accept:", header,7)) || // An intelligent // gamble... (0 == memCaseCmp("accept:", header, 7))) { // This is the Accept header }

In 95% of the inputs, the memcmp() test is going to succeed, and we will never call the more expensive memCaseCmp().

No comments :

Post a Comment