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

gh-124502: Optimize unicode_eq() #125105

Merged
merged 3 commits into from
Oct 8, 2024
Merged

Conversation

vstinner
Copy link
Member

@vstinner vstinner commented Oct 8, 2024

  • Cleanup unicode_compare_eq() code.
  • Copy unicode_compare_eq() code in unicode_eq(): the two functions are now identical.

* Cleanup unicode_compare_eq() code.
* Copy unicode_compare_eq() code in unicode_eq(): the two functions
  are now identical.
@vstinner
Copy link
Member Author

vstinner commented Oct 8, 2024

Microbenchmark:

import pyperf
import _testcapi

str1 = "a" * 10
str2 = str1.encode().decode()
if str2 is str1:
    raise Exception

runner = pyperf.Runner()
runner.bench_time_func('_PyUnicode_EQ', _testcapi.bench_eq, str1, str2)
runner.bench_time_func('_PyUnicode_Equal', _testcapi.bench__equal, str1, str2)

Result on Python built with PGO+LTO (without CPU isolation):

_PyUnicode_EQ: Mean +- std dev: 7.45 ns +- 0.02 ns
_PyUnicode_Equal: Mean +- std dev: 7.44 ns +- 0.02 ns

The performance is the same.

@vstinner
Copy link
Member Author

vstinner commented Oct 8, 2024

cc @serhiy-storchaka

Objects/stringlib/eq.h Outdated Show resolved Hide resolved
Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

Why change unicode_compare_eq()?

@vstinner
Copy link
Member Author

vstinner commented Oct 8, 2024

For comparison, results on the main branch, also with PGO+LTO:

_PyUnicode_EQ: Mean +- std dev: 8.52 ns +- 0.02 ns
_PyUnicode_Equal: Mean +- std dev: 7.45 ns +- 0.02 ns

@vstinner
Copy link
Member Author

vstinner commented Oct 8, 2024

Why change unicode_compare_eq()?

I like to reduce the scope of variables by only declaring them where they are first assigned.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

If this PR is only about unique_eq(), change only it. Otherwise simply use unique_eq() instead of unique_compare_eq(). There are no reasons to have two identical static functions with different names.

@vstinner
Copy link
Member Author

vstinner commented Oct 8, 2024

There are no reasons to have two identical static functions with different names.

Once this PR will be merged, I plan to write a following PR to remove unicode_compare_eq() and _PyUnicode_EQ().

I limited this PR to optimizing unicode_eq() to be able to measure the speedup. Otherwise, it was too difficult to measure it.

@serhiy-storchaka
Copy link
Member

If you plan to remove unicode_compare_eq(), do not make cosmetic changes to it.

s/unicode_compare_eq/unicode_eq/

@vstinner
Copy link
Member Author

vstinner commented Oct 8, 2024

If you plan to remove unicode_compare_eq(), do not make cosmetic changes to it.

As you wish: I reverted unicode_compare_eq() changes.

Copy link
Member

@serhiy-storchaka serhiy-storchaka left a comment

Choose a reason for hiding this comment

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

LGTM. Although I think that #125070 was clearer, because it shown the larger picture.

@vstinner vstinner merged commit c203955 into python:main Oct 8, 2024
37 checks passed
@vstinner vstinner deleted the optimize_unicode_eq branch October 8, 2024 14:25
@vstinner
Copy link
Member Author

vstinner commented Oct 8, 2024

Merged, thanks for reviews.

Follow-up PR: #125114 removes _PyUnicode_EQ().

efimov-mikhail pushed a commit to efimov-mikhail/cpython that referenced this pull request Oct 9, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

3 participants