Recent

Author Topic: To optimize RTL, make function PCharToInt  (Read 37124 times)

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: To optimize RTL, make function PCharToInt
« Reply #15 on: July 23, 2021, 05:37:01 pm »
Length should always be respected instead of being lazy and use Copy with MaxInt.
Note that recent FPCs allow you to use Copy() with only two parameters when you want the copy to be from Index to Length inclusive -- so no Maxint is needed.

Thank you. This is a step in the correct direction. I mentioned this example as one of my bad habits.

BeniBela

  • Hero Member
  • *****
  • Posts: 905
    • homepage
Re: To optimize RTL, make function PCharToInt
« Reply #16 on: July 23, 2021, 05:38:41 pm »
Yes, and if length were always respected we would not have all these buffer underruns in C. As I said: "if done properly"... "When things can go wrong they will go wrong." Or: "Never change a winning team".

I did a lot of these micro-optimizations myself with arguments like "it looks better when I do it this way", "ah, this is a bit faster" etc., and I almost always ended up in introducing a bug somewhere. This is different from fixing a bug. A bug MUST be fixed, micro-optimizations gains nothing.


That is the problem with pchar

It is very unsafe, and easy to accidentally use a wrong length


But with a sub string type, it would store the length together with the pchar, so you cannot use the wrong length. The example

In fpreport.pp: SameText(Copy(BaseName,1,9),'TFPReport')

would be changed

In fpreport.pp: SameText(CopyAsSubStringView(BaseName,1,9),'TFPReport')

and is faster, and still safe.

Or with type helpers, one could do:

In fpreport.pp: BaseName.SubStringView(1,9).SameText('TFPReport')

It is safe, faster and looks better



If a function is not used in your program then it will be smartlinked out, especially if you did indeed enable the smartlink options. If it is not left out then it is indeed used either directly or indirectly.


That did not work at all

Looks like -gl disables smartlinking

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: To optimize RTL, make function PCharToInt
« Reply #17 on: July 23, 2021, 05:42:11 pm »
Dealing with strings, pchar is the natural type to use, while String type is more of a user-friendly type to hide address, length, codepage and reference count.


Hi!

The natural Pascal type is string, not pchar. Turbo Pascal lived years without pchar. I think pchar was first used in version 7 but only with extended syntax switch on.  pchar is a concession to the Operating systems but not  original Pascal syntax.

Winni

Hi. Yes, I meant natural in the sense when you need to deal with individual characters and at a closer level to the CPU, pchar is the type to use. Glad you pointed this out, it shows when pchar was perceived as a needed type.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: To optimize RTL, make function PCharToInt
« Reply #18 on: July 23, 2021, 05:45:14 pm »
@BeniBela

+1 for Sub String Type

The header preceding String type is more of a burden. At least a new type that can be separate from the String itself is needed.
« Last Edit: July 23, 2021, 05:53:55 pm by engkin »

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: To optimize RTL, make function PCharToInt
« Reply #19 on: July 23, 2021, 06:03:59 pm »
Hi!

Depends what you want:

If you want Pascal then use the string.

If you want speed then use pchar which has nothing to do with Pascal but with the OS. And if you want speed then use Assembler but not Pascal.

Winni

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: To optimize RTL, make function PCharToInt
« Reply #20 on: July 23, 2021, 06:16:24 pm »
You make it sound like you choose one and only one, with no other possibility. It sounds more like a gang membership, it is not. You can have Pascal, String, and speed through pchar or sub string.

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: To optimize RTL, make function PCharToInt
« Reply #21 on: July 23, 2021, 06:29:15 pm »
I think pchar was first used in version 7 but only with extended syntax switch on.  pchar is a concession to the Operating systems but not  original Pascal syntax.

[...] Glad you pointed this out, it shows when pchar was perceived as a needed type.

Historical disgression:
It was in fact introduced as a predeclared type in Turbo Pascal for Windows 1.0, which extended TP6.0, though IIRC the (very limited) compatibility rules enabled through Extended Syntax were introduced in TP7. Almost-full compatibility (allowing you to e.g. typecast a String as a PChar) were not added until Delphi 2 or 3 (don'r rememeber which). So it was indeed the drive toward Windows (and Protected-mode) programming which made the addition necessary.
 8-)
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: To optimize RTL, make function PCharToInt
« Reply #22 on: July 23, 2021, 06:42:11 pm »
Thank you. I am happy they gave us pchar, and also extending strings beyond 255-char length, not to forget adding code pages, and later breaking out of ANSI codepage that used to limit us to only two languages, or more precisely one additional language beside English.

Should this progress stop?

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: To optimize RTL, make function PCharToInt
« Reply #23 on: July 23, 2021, 07:25:33 pm »
@lucamar

Delphi1 introduced strpas and strPcopy to convert  string <--> pchar

@engkin

Don't struggle so useless. The introduction of the AnsiString and the UTF8 codepage has nothing to do with the pchar.

The pchar is just an awful bridge to connect to the OS.

Winni

lucamar

  • Hero Member
  • *****
  • Posts: 4219
Re: To optimize RTL, make function PCharToInt
« Reply #24 on: July 23, 2021, 08:14:59 pm »
Delphi1 introduced strpas and strPcopy to convert  string <--> pchar

Not quite: those two (among others) were already in TP7, in the Strings unit ;D
Turbo Pascal 3 CP/M - Amstrad PCW 8256 (512 KB !!!) :P
Lazarus/FPC 2.0.8/3.0.4 & 2.0.12/3.2.0 - 32/64 bits on:
(K|L|X)Ubuntu 12..18, Windows XP, 7, 10 and various DOSes.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: To optimize RTL, make function PCharToInt
« Reply #25 on: July 23, 2021, 08:33:50 pm »
Delphi1 introduced strpas and strPcopy to convert  string <--> pchar

Not quite: those two (among others) were already in TP7, in the Strings unit ;D

Thanx, lucamar!

That was the time when I (and a lot other people) used TP 5.5, TP 7.0, TP 1.0/1.5 for Windows (cruel!!) and Delphi1 parallel. So the reminder is not quiet sharp.

That stopped with 32 bit and Delphi 2,3,...

But the simple implicit cast of
Code: Pascal  [Select][+][-]
  1.  
  2.  
  3. String := pchar;

came some versions later.

Winni


engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: To optimize RTL, make function PCharToInt
« Reply #26 on: July 23, 2021, 08:55:17 pm »
Don't struggle so useless.

How is this supposed to help?

The introduction of the AnsiString and the UTF8 codepage has nothing to do with the pchar.

Ok, let's forget pchar for a second. Can I assume you have no problem with another string type like SubString?

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: To optimize RTL, make function PCharToInt
« Reply #27 on: July 23, 2021, 09:08:11 pm »


Ok, let's forget pchar for a second. Can I assume you have no problem with another string type like SubString?

What  you call a "SubString" s nothing but an Alias.
A collegue told me that is a great source of confusion in C++

So: When you like to increase the Confusion in Pascal:  Why not?

I go allways the opposite direction:  Keep It Simple, Stupid: KISS

Winni


engkin

  • Hero Member
  • *****
  • Posts: 3112
Re: To optimize RTL, make function PCharToInt
« Reply #28 on: July 23, 2021, 09:16:26 pm »
This is not helping. The argument now is based on what your collegue is telling you. It does not leave a way for me to accept your argument, nor it leaves a way to convince you of anything your colleague does not like.

winni

  • Hero Member
  • *****
  • Posts: 3197
Re: To optimize RTL, make function PCharToInt
« Reply #29 on: July 23, 2021, 09:29:57 pm »
It is so obvious:

Whatever I tell you - you try to get out of the trap of logic.

So keep on going, muddle Pascal with pchar, "SubStrings" or whatever you like.
But you can count me out.

Winni

 

TinyPortal © 2005-2018