Thanks to visit codestin.com
Credit goes to github.com

Skip to content

gh-122561: Clean up and microoptimize str.translate and charmap codec #122932

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

Merged
merged 3 commits into from
Aug 28, 2024

Conversation

serhiy-storchaka
Copy link
Member

@serhiy-storchaka serhiy-storchaka commented Aug 12, 2024

  • Replace PyLong_AS_LONG() with PyLong_AsLong().
  • Call PyLong_AsLong() only once per the replacement code.
  • Use PyMapping_GetOptionalItem() instead of PyObject_GetItem().

… codec

* Replace PyLong_AS_LONG() with PyLong_AsLong().
* Call PyLong_AsLong() only once per the replacement code.
* Use PyMapping_GetOptionalItem() instead of PyObject_GetItem().
@@ -8935,15 +8946,20 @@ unicode_translate_call_errorhandler(const char *errors,
which must be decrefed by the caller.
Return 0 on success, -1 on error */
static int
charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result)
charmaptranslate_lookup(Py_UCS4 c, PyObject *mapping, PyObject **result, Py_UCS4 *replace)
Copy link
Member

Choose a reason for hiding this comment

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

Can you explain in the comment when replace is set?

Copy link
Member

@vstinner vstinner left a comment

Choose a reason for hiding this comment

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

LGTM

@@ -8594,7 +8606,7 @@ charmapencode_output(Py_UCS4 c, PyObject *mapping,
return enc_SUCCESS;
}

rep = charmapencode_lookup(c, mapping);
rep = charmapencode_lookup(c, mapping, &replace);
Copy link
Member

Choose a reason for hiding this comment

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

You can move unsigned char replace; declaration here, to reduce the scope of the variable.

Copy link
Member Author

Choose a reason for hiding this comment

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

I added it next to the declaration of rep, as in all other cases.

@serhiy-storchaka serhiy-storchaka merged commit 1a0b828 into python:main Aug 28, 2024
37 checks passed
@serhiy-storchaka serhiy-storchaka deleted the unicode-translate branch August 28, 2024 09:11
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.

2 participants