The Wiert Corner – irregular stream of stuff

Jeroen W. Pluimers on .NET, C#, Delphi, databases, and personal interests

  • My work

  • My badges

  • Twitter Updates

  • My Flickr Stream

  • Pages

  • All categories

  • Enter your email address to subscribe to this blog and receive notifications of new posts by email.

    Join 1,822 other followers

Delphi code of the day…

Posted by jpluimers on 2017/09/11

I was debugging an issue where a Delphi SOAP implementation was shoe-horned into an Indy server and came across the [WayBackTWebRequest.UpdateMethodType Method.

The further you get down the if/then/else tree, the more often the indexed property Method is accessed, same for the conversion/comparison code.

property Method: string index 0 read GetStringVariable;

So if the HTTP method is POST (very common), then the calls are being made 3 times:

procedure TWebRequest.UpdateMethodType;
begin
{$IFDEF NEXTGEN}
  if Method = 'GET' then  { do not localize }
    FMethodType := mtGet
  else if Method = 'PUT' then   { do not localize }
    FMethodType := mtPut
  else if Method = 'POST' then  { do not localize }
    FMethodType := mtPost
  else if Method = 'HEAD' then  { do not localize }
    FMethodType := mtHead
  else if Method = 'DELETE' then  { do not localize }
    FMethodType := mtDelete
  else if Method = 'PATCH' then  { do not localize }
    FMethodType := mtPatch;
{$ELSE !NEXTGEN}
  if System.AnsiStrings.AnsiStrComp(PAnsiChar(Method), 'GET') = 0 then  { do not localize }
    FMethodType := mtGet
  else if System.AnsiStrings.AnsiStrComp(PAnsiChar(Method), 'PUT') = 0 then   { do not localize }
    FMethodType := mtPut
  else if System.AnsiStrings.AnsiStrComp(PAnsiChar(Method), 'POST') = 0 then  { do not localize }
    FMethodType := mtPost
  else if System.AnsiStrings.AnsiStrComp(PAnsiChar(Method), 'HEAD') = 0 then  { do not localize }
    FMethodType := mtHead
  else if System.AnsiStrings.AnsiStrComp(PAnsiChar(Method), 'DELETE') = 0 then  { do not localize }
    FMethodType := mtDelete
  else if System.AnsiStrings.AnsiStrComp(PAnsiChar(Method), 'PATCH') = 0 then  { do not localize }
    FMethodType := mtPatch;
{$ENDIF NEXTGEN}
end;

–jeroen

4 Responses to “Delphi code of the day…”

  1. Thaddy said

    It optimizes compile speed ;)

  2. sglienke said

    Who needs case of on strings when you can have half a dozen if statements, right?

  3. ruurd said

    Ha. In that case you surely have used a pessimizing compiler. No invariant code motion in sight.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

 
%d bloggers like this: