Skip to content

Conversation

@Ameen-Alam
Copy link

Optimize unicode string comparison with early length check

Add a fast-path optimization to unicode_compare() that checks string lengths
before performing character-by-character comparison. When strings have
different lengths, we can return immediately since they cannot be equal.
This improves performance for the common case where strings of different
lengths are compared.

The change:

  • Moves length checks to the start of the function
  • Returns early if lengths differ
  • Maintains existing comparison behavior
  • Follows CPython coding style

@python-cla-bot
Copy link

python-cla-bot bot commented May 9, 2025

The following commit authors need to sign the Contributor License Agreement:

CLA signed

@bedevere-app
Copy link

bedevere-app bot commented May 9, 2025

Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool.

If this change has little impact on Python users, wait for a maintainer to apply the skip news label instead.

@StanFromIreland
Copy link
Member

Performance benchmarks?

@picnixz
Copy link
Member

picnixz commented May 9, 2025

The change is also incorrect, as highlighted by the test failures. Please open an issue with results showing the improvements and the correctness.

@picnixz picnixz closed this May 9, 2025
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