Skip to content

Commit 576bb60

Browse files
Improved hashing performance
1 parent 99838f8 commit 576bb60

File tree

1 file changed

+15
-6
lines changed

1 file changed

+15
-6
lines changed

src/python/freeze.cpp

Lines changed: 15 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1471,12 +1471,21 @@ FlatVariablesHasher::operator()(const std::shared_ptr<FlatVariables> &key) const
14711471
(uint64_t) (key->var_layout.size() << 2);
14721472

14731473
for (const Layout &layout : key->layout) {
1474-
// if layout.fields is not 0 then layout.num == layout.fields.size()
1474+
// If layout.fields is not 0 then layout.num == layout.fields.size()
14751475
// therefore we can omit layout.fields.size().
1476-
hash_combine(hash,
1477-
((uint64_t) layout.num << 32) | ((uint64_t) layout.index));
1478-
hash_combine(hash,
1479-
((uint64_t) layout.flags << 32) | ((uint64_t) layout.vt));
1476+
// Layout of the hash key data:
1477+
// struct {
1478+
// uint64_t num : 20;
1479+
// uint64_t index: 32;
1480+
// uint64_t flags: 8;
1481+
// uint64_t vt: 4;
1482+
// };
1483+
// This makes the assumption that we either don't have more than 1M
1484+
// elements in one layout.
1485+
hash_combine(hash, ((uint64_t) layout.num << 44) |
1486+
((uint64_t) layout.index << 12) |
1487+
((uint64_t) layout.flags << 4) |
1488+
((uint64_t) layout.vt));
14801489
if (layout.flags & (uint32_t) LayoutFlag::JitIndex)
14811490
hash_combine(hash, layout.literal);
14821491

@@ -1507,7 +1516,7 @@ FlatVariablesHasher::operator()(const std::shared_ptr<FlatVariables> &key) const
15071516
for (const VarLayout &layout : key->var_layout) {
15081517
// layout.vt: 4
15091518
// layout.vs: 4
1510-
// layout.flags: 6
1519+
// layout.flags: 8
15111520
hash_combine(hash, ((uint64_t) layout.size_index << 32) |
15121521
((uint64_t) layout.flags << 8) |
15131522
((uint64_t) layout.vs << 4) |

0 commit comments

Comments
 (0)