Skip to content

Improvement of function TSocketServer.RunIdleLoop

By 'lagprogramming'.

packages/fcl-net/src/ssockets.pp has the following function:

function TSocketServer.RunIdleLoop: Boolean;
 
// Run Accept idle loop. Return True if there is a new connection waiting
{$if defined(unix) or defined(windows)}
var
  FDS: TFDSet;
  TimeV: TTimeVal;
{$endif}
begin
  Repeat
    Result:=False;
{$if defined(unix) or defined(windows)}
    TimeV.tv_usec := (AcceptIdleTimeout mod 1000) * 1000;
    TimeV.tv_sec := AcceptIdleTimeout div 1000;
{$endif}
{$ifdef unix}
    FDS := Default(TFDSet);
    fpFD_Zero(FDS);
    fpFD_Set(FSocket, FDS);
    Result := fpSelect(FSocket + 1, @FDS, @FDS, @FDS, @TimeV) > 0;
{$else}
{$ifdef windows}
    FDS := Default(TFDSet);
    FD_Zero(FDS);
    FD_Set(FSocket, FDS);
    Result := winsock2.Select(FSocket + 1, @FDS, @FDS, @FDS, @TimeV) > 0;
{$endif}
{$endif}
    If not Result then
      DoOnIdle;
  Until Result or (Not FAccepting);
end;

The repeat loop contains an unnecessary conditional jump.

The patch at the end of the post changes

    If not Result then
      DoOnIdle;
    Until Result or (Not FAccepting);

with

    If Result then
      break;
    DoOnIdle;
    Until (Not FAccepting);

Here is the patch

diff --git a/packages/fcl-net/src/ssockets.pp b/packages/fcl-net/src/ssockets.pp
index 755af6d8bf..26da36d7fa 100644
--- a/packages/fcl-net/src/ssockets.pp
+++ b/packages/fcl-net/src/ssockets.pp
@@ -928,9 +928,10 @@ function TSocketServer.RunIdleLoop: Boolean;
     Result := winsock2.Select(FSocket + 1, @FDS, @FDS, @FDS, @TimeV) > 0;
 {$endif}
 {$endif}
-    If not Result then
-      DoOnIdle;
-  Until Result or (Not FAccepting);
+    If Result then
+      break;
+    DoOnIdle;
+  Until (Not FAccepting);
 end;
 
 procedure TSocketServer.Listen;
To upload designs, you'll need to enable LFS and have an admin enable hashed storage. More information