Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Hexadecimal optimizations #223

Merged
merged 3 commits into from
May 20, 2022
Merged

Hexadecimal optimizations #223

merged 3 commits into from
May 20, 2022

Conversation

BoboTiG
Copy link
Contributor

@BoboTiG BoboTiG commented Apr 16, 2022

What was wrong?

Nothing wrong, simply trying to improve performances here, and there.

How was it fixed?

I used that script to naively benchmark changes for the 2nd commit:

import re
from timeit import timeit


def original(value, regex=re.compile("(0x)?[0-9a-f]*", re.IGNORECASE | re.ASCII)):
    return regex.fullmatch(value) is not None


def new(value, regex=re.compile("(0[xX])?[0-9a-fA-F]*")):
    return regex.fullmatch(value) is not None


print("original(True)", timeit(
    "original('0x5831e66b7de5df2d8d4705c86c844ff60a50efcec53f9449b16dc43216a783f7')", "from __main__ import original"))
print("new(True)", timeit(
    "new('0x5831e66b7de5df2d8d4705c86c844ff60a50efcec53f9449b16dc43216a783f7')", "from __main__ import new"))
print()
print("original(False)", timeit(
    "original('0x5831e66b7de5df2d8d4705c86c844ff60a50efcec53f9449b16dc43216a783f7g')", "from __main__ import original"))
print("new(False)", timeit(
    "new('0x5831e66b7de5df2d8d4705c86c844ff60a50efcec53f9449b16dc43216a783f7g')", "from __main__ import new"))

Improvements unlocked by the first commit:

  • When value has 0x prefix:

    • before: 0.1212 sec
    • after : 0.1132 sec (-6.6 %)
  • When value has 0X prefix:

    • before: 0.1976 sec
    • after : 0.1167 sec (-40.9 %)
  • When value has no 0x prefix:

    • before: 0.1918 sec
    • after : 0.1165 sec (-39.2 %)

Improvements unlocked by the 2nd commit:

  • When value is hexadecimal string (TX hash):

    • before: 1.0965 sec
    • after : 0.3652 sec (-66.7 %)
  • When value is not hexadecimal string (TX hash + 'g'):

    • before: 1.4546 sec
    • after : 0.7022 sec (-51.7 %)

To-Do

  • Clean up commit history

Cute Animal Picture

put a cute animal picture link inside the parentheses

Quick benchmarks:

- When `value` has 0x prefix:
  - before: 0.1212 sec
  - after : 0.1132 sec (-6.6 %)

- When `value` has 0X prefix:
  - before: 0.1976 sec
  - after : 0.1167 sec (-40.9 %)

- When `value` has no 0x prefix:
  - before: 0.1918 sec
  - after : 0.1165 sec (-39.2 %)
Quick benchmarks:

- When `value` is hexadecimal string (TX hash):
  - before: 1.0965 sec
  - after : 0.3652 sec (-66.7 %)

- When `value` is not hexadecimal string (TX hash + 'g'):
  - before: 1.4546 sec
  - after : 0.7022 sec (-51.7 %)
Copy link
Collaborator

@carver carver left a comment

Choose a reason for hiding this comment

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

Neat! Welcome to the repo 😄 -- excellent cute animal.

If you add a file at newsfragments/223.performance.rst with a one-liner explaining the change, then this change will get mentioned in the release notes with a link to this PR. Something like "Performance improvement of up to 65% on is_0x_prefixed"

@BoboTiG
Copy link
Contributor Author

BoboTiG commented May 20, 2022

I wasn't sure it deserved a news fragment. Here it is!

@carver carver merged commit db5d80a into ethereum:master May 20, 2022
@carver
Copy link
Collaborator

carver commented May 20, 2022

Thanks for the contribution!

@BoboTiG BoboTiG deleted the impr-several-minor-optimizations branch May 20, 2022 03:32
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