troubleshooting Question

Optimization

Avatar of Eriandus
EriandusFlag for Poland asked on
Delphi
12 Comments1 Solution395 ViewsLast Modified:
Hello,
how can I optimize following functions? They're still working too slow causing my application to lag, and I've got absolutely no idea how to fix it.

Note: I won't assign points for people who repeated what someone else said.
type
  TPacket = array of byte;
  TNetMsg = class
  private
    Packet : TPacket;
    PacketType : Integer;
    ...  
  end;
 
...
 
function TNetMsg.Get: TPacket;
var
  DataSize : Integer;
  EncryptedSize : Integer;
  I : Integer;
begin
  if BlockPacket then begin
    SetLength(Result, 10);
    for I := 0 to 9 do begin
      Result[I] := 0;
    end;
    Exit;
  end;
  DataSize := 1 + EndPos;
  EncryptedSize := RoundEx(DataSize + 2, 8);
  SetLength(Result, EncryptedSize+2);
 
  Result[0] := Lo(EncryptedSize);
  Result[1] := Hi(EncryptedSize);
  Result[2] := Lo(DataSize);
  Result[3] := Hi(DataSize);
  Result[4] := PacketType;
  CopyMemory(@Result[5], @Packet[StartPos], EndPos);
  for I := EndPos+5 to EncryptedSize+1 do begin
    Result[I] := Random($FF);
  end;
end;
 
function TNetMsg.GetRest: TPacket;
var
  DataSize : Integer;
  EncryptedSize : Integer;
  I : Integer;
begin
  if BlockPacket then begin
    SetLength(Result, 10);
    for I := 0 to 9 do begin
      Result[I] := 0;
    end;
    Exit;
  end;
  DataSize := 1 + (Length(Packet) - StartPos);
  EncryptedSize := RoundEx(DataSize + 2, 8);
  SetLength(Result, EncryptedSize+2);
 
  Result[0] := Lo(EncryptedSize);
  Result[1] := Hi(EncryptedSize);
  Result[2] := Lo(DataSize);
  Result[3] := Hi(DataSize);
  Result[4] := PacketType;
  CopyMemory(@Result[5], @Packet[StartPos], DataSize-1);
  for I := DataSize+4 to EncryptedSize+1 do begin
    Result[I] := Random($FF);
  end;
end;
Join the community to see this answer!
Join our exclusive community to see this answer & millions of others.
Unlock 1 Answer and 12 Comments.
Join the Community
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 12 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros