Recent

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

MathMan

  • Full Member
  • ***
  • Posts: 238
Re: To optimize RTL, make function PCharToInt
« Reply #75 on: August 14, 2021, 02:24:20 pm »

[snip]

Although I had a bug that it only used the 8-digit fast path when there where at least 9 digits. The condition in the loop should have been while pstart + 8 <= pend do begin rather than while pstart + 8 < pend do begin. Changing that made it somewhat faster. Or sometimes slower. It is very fiddly to benchmark. The branch predicator appears to be very random

Point is - if you read in the ASCII digits from memory, then you have to differentiate between little & big endian CPU in the above!

And the code got exactly the wrong endianess. Everything comes out in reverse.

I had to put in a SwapEndian but that call slow  it down too much. FPC not inlining such functions is another big RTL problem.

Inline assembler bswap is even worse because it apparently prevents optimizations.


However - if you really want to dive into implementing a faster 'Val' then maybe you want to take a look at this - https://lemire.me/blog/2021/01/29/number-parsing-at-a-gigabyte-per-second/

That is for float. Integer should always be faster than float.

I have already convinced Bero to implement that.  But I am not sure he got everything right.

Thanks for trying - you gave me some food for thought already. Things I can already state are

* you don't need the SwapEndian - better turn around the muls and shifts
* yes, the paper from Lemire is about float to binary, but if you read careful then Uint to binary is a sub-component of that - iirc
* I also think that the benchmark can be improved - for my taste it has to little variance in sizes of the input strings

Give me a couple of days to play with some ideas.

I'll leave the naming thing unanswered - I am absurdly bad at naming.

Cheers,
MathMan

440bx

  • Hero Member
  • *****
  • Posts: 2504
Re: To optimize RTL, make function PCharToInt
« Reply #76 on: August 14, 2021, 10:19:36 pm »
I am absurdly bad at naming.
In that case, I hope your wife named the kids. ;)
FPC v3.0.4 and Lazarus 1.8.2 on Windows 7 64bit.

Alextp

  • Hero Member
  • *****
  • Posts: 1417
    • UVviewsoft
Re: To optimize RTL, make function PCharToInt
« Reply #77 on: August 14, 2021, 10:27:36 pm »
Names:
Now we have

  StrToInt
  StrToInt64
  StrToInt64Def
  StrToIntDef
  TryStrToInt
  TryStrToInt64

I suggest the word "Buffer" for 1-byte strings, and BufferW for widechar strings.

  BufferToInt
  BufferToInt64
  BufferToInt64Def
  BufferToIntDef
  TryBufferToInt
  TryBufferToInt64

MathMan

  • Full Member
  • ***
  • Posts: 238
Re: To optimize RTL, make function PCharToInt
« Reply #78 on: August 15, 2021, 10:53:12 am »
I am absurdly bad at naming.
In that case, I hope your wife named the kids. ;)

She did - though she didn't have a chance to do at the same time  ;)

 

TinyPortal © 2005-2018