pybind / pybind/pybind11

[BUG]: Time for `name` property on enum grows with enum size

Open
#5,031 0 comments 1 reaction 0 assignees View on GitHub

Nobody has claimed this yet.

triage
Dominant language
C++
Stars
18k
Forks
2.3k
Avg merge
5d 17h
Merged PRs (30d)
10

Description

Required prerequisites
What version (or hash if on master) of pybind11 are you using?

>=2.10.0, same as pybind/python_example

Problem description

The Enum.name property implemented in #1345 has an unexpected performance characteristic.
Namely the larger the enum the longer it takes.

I believe this function gets called https://github.com/pybind/pybind11/blob/8b48ff878c168b51fe5ef7b8c728815b9e1a9857/include/pybind11/pybind11.h#L1988 and it's on average O(n) in the size of the enum.

The example is synthetic, but I've run into this being a problem in a real code base.

Reproducible example code

I've created a reproducible example here: https://github.com/niteria/python_example/tree/linear-enum-name-repro

It just wraps 3 different sizes of enums and measures the time taken by enum.name.

https://github.com/niteria/python_example/commit/3b063004e24610b712bd64b3e752aeafc8732c55 is the only change on top of pybind/python_example.

To run:

~/tmp/python_example$ pip install .
~/tmp/python_example$ python test.py

Output on my computer:

$ python test.py 
Small time: 0.009021997451782227 seconds
Medium time: 0.31952929496765137 seconds
Large time: 3.216634511947632 seconds
Is this a regression? Put the last known working version here if it is.

Not a regression

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Read include/pybind11/pybind11.h around line 1988, then run the linked python_example reproduction with its small, medium, and large enums. Compare the timing of Enum.name across enum sizes and verify that the completed change removes the reported growth in lookup time.

Written by the indexing model from the issue text.

Assessment

Tech stack
cpp, python
Domain
backend-api-design, performance
Issue type
Bug
Difficulty
3/5
Estimated time
1-2 days
Activity status
Stale
Clarity
Mostly clear
Newbie friendliness
45/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.