Title: set/frozenset re-hash elements when constructed from a set, frozenset, or dict · Issue #8489 · RustPython/RustPython · GitHub
Open Graph Title: set/frozenset re-hash elements when constructed from a set, frozenset, or dict · Issue #8489 · RustPython/RustPython
X Title: set/frozenset re-hash elements when constructed from a set, frozenset, or dict · Issue #8489 · RustPython/RustPython
Description: Summary set/frozenset recompute __hash__ for every element when the source is a set, frozenset, or dict. CPython reuses the hash already stored in the source's entries (set_update_internal in Objects/setobject.c, branching on PyAnySet_Ch...
Open Graph Description: Summary set/frozenset recompute __hash__ for every element when the source is a set, frozenset, or dict. CPython reuses the hash already stored in the source's entries (set_update_internal in Objec...
X Description: Summary set/frozenset recompute __hash__ for every element when the source is a set, frozenset, or dict. CPython reuses the hash already stored in the source's entries (set_update_internal in O...
Opengraph URL: https://github.com/RustPython/RustPython/issues/8489
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"set/frozenset re-hash elements when constructed from a set, frozenset, or dict","articleBody":"## Summary\n\n`set`/`frozenset` recompute `__hash__` for every element when the source is a `set`, `frozenset`, or `dict`. CPython reuses the hash already stored in the source's entries (`set_update_internal` in `Objects/setobject.c`, branching on `PyAnySet_Check` / `PyDict_CheckExact`), so no extra `__hash__` calls happen.\n\n## Scope\n\nThe set/frozenset target only. The `dict` target (`dict.fromkeys`) is #8490. `test_do_not_rehash_dict_keys` asserts on both directions, so its `@unittest.expectedFailure # TODO: RUSTPYTHON` marker can only be dropped once both are done.\n\n## Reproduction\n\n```python\nclass HashCountingInt(int):\n def __init__(self, *args):\n self.hash_count = 0\n def __hash__(self):\n self.hash_count += 1\n return int.__hash__(self)\n\nd = dict.fromkeys(map(HashCountingInt, range(10)))\nprint(sum(e.hash_count for e in d)) # 10\nfrozenset(d)\nprint(sum(e.hash_count for e in d)) # CPython: 10, RustPython: 20\n```\n\n`Lib/test/test_set.py:333` — `TestJointOps.test_do_not_rehash_dict_keys`, inherited by `TestSet`, `TestSetSubclass`, `TestFrozenSet` and `TestFrozenSetSubclass` (4 of the 10 expected failures in `test_set.py`).\n\n## Analysis\n\nThe hash is already stored — `DictEntry` keeps it next to the key (`crates/vm/src/dict_inner.rs:227`), and `PySetInner::update_internal` (`crates/vm/src/builtins/set.rs:379`) already special-cases `AnySet` and `PyDict`. But both branches drop it and go through `self.add()` → `content.insert()`, which re-hashes:\n\n```rust\nfn merge_set(&self, any_set: AnySet, vm: &VirtualMachine) -> PyResult\u003c()> {\n for item in any_set.as_inner().elements() {\n self.add(item, vm)?; // recomputes the hash\n }\n Ok(())\n}\n```\n\n`update_internal` is also only reached from `set.update()` (`:719`). These paths never dispatch on the source type at all:\n\n- `PySetInner::from_iter` (`:187`), used by the `set()` / `frozenset()` constructors\n- `union` (`:252`), `intersection` (`:261`), `difference` (`:272`), `symmetric_difference` (`:280`)\n- `intersection_update` (`:409`), `difference_update` (`:422`), `symmetric_difference_update` (`:436`)\n","author":{"url":"https://github.com/fregataa","@type":"Person","name":"fregataa"},"datePublished":"2026-08-10T16:49:30Z","url":"https://github.com/RustPython/RustPython/issues/8489"}
| react-profiling | 0 |
| react-import-map | react,react-dom,react-dom/client,react-dom/profiling,react-is,react/compiler-runtime,react/jsx-dev-runtime,react/jsx-runtime,scheduler@b8488ee2c322 |
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:11b3a79c-06ef-aee4-1452-fd6736e45fa6 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | A030:317608:3D915AD:4DED5D2:6B0206BC |
| html-safe-nonce | 06181a4c291a6081dd0b4edaa51718b340aac3f48b18c2dbb3f5bdff3f26b091 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJBMDMwOjMxNzYwODozRDkxNUFEOjRERUQ1RDI6NkIwMjA2QkMiLCJ2aXNpdG9yX2lkIjoiNzU2NDA5NzA1Njg5ODQxODM2NCIsInJlZ2lvbl9lZGdlIjoiaWFkIiwicmVnaW9uX3JlbmRlciI6ImlhZCJ9 |
| visitor-hmac | 3ff96f447671928142858c788a309e964d0a2de62b96234bd3e24eea98b344d4 |
| hovercard-subject-tag | issue:5112413997 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/RustPython/RustPython/8489/issue_layout |
| twitter:image | https://opengraph.githubassets.com/158efbf61676fa6cf2fd044ba43ef27cf2c3d1f76677d2be4bf98fa392971831/RustPython/RustPython/issues/8489 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/158efbf61676fa6cf2fd044ba43ef27cf2c3d1f76677d2be4bf98fa392971831/RustPython/RustPython/issues/8489 |
| og:image:alt | Summary set/frozenset recompute __hash__ for every element when the source is a set, frozenset, or dict. CPython reuses the hash already stored in the source's entries (set_update_internal in Objec... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | fregataa |
| hostname | github.com |
| expected-hostname | github.com |
| None | 87e774ee7c6b3bee0490394e4d3e57ec9dbc956f74b87ea685dc8996c1676456 |
| turbo-cache-control | no-preview |
| go-import | github.com/RustPython/RustPython git https://github.com/RustPython/RustPython.git |
| octolytics-dimension-user_id | 39710557 |
| octolytics-dimension-user_login | RustPython |
| octolytics-dimension-repository_id | 135201145 |
| octolytics-dimension-repository_nwo | RustPython/RustPython |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 135201145 |
| octolytics-dimension-repository_network_root_nwo | RustPython/RustPython |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | 423dbb54c5e7bfecc838eb387c1435eb3d0e760d |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
| react-app-name | issues |
Links:
Viewport: width=device-width