KEMBAR78
gh-121562: optimized hex_from_char by LimaBD · Pull Request #121563 · python/cpython · GitHub
Skip to content

Conversation

@LimaBD
Copy link
Contributor

@LimaBD LimaBD commented Jul 10, 2024

Optimized hex_from_char performance

issue #121562

Timing with old hex_from_char:

$ ./python -m pyperf timeit --duplicate 100 "float.fromhex('0x123456.ffffp10')"
.....................
Mean +- std dev: 86.0 ns +- 0.9 ns

With new hex_from_char:

$ ./python -m pyperf timeit --duplicate 100 "float.fromhex('0x123456.ffffp10')"
.....................
Mean +- std dev: 81.8 ns +- 1.0 ns

If we measure hex_from_char directly, probably we will get a much better % improvement because there is additional calls that float.fromhex makes apart from this one.

Copy link
Member

@mdickinson mdickinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM in principle. The old code is agnostic to character encoding (works equally well with ASCII / EBCDIC / whatever), while the new code is based on the bytes of the UTF8-encoded string. That's fine, since the only use-case is applying this to a UTF-8-encoded value. But please could we add a comment that makes it clear that this table should only be used with encodings whose lower half is ASCII (like UTF-8).

@LimaBD
Copy link
Contributor Author

LimaBD commented Jul 10, 2024

Hello @mdickinson, thanks for your time, I just added the comment clarifying it but my English is average, anything let me know!

Copy link
Member

@mdickinson mdickinson left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for the update; the comment looks good.

@mdickinson mdickinson merged commit 04130b2 into python:main Jul 14, 2024
@mdickinson
Copy link
Member

@LimaBD Merged. Thank you for the contribution!

@LimaBD LimaBD deleted the hex-from-char-performance branch July 14, 2024 12:35
estyxx pushed a commit to estyxx/cpython that referenced this pull request Jul 17, 2024
Performance improvement to `float.fromhex`: use a lookup table
for computing the hexadecimal value of a character, in place of the
previous switch-case construct. Patch by Bruno Lima.
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants