Recent

Author Topic: [SOLVED] Improvement of function EncodeURI  (Read 2496 times)

lagprogramming

  • Sr. Member
  • ****
  • Posts: 390
[SOLVED] Improvement of function EncodeURI
« on: September 22, 2023, 09:46:29 am »
packages/fcl-base/src/uriparser.pp has
Code: Pascal  [Select][+][-]
  1. function EncodeURI(const URI: TURI): String;
  2. // ! if no scheme then first colon in path should be escaped
  3. begin
  4.   SetLength(Result, 0);
  5.   if Length(URI.Protocol) > 0 then
  6.     Result := LowerCase(URI.Protocol) + ':';
  7.   if URI.HasAuthority then
  8.   begin
  9.     Result := Result + '//';
  10.     if Length(URI.Username) > 0 then
  11.     begin
  12.       Result := Result + URI.Username;
  13.       if Length(URI.Password) > 0 then
  14.         Result := Result + ':' + URI.Password;
  15.       Result := Result + '@';
  16.     end;
  17.     Result := Result + URI.Host;
  18.   end;
  19.   if URI.Port <> 0 then
  20.     Result := Result + ':' + IntToStr(URI.Port);
  21.   Result := Result + Escape(URI.Path, ValidPathChars);
  22.   if Length(URI.Document) > 0 then
  23.   begin
  24.     if (Length(URI.Path) > 0) and ((Length(Result) = 0) or (Result[Length(Result)] <> '/')) then
  25.       Result := Result + '/';
  26.     Result := Result + Escape(URI.Document, ValidPathChars);
  27.   end;
  28.   if Length(URI.Params) > 0 then
  29.     Result := Result + '?' + Escape(URI.Params, ValidPathChars);
  30.   if Length(URI.Bookmark) > 0 then
  31.     Result := Result + '#' + Escape(URI.Bookmark, ValidPathChars);
  32. end;
The initial "SetLength(Result, 0);" line has been replaced with "Result := '';".
All comparisons of Length(string) with zero have been replaced by comparisons of string with ''.
The function becomes:
Code: Pascal  [Select][+][-]
  1. function EncodeURI(const URI: TURI): String;
  2. // ! if no scheme then first colon in path should be escaped
  3. begin
  4.   Result := '';
  5.   if URI.Protocol <> '' then
  6.     Result := LowerCase(URI.Protocol) + ':';
  7.   if URI.HasAuthority then
  8.   begin
  9.     Result := Result + '//';
  10.     if URI.Username <> '' then
  11.     begin
  12.       Result := Result + URI.Username;
  13.       if URI.Password <> '' then
  14.         Result := Result + ':' + URI.Password;
  15.       Result := Result + '@';
  16.     end;
  17.     Result := Result + URI.Host;
  18.   end;
  19.   if URI.Port <> 0 then
  20.     Result := Result + ':' + IntToStr(URI.Port);
  21.   Result := Result + Escape(URI.Path, ValidPathChars);
  22.   if URI.Document <> '' then
  23.   begin
  24.     if (URI.Path <> '') and ((Result = '') or (Result[Length(Result)] <> '/')) then
  25.       Result := Result + '/';
  26.     Result := Result + Escape(URI.Document, ValidPathChars);
  27.   end;
  28.   if URI.Params <> '' then
  29.     Result := Result + '?' + Escape(URI.Params, ValidPathChars);
  30.   if URI.Bookmark <> '' then
  31.     Result := Result + '#' + Escape(URI.Bookmark, ValidPathChars);
  32. end;
The only comparison of a length of a string with zero in the entire file remained inside function ParseURI, so that line has been modified from "if Length(Authority)>0 then" to "if Authority <> '' then".
A patch has been attached.
« Last Edit: November 17, 2023, 01:23:03 pm by lagprogramming »

Fibonacci

  • Full Member
  • ***
  • Posts: 219
  • #PDK
Re: Improvement of function EncodeURI
« Reply #1 on: September 22, 2023, 10:13:24 am »
Out of curiosity I checked who and when would initialize the result to an empty string using SetLength(), it turns out that this file was added 20 years ago on 17.05.2003, until now this line was unchanged. The author is still an active maintainer with last commit just 5 days ago.

Hello Michaël!

runewalsh

  • Jr. Member
  • **
  • Posts: 72
Re: Improvement of function EncodeURI
« Reply #2 on: September 22, 2023, 10:30:23 am »
I tried to make a cool MR that should supposedly do the Length > 0 thing automatically, so maybe wait for one to ten years before rushing into ''s.

AlexTP

  • Hero Member
  • *****
  • Posts: 2292
    • UVviewsoft

AlexTP

  • Hero Member
  • *****
  • Posts: 2292
    • UVviewsoft
Re: Improvement of function EncodeURI
« Reply #4 on: October 03, 2023, 09:30:23 am »
It was applied.

New idea for this func optimization.
Don't concat small strings. But collect parts in the TStringArray with predefined Length (about 10). When all done, concat all StringArray parts via one mem realloc (like TStringList.Text does).
« Last Edit: October 03, 2023, 10:42:52 am by AlexTP »

Leledumbo

  • Hero Member
  • *****
  • Posts: 8717
  • Programming + Glam Metal + Tae Kwon Do = Me
Re: Improvement of function EncodeURI
« Reply #5 on: October 17, 2023, 08:47:04 am »
Don't concat small strings. But collect parts in the TStringArray with predefined Length (about 10). When all done, concat all StringArray parts via one mem realloc (like TStringList.Text does).
I don't think it will have significant improvement in a language with mutable strings, but a benchmark should be able to reveal the truth.

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 11079
  • FPC developer.
Re: Improvement of function EncodeURI
« Reply #6 on: October 17, 2023, 11:53:15 am »
TStringBuilder ?

 

TinyPortal © 2005-2018