Skip to content

Conversation

@oremanj
Copy link
Collaborator

@oremanj oremanj commented Oct 11, 2025

Description

During development of #5800, I noticed that the native enum caster unnecessarily heap-allocates the fallback base caster if it needs to handle a load of a type that was exposed using the pre-v3 py::enum_ rather than py::native_enum. Fix this.

Since enum_ is a wrapper around class_, a Python instance of a type exposed with enum_ contains a [pointer to a] C++ enum object of appropriate type. It is therefore unnecessary to keep the type caster alive in order to preserve the referent of casts to const EnumType& or EnumType*. The heap allocation previously used by the native enum caster was incurring a performance cost on loads without providing any reliability or clarity benefits.

Suggested changelog entry:

Improve the performance of from-Python conversions of legacy pybind11 enum objects bound by py::enum_.

Copy link
Collaborator

@rwgk rwgk left a comment

Choose a reason for hiding this comment

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

Reviewed together offline.

@rwgk rwgk merged commit 1cf0948 into pybind:master Oct 11, 2025
85 checks passed
@github-actions github-actions bot added the needs changelog Possibly needs a changelog entry label Oct 11, 2025
@oremanj oremanj deleted the native-enum-faster branch October 14, 2025 00:01
@rwgk rwgk removed the needs changelog Possibly needs a changelog entry label Jan 21, 2026
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

2 participants