Skip to content

Commit f601f03

Browse files
committed
refactor: ANother patch from sbalazs
1 parent 5c99be2 commit f601f03

File tree

1 file changed

+20
-50
lines changed

1 file changed

+20
-50
lines changed

entries/sbalazs/src/uweatherstations.pas

Lines changed: 20 additions & 50 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
{$mode ObjFPC}{$H+}
44
{$R-} {$Q-}
5+
{$INLINE ON}
56

67
interface
78

@@ -14,7 +15,6 @@ interface
1415
type
1516
TWSManager = class;
1617

17-
type
1818
TData = record
1919
FMin: Int64;
2020
FMax: Int64;
@@ -70,11 +70,10 @@ TWSThread = class(TWSThreadBase)
7070
private
7171
FStartP: Int64;
7272
FEndP: Int64;
73-
FMS: TMemoryStream;
7473
FWSList: TWSList;
7574
procedure UpdateMainHashList;
76-
procedure ProcessBytes(ABytes: TBytes);
77-
procedure AddToHashList(AStation: TBytes; ATemp: Int64; AHash: QWord);
75+
procedure ProcessBytes(ABytes: TBytes); inline;
76+
procedure AddToHashList(ABytes: TBytes; AStartP, AEndP: Int64; ATemp: Int64; AHash: QWord);
7877
procedure UpdateThreadList(AUpdateType: TUpdateType);
7978
protected
8079
procedure Execute; override;
@@ -85,7 +84,7 @@ TWSThread = class(TWSThreadBase)
8584

8685
TWSThreadsWatcher = class(TWSThreadBase)
8786
private
88-
function RoundEx(x: Double): Double;
87+
function RoundEx(x: Double): Double; inline;
8988
function PascalRound(x: Double): Double;
9089
procedure CreateFinalList;
9190
protected
@@ -244,12 +243,10 @@ constructor TWSThread.Create(AStartP, AEndP: Int64; AWSManager: TWSManager);
244243
FStartP := AStartP;
245244
FEndP := AEndP;
246245
FStarted := False;
247-
FMS := TMemoryStream.Create;
248246
end;
249247

250248
destructor TWSThread.Destroy;
251249
begin
252-
FMS.Free;
253250
inherited Destroy;
254251
end;
255252

@@ -284,7 +281,6 @@ procedure TWSThread.ProcessBytes(ABytes: TBytes);
284281
PBeg: Int64;
285282
PDel: Int64;
286283
Len: Int64;
287-
Station: TBytes;
288284
Temp: Int64;
289285
DoHash: Boolean;
290286
DoTemp: Boolean;
@@ -295,7 +291,6 @@ procedure TWSThread.ProcessBytes(ABytes: TBytes);
295291
PBeg := 0;
296292
PDel := 0;
297293
Temp := 1000;
298-
Station := nil;
299294
DoHash := True;
300295
DoTemp := False;
301296
Len := Length(ABytes);
@@ -338,13 +333,10 @@ procedure TWSThread.ProcessBytes(ABytes: TBytes);
338333
end;
339334
if (ABytes[PCur] = 10) and (PCur < Len) then
340335
begin
341-
SetLength(Station, PDel - PBeg);
342-
Move(ABytes[PBeg], Station[0], PDel - PBeg);
343-
if (Station <> nil) and (Temp < 1000) then
344-
AddToHashList(Station, Temp, Hash);
336+
if (Temp < 1000) then
337+
AddToHashList(ABytes, PBeg, PDel, Temp, Hash);
345338
Hash := 14695981039346656037;
346339
DoHash := True;
347-
Station := nil;
348340
Temp := 1000;
349341
DoTemp := False;
350342
PBeg := PCur + 1;
@@ -354,12 +346,9 @@ procedure TWSThread.ProcessBytes(ABytes: TBytes);
354346

