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

perf: Add object rvalue overload for accessors. Enables reference stealing #3970

Merged
Merged
Changes from 4 commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 17 additions & 2 deletions include/pybind11/pytypes.h
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,10 @@ class object_api : public pyobject_tag {
or `object` subclass causes a call to ``__setitem__``.
\endrst */
item_accessor operator[](handle key) const;
/// See above (the only difference is that they key is provided as a string literal)
/// See above (the only difference is that the key is provided as a string literal)
item_accessor operator[](const char *key) const;
/// See above (the only difference is that the key's reference is stolen)
item_accessor operator[](object &&key) const;

/** \rst
Return an internal functor to access the object's attributes. Casting the
Expand All @@ -95,8 +97,10 @@ class object_api : public pyobject_tag {
or `object` subclass causes a call to ``setattr``.
\endrst */
obj_attr_accessor attr(handle key) const;
/// See above (the only difference is that they key is provided as a string literal)
/// See above (the only difference is that the key is provided as a string literal)
str_attr_accessor attr(const char *key) const;
/// See above (the only difference is that the key's reference is stolen)
obj_attr_accessor attr(object &&key) const;

/** \rst
Matches * unpacking in Python, e.g. to unpack arguments out of a ``tuple``
Expand Down Expand Up @@ -1683,6 +1687,7 @@ class tuple : public object {
bool empty() const { return size() == 0; }
detail::tuple_accessor operator[](size_t index) const { return {*this, index}; }
detail::item_accessor operator[](handle h) const { return object::operator[](h); }
detail::item_accessor operator[](object &&o) const { return object::operator[](std::move(o)); }
detail::tuple_iterator begin() const { return {*this, 0}; }
detail::tuple_iterator end() const { return {*this, PyTuple_GET_SIZE(m_ptr)}; }
};
Expand Down Expand Up @@ -1743,6 +1748,7 @@ class sequence : public object {
bool empty() const { return size() == 0; }
detail::sequence_accessor operator[](size_t index) const { return {*this, index}; }
detail::item_accessor operator[](handle h) const { return object::operator[](h); }
detail::item_accessor operator[](object &&o) const { return object::operator[](std::move(o)); }
detail::sequence_iterator begin() const { return {*this, 0}; }
detail::sequence_iterator end() const { return {*this, PySequence_Size(m_ptr)}; }
};
Expand All @@ -1762,6 +1768,7 @@ class list : public object {
bool empty() const { return size() == 0; }
detail::list_accessor operator[](size_t index) const { return {*this, index}; }
detail::item_accessor operator[](handle h) const { return object::operator[](h); }
detail::item_accessor operator[](object &&o) const { return object::operator[](std::move(o)); }
detail::list_iterator begin() const { return {*this, 0}; }
detail::list_iterator end() const { return {*this, PyList_GET_SIZE(m_ptr)}; }
template <typename T>
Expand Down Expand Up @@ -2057,6 +2064,10 @@ iterator object_api<D>::end() const {
return iterator::sentinel();
}
template <typename D>
item_accessor object_api<D>::operator[](object &&key) const {
Skylion007 marked this conversation as resolved.
Show resolved Hide resolved
Copy link
Collaborator

Choose a reason for hiding this comment

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

The declaration appears after the overload for handle. Could you please put them in the same order here?

return {derived(), std::move(key)};
}
template <typename D>
item_accessor object_api<D>::operator[](handle key) const {
return {derived(), reinterpret_borrow<object>(key)};
}
Expand All @@ -2065,6 +2076,10 @@ item_accessor object_api<D>::operator[](const char *key) const {
return {derived(), pybind11::str(key)};
}
template <typename D>
obj_attr_accessor object_api<D>::attr(object &&key) const {
Copy link
Collaborator

Choose a reason for hiding this comment

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

Please keep the order of declarations here, too.

return {derived(), std::move(key)};
}
template <typename D>
obj_attr_accessor object_api<D>::attr(handle key) const {
return {derived(), reinterpret_borrow<object>(key)};
}
Expand Down