Forum > FPC development

[CLOSED] Improvements in rtl/objpas/sysutils/fina.inc

(1/2) > >>

lagprogramming:
rtl/objpas/sysutils/fina.inc has the folowing routines:
function ChangeFileExt(const FileName, Extension: PathStr): PathStr;
function ExtractFilePath(const FileName: PathStr): PathStr;
function ExtractFileDir(const FileName: PathStr): PathStr;
function ExtractFileName(const FileName: PathStr): PathStr;
function ExtractFileExt(const FileName:PathStr):PathStr;

Switching the "I" and "EndSep" assignment lines makes the compiler produce better code. You can verify by looking at the generated assembly file.
I'm talking about the lines like:

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---  I := Length(FileName);  EndSep:=AllowDirectorySeparators+AllowDriveSeparators; 
The following patch modifies all the above functions.

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---diff --git a/rtl/objpas/sysutils/fina.inc b/rtl/objpas/sysutils/fina.incindex e817d60e6a..95217a026b 100644--- a/rtl/objpas/sysutils/fina.inc+++ b/rtl/objpas/sysutils/fina.inc@@ -21,8 +21,8 @@ function ChangeFileExt(const FileName, Extension: PathStr): PathStr;   SOF : Boolean;    begin-  i := Length(FileName);   EndSep:=AllowDirectorySeparators+AllowDriveSeparators+[ExtensionSeparator];+  i := Length(FileName);   while (I > 0) and not(FileName[I] in EndSep) do     Dec(I);   if (I = 0) or (FileName[I] <> ExtensionSeparator) then@@ -41,8 +41,8 @@ function ExtractFilePath(const FileName: PathStr): PathStr;   i : longint;   EndSep : Set of Char; begin-  i := Length(FileName);   EndSep:=AllowDirectorySeparators+AllowDriveSeparators;+  i := Length(FileName);   while (i > 0) and not CharInSet(FileName[i],EndSep) do     Dec(i);   If I>0 then@@ -56,8 +56,8 @@ function ExtractFileDir(const FileName: PathStr): PathStr;   i : longint;   EndSep : Set of Char; begin-  I := Length(FileName);   EndSep:=AllowDirectorySeparators+AllowDriveSeparators;+  I := Length(FileName);   while (I > 0) and not CharInSet(FileName[I],EndSep) do     Dec(I);   if (I > 1) and CharInSet(FileName[I],AllowDirectorySeparators) and@@ -102,8 +102,8 @@ function ExtractFileName(const FileName: PathStr): PathStr;   i : longint;   EndSep : Set of Char; begin-  I := Length(FileName);   EndSep:=AllowDirectorySeparators+AllowDriveSeparators;+  I := Length(FileName);   while (I > 0) and not CharInSet(FileName[I],EndSep) do     Dec(I);   Result := Copy(FileName, I + 1, MaxInt);@@ -117,8 +117,8 @@ function ExtractFileExt(const FileName: PathStr): PathStr;    begin   Result:='';-  I := Length(FileName);   EndSep:=AllowDirectorySeparators+AllowDriveSeparators+[ExtensionSeparator];+  I := Length(FileName);   while (I > 0) and not CharInSet(FileName[I],EndSep) do     Dec(I);   if (I > 0) and (FileName[I] = ExtensionSeparator) then

Thaddy:
Why would you slow down the code with this patch?

marcov:
The window that the variable is live shortens, increasing the chances that it can be registered. Perhaps the compiler should be able to do this automatically though :-)

lagprogramming:
AllowDirectorySeparators, AllowDriveSeparators and ExtensionSeparator are all constants, but I couldn't turn EndSep from a variable to a constant. Turning EndSep variables into constants will make the patch obsolete.
I mean I've tried adding

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---const  EndSep : Set of Char = AllowDirectorySeparators+AllowDriveSeparators+[ExtensionSeparator];in order to help the compiler produce better code but the compiler refused to build the modified file.
It might be something trivial to make the compiler compute the EndSep values at compile time, I don't know, or maybe it should already compute the EndSep values at compile time and there is a bug.  :-\

PascalDragon:

--- Quote from: lagprogramming on May 25, 2023, 04:33:07 pm ---AllowDirectorySeparators, AllowDriveSeparators and ExtensionSeparator are all constants, but I couldn't turn EndSep from a variable to a constant. Turning EndSep variables into constants will make the patch obsolete.
I mean I've tried adding

--- Code: Pascal  [+][-]window.onload = function(){var x1 = document.getElementById("main_content_section"); if (x1) { var x = document.getElementsByClassName("geshi");for (var i = 0; i < x.length; i++) { x[i].style.maxHeight='none'; x[i].style.height = Math.min(x[i].clientHeight+15,306)+'px'; x[i].style.resize = "vertical";}};} ---const  EndSep : Set of Char = AllowDirectorySeparators+AllowDriveSeparators+[ExtensionSeparator];in order to help the compiler produce better code but the compiler refused to build the modified file.
It might be something trivial to make the compiler compute the EndSep values at compile time, I don't know, or maybe it should already compute the EndSep values at compile time and there is a bug.  :-\

--- End quote ---

AllowDirectorySeperators and AllowDriveSeparators are typed constants. Typed constants can not be used as an initializer for constants (typed or untyped) or variables, thus you simply can't declare a EndSep constant.

Navigation

[0] Message Index

[#] Next page

Go to full version