Title: dict.fromkeys() re-hashes keys taken from a set, frozenset, or dict · Issue #8490 · RustPython/RustPython · GitHub
Open Graph Title: dict.fromkeys() re-hashes keys taken from a set, frozenset, or dict · Issue #8490 · RustPython/RustPython
X Title: dict.fromkeys() re-hashes keys taken from a set, frozenset, or dict · Issue #8490 · RustPython/RustPython
Description: Summary dict.fromkeys() recomputes __hash__ for every key when the iterable is a set, frozenset, or dict — all of which already store a hash per entry. CPython's dict_fromkeys_impl (Objects/dictobject.c) branches on PyAnySet_Check / PyDi...
Open Graph Description: Summary dict.fromkeys() recomputes __hash__ for every key when the iterable is a set, frozenset, or dict — all of which already store a hash per entry. CPython's dict_fromkeys_impl (Objects/dictobj...
X Description: Summary dict.fromkeys() recomputes __hash__ for every key when the iterable is a set, frozenset, or dict — all of which already store a hash per entry. CPython's dict_fromkeys_impl (Objects/dic...
Opengraph URL: https://github.com/RustPython/RustPython/issues/8490
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"dict.fromkeys() re-hashes keys taken from a set, frozenset, or dict","articleBody":"## Summary\n\n`dict.fromkeys()` recomputes `__hash__` for every key when the iterable is a `set`, `frozenset`, or `dict` — all of which already store a hash per entry. CPython's `dict_fromkeys_impl` (`Objects/dictobject.c`) branches on `PyAnySet_Check` / `PyDict_CheckExact` and feeds the hash from `_PySet_NextEntry` / `_PyDict_Next` straight into `insertdict`.\n\nThis is the dict-target counterpart of #8489 (set/frozenset target).\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\ndict.fromkeys(set(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` asserts on both directions, so its `@unittest.expectedFailure # TODO: RUSTPYTHON` marker can only be dropped once both this issue and #8489 are done. It is inherited by `TestSet`, `TestSetSubclass`, `TestFrozenSet` and `TestFrozenSetSubclass` (4 of the 10 expected failures in `test_set.py`).\n\n## Analysis\n\n`PyDict::fromkeys` (`crates/vm/src/builtins/dict.rs:377`) iterates generically regardless of what the source is:\n\n```rust\nmatch d.downcast_exact::\u003cSelf>(vm) {\n Ok(pydict) => {\n for key in iterable.iter(vm)? {\n pydict.__setitem__(key?, value.clone(), vm)?;\n }\n Ok(pydict.into_pyref().into())\n }\n ...\n}\n```\n\n`__setitem__` → `inner_setitem` (`:302`) → `entries.insert()` ends up calling `key.key_hash(vm)`, so a source that already knows the hash pays for it twice.\n","author":{"url":"https://github.com/fregataa","@type":"Person","name":"fregataa"},"datePublished":"2026-08-11T01:09:33Z","url":"https://github.com/RustPython/RustPython/issues/8490"}
| 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:31aeae93-d008-9bd1-5d3b-0713dc72db76 |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | E1B2:39FDE1:5C4BA5:73FAF0:6B0206B3 |
| html-safe-nonce | d3736ba47338b66962c3375c50cf6a7e905d0e7603b2b0d2aeb3bffbe45a6f78 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiJFMUIyOjM5RkRFMTo1QzRCQTU6NzNGQUYwOjZCMDIwNkIzIiwidmlzaXRvcl9pZCI6IjQ0MzcwMzYyMTc0MjI0NDgzMDciLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | 8ba7c871a9ececb3a3110984a8b9ffd28bb4c06a32cbcf7f39f6c4e9b8de0454 |
| hovercard-subject-tag | issue:5115707621 |
| 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/8490/issue_layout |
| twitter:image | https://opengraph.githubassets.com/360aa59f34e4235f367796524e79a5ba41afb10bb46455b31aedf2d8272d3d4e/RustPython/RustPython/issues/8490 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/360aa59f34e4235f367796524e79a5ba41afb10bb46455b31aedf2d8272d3d4e/RustPython/RustPython/issues/8490 |
| og:image:alt | Summary dict.fromkeys() recomputes __hash__ for every key when the iterable is a set, frozenset, or dict — all of which already store a hash per entry. CPython's dict_fromkeys_impl (Objects/dictobj... |
| 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