-
Notifications
You must be signed in to change notification settings - Fork 2.2k
[BUG]: string_caster should not use temporary on Python >= 3.3 #3252
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
Labels
Comments
@jbms PR on this front is welcome, would you mind submitting one? |
jbms
added a commit
to jbms/pybind11
that referenced
this issue
Sep 9, 2021
@Skylion007 Done. |
jbms
added a commit
to jbms/pybind11
that referenced
this issue
Sep 9, 2021
jbms
added a commit
to jbms/pybind11
that referenced
this issue
Sep 9, 2021
jbms
added a commit
to jbms/pybind11
that referenced
this issue
Sep 9, 2021
jbms
added a commit
to jbms/pybind11
that referenced
this issue
Sep 9, 2021
Skylion007
pushed a commit
that referenced
this issue
Sep 9, 2021
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Required prerequisites
Problem description
Currently,
string_caster
always creates a temporary PyBytes object by callingPyUnicode_AsEncodedString
. However, in the common case ofUTF_N == 8
, on Python >= 3.3 we can instead usePyUnicode_AsUTF8AndSize
, which manages and caches the UTF-8 encoding internally within the PyUnicode object. If the UTF-8 representation is cached, then the encoding does not have to be done at all, avoiding an extra copy of the string.This is particularly advantageous in the
IsView == true
case: users likely expect casting from PyUnicode tostd::string_view
to be low cost, but currently it always involves a copy of the string. Additionally, it theIsView == true
case has the additional cost of relying onloader_life_support::add_patient
, which introduces additional cost and additional memory allocations (e.g. with the change in #3237, an allocation of the unordered_set bucket array on first use of loader_life_support, and an additional allocation of the node).Reproducible example code
No response
The text was updated successfully, but these errors were encountered: