Skip to content

fix: release pyclass dict during deallocation#6198

Open
ImFeH2 wants to merge 3 commits into
PyO3:mainfrom
ImFeH2:issue-5953
Open

fix: release pyclass dict during deallocation#6198
ImFeH2 wants to merge 3 commits into
PyO3:mainfrom
ImFeH2:issue-5953

Conversation

@ImFeH2

@ImFeH2 ImFeH2 commented Jul 11, 2026

Copy link
Copy Markdown

Fixes #5953.

PyClassDictSlot::clear_dict previously called PyDict_Clear, which removed the contents of __dict__ but did not release the pyclass instance's reference to the dictionary itself. This leaked the empty dictionary when the instance was deallocated.

Use Py_CLEAR to clear the slot and decrement the dictionary's reference count.

@codspeed-hq

codspeed-hq Bot commented Jul 11, 2026

Copy link
Copy Markdown

Merging this PR will degrade performance by 24.72%

⚠️ Different runtime environments detected

Some benchmarks with significant performance changes were compared across different runtime environments,
which may affect the accuracy of the results.

Open the report in CodSpeed to investigate

❌ 1 regressed benchmark
✅ 139 untouched benchmarks

Warning

Please fix the performance issues or acknowledge them on CodSpeed.

Performance Changes

Benchmark BASE HEAD Efficiency
into_u128_max 1.5 µs 2 µs -24.72%

Tip

Investigate this regression by commenting @codspeedbot fix this regression on this PR, or directly use the CodSpeed MCP with your agent.


Comparing ImFeH2:issue-5953 (811d35c) with main (8fcf8fc)

Open in CodSpeed

@ImFeH2

ImFeH2 commented Jul 12, 2026

Copy link
Copy Markdown
Author

The reported into_u128_max regression appears unrelated to this change, which only affects #[pyclass(dict)] deallocation. CodSpeed also reports that the comparison used different runtime environments.

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.

Memory leak on dict pyclasses after setting dynamic attribute

1 participant