Recent

Author Topic: [SOLVED] Improvement of function TSocketServer.RunIdleLoop  (Read 804 times)

lagprogramming

  • Sr. Member
  • ****
  • Posts: 403
[SOLVED] Improvement of function TSocketServer.RunIdleLoop
« on: July 15, 2023, 10:46:43 am »
packages/fcl-net/src/ssockets.pp has the following function:
Code: Pascal  [Select][+][-]
  1. function TSocketServer.RunIdleLoop: Boolean;
  2.  
  3. // Run Accept idle loop. Return True if there is a new connection waiting
  4. {$if defined(unix) or defined(windows)}
  5. var
  6.   FDS: TFDSet;
  7.   TimeV: TTimeVal;
  8. {$endif}
  9. begin
  10.   Repeat
  11.     Result:=False;
  12. {$if defined(unix) or defined(windows)}
  13.     TimeV.tv_usec := (AcceptIdleTimeout mod 1000) * 1000;
  14.     TimeV.tv_sec := AcceptIdleTimeout div 1000;
  15. {$endif}
  16. {$ifdef unix}
  17.     FDS := Default(TFDSet);
  18.     fpFD_Zero(FDS);
  19.     fpFD_Set(FSocket, FDS);
  20.     Result := fpSelect(FSocket + 1, @FDS, @FDS, @FDS, @TimeV) > 0;
  21. {$else}
  22. {$ifdef windows}
  23.     FDS := Default(TFDSet);
  24.     FD_Zero(FDS);
  25.     FD_Set(FSocket, FDS);
  26.     Result := winsock2.Select(FSocket + 1, @FDS, @FDS, @FDS, @TimeV) > 0;
  27. {$endif}
  28. {$endif}
  29.     If not Result then
  30.       DoOnIdle;
  31.   Until Result or (Not FAccepting);
  32. end;
The repeat loop contains an unnecessary conditional jump.

The patch at the end of the post changes
Code: Pascal  [Select][+][-]
  1. If not Result then
  2.   DoOnIdle;
  3. Until Result or (Not FAccepting);
with
Code: Pascal  [Select][+][-]
  1. If Result then
  2.   break;
  3. DoOnIdle;
  4. Until (Not FAccepting);

Here is the patch
Code: Pascal  [Select][+][-]
  1. diff --git a/packages/fcl-net/src/ssockets.pp b/packages/fcl-net/src/ssockets.pp
  2. index 755af6d8bf..26da36d7fa 100644
  3. --- a/packages/fcl-net/src/ssockets.pp
  4. +++ b/packages/fcl-net/src/ssockets.pp
  5. @@ -928,9 +928,10 @@ function TSocketServer.RunIdleLoop: Boolean;
  6.      Result := winsock2.Select(FSocket + 1, @FDS, @FDS, @FDS, @TimeV) > 0;
  7.  {$endif}
  8.  {$endif}
  9. -    If not Result then
  10. -      DoOnIdle;
  11. -  Until Result or (Not FAccepting);
  12. +    If Result then
  13. +      break;
  14. +    DoOnIdle;
  15. +  Until (Not FAccepting);
  16.  end;
  17.  
  18.  procedure TSocketServer.Listen;
« Last Edit: July 16, 2023, 01:28:39 pm by lagprogramming »

bytebites

  • Hero Member
  • *****
  • Posts: 614
Re: Improvement of function TSocketServer.RunIdleLoop
« Reply #1 on: July 15, 2023, 07:21:53 pm »
Code: Pascal  [Select][+][-]
  1. Until (Not FAccepting);
  2.  
Unnecessary ().

runewalsh

  • Jr. Member
  • **
  • Posts: 73
Re: Improvement of function TSocketServer.RunIdleLoop
« Reply #2 on: July 15, 2023, 07:31:22 pm »
Also
Code: Pascal  [Select][+][-]
  1. {$ifdef unix}
  2.     FDS := Default(TFDSet);
  3.     fpFD_Zero(FDS);
  4.     fpFD_Set(FSocket, FDS);
  5.     Result := fpSelect(FSocket + 1, @FDS, @FDS, @FDS, @TimeV) > 0;
  6. {$else}
  7. {$ifdef windows}
  8.     FDS := Default(TFDSet);
  9.     FD_Zero(FDS);
  10.     FD_Set(FSocket, FDS);
  11.     Result := winsock2.Select(FSocket + 1, @FDS, @FDS, @FDS, @TimeV) > 0;
  12. {$endif}
  13. {$endif}

Code: Pascal  [Select][+][-]
  1. {$ifdef unix}
  2.     FDS := Default(TFDSet);
  3.     fpFD_Zero(FDS);
  4.     fpFD_Set(FSocket, FDS);
  5.     Result := fpSelect(FSocket + 1, @FDS, @FDS, @FDS, @TimeV) > 0;
  6. {$elseif defined(windows)}
  7.     FDS := Default(TFDSet);
  8.     FD_Zero(FDS);
  9.     FD_Set(FSocket, FDS);
  10.     Result := winsock2.Select(FSocket + 1, @FDS, @FDS, @FDS, @TimeV) > 0;
  11. {$endif}

Josh

  • Hero Member
  • *****
  • Posts: 1254
Re: Improvement of function TSocketServer.RunIdleLoop
« Reply #3 on: July 16, 2023, 11:10:24 am »
maybe
Code: Pascal  [Select][+][-]
  1. function TSocketServer.RunIdleLoop: Boolean;
  2. // Run Accept idle loop. Return True if there is a new connection waiting
  3. {$if defined(unix) or defined(windows)}
  4. var
  5.   FDS: TFDSet;
  6.   TimeV: TTimeVal;
  7. {$endif}
  8. begin
  9.   Repeat
  10.     Result:=False;
  11.     {$if defined(unix) or defined(windows)}
  12.     TimeV.tv_usec := (AcceptIdleTimeout mod 1000) * 1000;
  13.     TimeV.tv_sec := AcceptIdleTimeout div 1000;
  14.     FDS := Default(TFDSet);
  15.     {$ifdef unix}
  16.     fpFD_Zero(FDS);
  17.     fpFD_Set(FSocket, FDS);
  18.     Result := fpSelect(FSocket + 1, @FDS, @FDS, @FDS, @TimeV) > 0;
  19.     {$else}
  20.     FD_Zero(FDS);
  21.     FD_Set(FSocket, FDS);
  22.     Result := winsock2.Select(FSocket + 1, @FDS, @FDS, @FDS, @TimeV) > 0;
  23.     {$endif}
  24.     {$endif}
  25.     If Result then break;
  26.     DoOnIdle;
  27.   Until (Not FAccepting);
  28. end;              
The best way to get accurate information on the forum is to post something wrong and wait for corrections.


 

TinyPortal © 2005-2018