Skip to content

Commit

Permalink
optimization: avoid resetting the array length when unnecessary
Browse files Browse the repository at this point in the history
  • Loading branch information
rejetto committed Jun 28, 2020
1 parent 863c667 commit 6c5139d
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions main.pas
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{
{
Copyright (C) 2002-2020 Massimo Melina (www.rejetto.com)
This file is part of HFS ~ HTTP File Server.
Expand Down Expand Up @@ -1365,6 +1365,7 @@ function findNameInDescriptionFile(txt, name:string):integer;

type
TfileListing = class
actualCount: integer;
public
dir: array of Tfile;
ignoreConnFilter: boolean;
Expand Down Expand Up @@ -1517,16 +1518,13 @@ procedure loadIon(path:string; comments:TstringList);
function TfileListing.fromFolder(folder:Tfile; cd:TconnData;
recursive:boolean=FALSE; limit:integer=-1; toSkip:integer=-1; doClear:boolean=TRUE):integer;
var
actualCount: integer;
seeProtected, noEmptyFolders, forArchive: boolean;
filesFilter, foldersFilter, urlFilesFilter, urlFoldersFilter: string;

procedure recurOn(f:Tfile);
begin
if not f.isFolder() then exit;
setLength(dir, actualCount);
toSkip:=fromFolder(f, cd, TRUE, limit, toSkip, FALSE);
actualCount:=length(dir);
end; // recurOn

procedure addToListing(f:Tfile);
Expand All @@ -1540,7 +1538,11 @@ function TfileListing.fromFolder(folder:Tfile; cd:TconnData;
else
begin
if actualCount >= length(dir) then
setLength(dir, actualCount+100);
begin
setLength(dir, actualCount+1000);
if actualCount > 0 then
mainfrm.setStatusBarText(format('Listing files: %s',[dotted(actualCount)]));
end;
dir[actualCount]:=f;
inc(actualCount);
end;
Expand Down Expand Up @@ -1719,8 +1721,7 @@ function TfileListing.fromFolder(folder:Tfile; cd:TconnData;
sr: TSearchRec;
n: Ttreenode;
begin
{ this folder has been dinamically generated, thus the node is not actually
{ its own... skip }
// this folder has been dinamically generated, thus the node is not actually its own... skip
if folder.isTemp() then exit;

// include (valid) items from the VFS branch
Expand Down Expand Up @@ -1785,7 +1786,8 @@ function TfileListing.fromFolder(folder:Tfile; cd:TconnData;

begin
result:=toSkip;
if doClear then dir:=NIL;
if doClear then
actualCount:=0;

if not folder.isFolder()
or not folder.accessFor(cd)
Expand All @@ -1803,7 +1805,6 @@ function TfileListing.fromFolder(folder:Tfile; cd:TconnData;
toSkip:=max(0, pred(strToIntDef(par('page'), 1))*limit);
end;

actualCount:=length(dir);
folder.getFiltersRecursively(filesFilter, foldersFilter);
if assigned(cd) and not ignoreConnFilter then
begin
Expand All @@ -1827,7 +1828,10 @@ function TfileListing.fromFolder(folder:Tfile; cd:TconnData;
if folder.isRealFolder() and not (FA_HIDDENTREE in folder.flags) and allowedTo(folder) then
includeFilesFromDisk();
includeItemsFromVFS();
finally setLength(dir, actualCount) end;
finally
if doClear then
setLength(dir, actualCount)
end;
result:=toSkip;
end; // fromFolder

Expand Down Expand Up @@ -5102,7 +5106,7 @@ procedure Tmainfrm.httpEvent(event:ThttpEvent; conn:ThttpConn);

antiDos:=TantiDos.create;
try
tar:=TtarStream.create(); // this is freed by ThttpSrv
tar:=TtarStream.create();
try
tar.fileNamesOEM:=oemTarChk.checked;
addSelection();
Expand All @@ -5123,7 +5127,7 @@ procedure Tmainfrm.httpEvent(event:ThttpEvent; conn:ThttpConn);
conn.reply.mode:=HRM_REPLY;
conn.reply.contentType:=DEFAULT_MIME;
conn.reply.bodyMode:=RBM_STREAM;
conn.reply.bodyStream:=tar;
conn.reply.bodyStream:=tar; // it will be freed by ThttpSrv

if f.name = '' then exit; // can this really happen?
data.lastFN:=if_(f.name='/', 'home', f.name)
Expand Down

0 comments on commit 6c5139d

Please sign in to comment.