Bookstore

Recent

Author Topic: Is ParamStr(i) efficient?  (Read 583 times)

Alextp

  • Hero Member
  • *****
  • Posts: 945
    • UVviewsoft
Is ParamStr(i) efficient?
« on: January 30, 2020, 01:24:19 pm »
I saw the code of ParamStr(i) with i>0 and saw it was getting Argc/Argv values, but cannot find details where argv is made. So i ask. Does FPC parse Win32 command line to separate argv's each time I call ParamStr? Or does it parse it once, and saves ParamStr values to argv's?

Thaddy

  • Hero Member
  • *****
  • Posts: 9791
Re: Is ParamStr(i) efficient?
« Reply #1 on: January 30, 2020, 02:46:18 pm »
It is exactly just as (in)efficient as in C or C++ or any other language. There is not much to optimize...
I am more like donkey than shrek

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8071
Re: Is ParamStr(i) efficient?
« Reply #2 on: January 30, 2020, 02:49:12 pm »
I saw the code of ParamStr(i) with i>0 and saw it was getting Argc/Argv values, but cannot find details where argv is made.

rtl/win/syswin.inc line 220 and on.

Quote
So i ask. Does FPC parse Win32 command line to separate argv's each time I call ParamStr? Or does it parse it once, and saves ParamStr values to argv's?

Once.  It is part of an unified interface of the system unit to the commandline handling. Mostly everything operates on argv, argc or on fake ones (like Windows, where they are pascal symbols containing the split commandline).

It is exactly just as (in)efficient as in C or C++ or any other language. There is not much to optimize...

On Windows argv and argc Pascal symbols are totally unrelated to whatever C or C++ might or might not do.

Thaddy

  • Hero Member
  • *****
  • Posts: 9791
Re: Is ParamStr(i) efficient?
« Reply #3 on: January 30, 2020, 02:58:36 pm »
On Windows argv and argc Pascal symbols are totally unrelated to whatever C or C++ might or might not do.
No. They are - almost - exactly the same. By heritage. You are not often wrong but here you are.
(e.g. in Borland Pascal, Turbo Basic  and Borland C even exactly the same code)
« Last Edit: January 30, 2020, 03:00:33 pm by Thaddy »
I am more like donkey than shrek

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8071
Re: Is ParamStr(i) efficient?
« Reply #4 on: January 30, 2020, 03:34:41 pm »
On Windows argv and argc Pascal symbols are totally unrelated to whatever C or C++ might or might not do.
No. They are - almost - exactly the same. By heritage. You are not often wrong but here you are.
(e.g. in Borland Pascal, Turbo Basic  and Borland C even exactly the same code)

Afaik this comes from the parsing from go32v2 extender's argv and argc in ye old days, manually fixed for Windows when it came. Not from Borland*

But the point is that a Windows C compiler will use a different argv and argc, since they are not externals.

They also will be different on Linux (since FPC uses the startup versions, while C programs will use the libc versions, which are hopefully 1:1 tho)
« Last Edit: January 30, 2020, 03:37:04 pm by marcov »

Alextp

  • Hero Member
  • *****
  • Posts: 945
    • UVviewsoft
Re: Is ParamStr(i) efficient?
« Reply #5 on: January 30, 2020, 04:29:49 pm »
syswin.inc
Code: Pascal  [Select]
  1.     procedure allocarg(idx,len:longint);
  2.       var
  3.         oldargvlen : longint;
  4.       begin
  5.         if idx>=argvlen then
  6.          begin
  7.            oldargvlen:=argvlen;
  8.            argvlen:=(idx+8) and (not 7);
  9.            sysreallocmem(argv,argvlen*sizeof(pointer));
  10.            fillchar(argv[oldargvlen],(argvlen-oldargvlen)*sizeof(pointer),0);
  11.          end;
  12.         { use realloc to reuse already existing memory }
  13.         { always allocate, even if length is zero, since }
  14.         { the arg. is still present!                     }
  15.         sysreallocmem(argv[idx],len+1);
  16.       end;
  17.  
IMO here we can allocate by 10 elements (for ex), not by 1 element, when idx>=argvlen. this will make 10 times less reallocs.

marcov

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 8071
Re: Is ParamStr(i) efficient?
« Reply #6 on: January 30, 2020, 04:31:08 pm »

IMO here we can allocate by 10 elements (for ex), not by 1 element, when idx>=argvlen. this will make 10 times less reallocs.

Code executed once on startup. Is it really worth the extra bytes in every app?

skalogryz

  • Global Moderator
  • Hero Member
  • *****
  • Posts: 2393
    • havefunsoft.com
Re: Is ParamStr(i) efficient?
« Reply #7 on: January 30, 2020, 04:38:49 pm »
It is exactly just as (in)efficient as in C or C++ or any other language. There is not much to optimize...
for comparison, Delphi implementation does parameter lookup on every call to ParamStr()
Patron Cocoa Widgetset development https://www.patreon.com/skalogryz