Skip to content

Use custom hash for maps and tuples to improve ordering#15398

Open
gldubc wants to merge 2 commits into
elixir-lang:mainfrom
gldubc:map-leaf-hash-order
Open

Use custom hash for maps and tuples to improve ordering#15398
gldubc wants to merge 2 commits into
elixir-lang:mainfrom
gldubc:map-leaf-hash-order

Conversation

@gldubc
Copy link
Copy Markdown
Member

@gldubc gldubc commented May 16, 2026

Map literals with a smaller hash get ordered higher in the BDD. If we use a negative hash for the :closed maps, then they will appear higher in BDDs and this will factor them (instead of having it multiplied on every leaf). We can also add a weight for the number of fields, so that more fields appear higher than less fields.

The same can be done for tuples as well.

The performance boost appears when using plain bdd operations (e.g. Livebook typechecking goes from 750ms to the usual ~280ms).

@gldubc gldubc force-pushed the map-leaf-hash-order branch from 1f93d21 to 6f1ac4c Compare May 17, 2026 10:35
@gldubc gldubc changed the title Use perf-boosting custom hash for ordering maps and tuples Use custom hash for maps and tuples to improve ordering May 17, 2026
Comment on lines +53 to +54
@map_leaf_hash_range 1 <<< 28
@tuple_leaf_hash_range 1 <<< 28
Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I believe phash2 is 32bit so maybe we want to use 33?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Apparently the default is to 27, so we are good!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Development

Successfully merging this pull request may close these issues.

2 participants