strings bit me [dang-it!]

2008-04-11 @ 17:50#

i'm doing some optimizing of log-display screens and found myself 'stuck' with a lousy performing HTTP page. even after i added support for GZip and a local in-memory caching pattern to prevent having to transform the log records for each request. even after all that, it was still slow. now the delivered page is close to 500KB (it's a sever log, eh?). but still, things should be quite zippy after the first request (caching and all that....)

i showed this to a colleague and it took him less than sixty seconds to cut the response time in half.

see if you can guess (from my pseudo code):

// shared var
protected p_etag = string.Empty;

private void Get()
{
  string rtn = string.Empty;
  string ifnonematch = string.Empty;
  
  rtn = HandleFileRequest(context.Request.QueryString["file"];)
  
  if(context.Request.Headers["if-none-match"]==p_etag)
  {
    SendResponse(304,"","");
  }
  else
  {
    SendResponse(200,rtn,p_etag);
  }

  return;
}

private string HandleFile(string file)
{
  string rtn = string.Empty;
  CacheObject co = null;
  
  co = CheckMemory(file);
  if(co!=null)
  {
    rtn = co.Payload;
    p_etag = co.ETag;
  }
  else
  {
    rtn = GetDiskFile(file);
    co = new CacheObject(rtn,computeETag(rtn));
    p_etag = co.ETag;
  }
  
  return rtn;
}

assume i have a 400KB string to send to the client (after transformation). how many copies of that string are created by the code above?

dang-it!

code