355347
procedure TWSThread.Execute;
356348
var
357-
ReadCnt: LongInt;
358-
Size: Int64;
359-
Bytes: TBytes;
360-
BytesEx: TBytes;
361-
Len, LenEx: LongInt;
362349
I: Integer;
350+
Bytes: TBytes;
351+
ReadCnt: LongInt;
363352
FS: TFileStream;
364353
begin
365354
UpdateThreadList(utAdd);
@@ -373,43 +362,24 @@ procedure TWSThread.Execute;
373362
FWSList[I].FData.FTot := 0;
374363
FWSList[I].FData.FCnt := 0;
375364
end;
365+
Bytes := nil;
376366
FS := TFileStream.Create(FWSManager.FSrcFile, fmOpenRead or fmShareDenyWrite);
377367
try
378368
FS.Position := FStartP;
379-
FMS.SetSize(FEndP - FStartP);
380-
FS.Read(FMS.Memory^, FEndP - FStartP);
381-
FMS.Position := 0;
369+
SetLength(Bytes, FEndP - FStartP);
370+
ReadCnt := FS.Read(Bytes[0], FEndP - FStartP);
371+
if ReadCnt > 0 then
372+
ProcessBytes(Bytes);
382373
finally
383374
FS.Free;
384375
end;
385-
Size := 1048576;
386-
repeat
387-
Bytes := nil;
388-
BytesEx := nil;
389-
if Size > FMS.Size - FMS.Position then
390-
Size := FMS.Size - FMS.Position;
391-
SetLength(Bytes, Size);
392-
ReadCnt := FMS.Read(Bytes[0], Size);
393-
if (ReadCnt > 0) then
394-
begin
395-
BytesEx := GetNextLineBreak(FMS);
396-
LenEx := Length(BytesEx);
397-
if LenEx > 0 then
398-
begin
399-
Len := Length(Bytes);
400-
SetLength(Bytes, Len + LenEx);
401-
Move(BytesEx[0], Bytes[Len], LenEx);
402-
end;
403-
AddLineBreak(Bytes);
404-
ProcessBytes(Bytes);
405-
end;
406-
until (ReadCnt = 0);
407376
finally
408377
UpdateThreadList(utRemove);
409378
end;
410379
end;
411380

412-
procedure TWSThread.AddToHashList(AStation: TBytes; ATemp: Int64; AHash: QWord);
381+
procedure TWSThread.AddToHashList(ABytes: TBytes; AStartP, AEndP: Int64;
382+
ATemp: Int64; AHash: QWord);
413383
var
414384
Index: Integer;
415385
begin
@@ -418,16 +388,16 @@ procedure TWSThread.AddToHashList(AStation: TBytes; ATemp: Int64; AHash: QWord);
418388
begin
419389
if FWSList[Index].FStation = nil then
420390
begin
421-
SetLength(FWSList[Index].FStation, Length(AStation));
422-
Move(AStation[0], FWSList[Index].FStation[0], Length(AStation));
391+
SetLength(FWSList[Index].FStation, AEndP - AStartP);
392+
Move(ABytes[AStartP], FWSList[Index].FStation[0], AEndP - AStartP);
423393
FWSList[Index].FData.FMin := ATemp;
424394
FWSList[Index].FData.FMax := ATemp;
425395
FWSList[Index].FData.FTot := ATemp;
426396
FWSList[Index].FData.FCnt := 1;
427397
FWSList[Index].FData.FHash := AHash;
428398
Break;
429399
end;
430-
if CompareMem(@FWSList[Index].FStation[0], @AStation[0], Length(AStation)) then
400+
if CompareMem(@FWSList[Index].FStation[0], @ABytes[AStartP], AEndP - AStartP) then
431401
begin
432402
FWSList[Index].FData.FMin := min(FWSList[Index].FData.FMin, ATemp);
433403
FWSList[Index].FData.FMax := max(FWSList[Index].FData.FMax, ATemp);
@@ -537,7 +507,7 @@ procedure TWSThreadsWatcher.CreateFinalList;
537507
Min, Max: Double;
538508
Mean: Double;
539509
SL: TStringList;
540-
MS: TMemoryStream;
510+
//MS: TMemoryStream;
541511
begin
542512
SL := TStringList.Create;
543513
try
@@ -557,8 +527,8 @@ procedure TWSThreadsWatcher.CreateFinalList;
557527
Str := Name + '=' + FormatFloat('0.0', Min) + '/' + FormatFloat('0.0', Mean) + '/' + FormatFloat('0.0', Max) + ',';
558528
SL.Add(Str);
559529
end;
560-
SL.EndUpdate;
561530
SL.CustomSort(@Compare);
531+
SL.EndUpdate;
562532
Str := SL.Text;
563533
finally
564534
SL.Free;

0 commit comments

Comments
 (0)