Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Specialize Dict's key to ByteArray #81

Merged
merged 7 commits into from
May 4, 2024
Merged

Conversation

KtorZ
Copy link
Member

@KtorZ KtorZ commented Jan 14, 2024

Looking at Aiken's repositories in the wild, no one is using Dict with
anything else than ByteArray as key. The extra complexity we bring
just for making it generic in the key argument seems not worth it.

Plus, if we know the key is a ByteArray, we can perform some
optimizations on a few functions looking for keys to short-circuit
them instead of traversing a list entirely.

Here's a comparison of the mem and cpu cost before and after the
chance for a few tests (omitted tests have identical cost).

mem cpu bench
-10.15% -5.88% delete_3
-4.00% -9.68% delete_4
-3.80% -9.39% delete_5
-31.03% -42.88% delete_6
-15.26% -26.34% find_4
-11.10% -18.85% from_list_2
-8.02% -16.26% from_list_3
-12.15% -25.39% from_list_4
-19.24% -36.11% bench_from_list
-17.79% -18.91% bench_from_ascending_list
-4.91% -10.58% get_2
-9.49% -14.63% get_3
-29.94% -42.75% get_4
-23.66% -37.70% get_5
-7.94% -5.03% has_key_3
-9.97% -18.50% has_key_4
-16.46% -23.61% insert_1
-16.52% -26.24% insert_2
-13.82% -22.08% insert_with_1
-18.77% -29.88% insert_with_2
-19.12% -30.52% insert_with_3
-10.47% -17.40% keys_2
4.25% 5.06% map_1
2.94% 3.31% map_2
-10.99% -19.83% size_3
-7.94% -16.17% union_1
-17.74% -28.17% union_3
-16.96% -26.55% union_4
-15.91% -25.10% union_with_1
-10.47% -17.39% values_2

KtorZ added 2 commits April 3, 2024 11:16
  Looking at Aiken's repositories in the wild, no one is using Dict with
  anything else than ByteArray as key. The extra complexity we bring
  just for making it generic in the key argument seems not worth it.

  Plus, if we know the key is a ByteArray, we can perform some
  optimizations on a few functions looking for keys to short-circuit
  them instead of traversing a list entirely.

  Here's a comparison of the mem and cpu cost before and after the
  chance for a few tests (omitted tests have identical cost).

  | mem     | cpu     | bench                     |
  | ---     | ---     | ---                       |
  | -10.15% | -5.88%  | delete_3                  |
  | -4.00%  | -9.68%  | delete_4                  |
  | -3.80%  | -9.39%  | delete_5                  |
  | -31.03% | -42.88% | delete_6                  |
  | -15.26% | -26.34% | find_4                    |
  | -11.10% | -18.85% | from_list_2               |
  | -8.02%  | -16.26% | from_list_3               |
  | -12.15% | -25.39% | from_list_4               |
  | -19.24% | -36.11% | bench_from_list           |
  | -17.79% | -18.91% | bench_from_ascending_list |
  | -4.91%  | -10.58% | get_2                     |
  | -9.49%  | -14.63% | get_3                     |
  | -29.94% | -42.75% | get_4                     |
  | -23.66% | -37.70% | get_5                     |
  | -7.94%  | -5.03%  | has_key_3                 |
  | -9.97%  | -18.50% | has_key_4                 |
  | -16.46% | -23.61% | insert_1                  |
  | -16.52% | -26.24% | insert_2                  |
  | -13.82% | -22.08% | insert_with_1             |
  | -18.77% | -29.88% | insert_with_2             |
  | -19.12% | -30.52% | insert_with_3             |
  | -10.47% | -17.40% | keys_2                    |
  | 4.25%   | 5.06%   | map_1                     |
  | 2.94%   | 3.31%   | map_2                     |
  | -10.99% | -19.83% | size_3                    |
  | -7.94%  | -16.17% | union_1                   |
  | -17.74% | -28.17% | union_3                   |
  | -16.96% | -26.55% | union_4                   |
  | -15.91% | -25.10% | union_with_1              |
  | -10.47% | -17.39% | values_2                  |
@KtorZ KtorZ force-pushed the specialize-dict-key branch from 89b2e9f to 2df2aa8 Compare April 18, 2024 18:03
@KtorZ KtorZ force-pushed the specialize-dict-key branch 2 times, most recently from 7bb5b2a to 4dc6672 Compare May 1, 2024 11:51
  Unlike `Dict`, `Map` have no invariant and are simply mere wrapper
  around a list of pairs. Functions make no assumption about order of
  the keys and this is translated into the API which facilitate the
  access to underlying elements in various ways.

  Co-authored-by: KtorZ <[email protected]>
@KtorZ KtorZ force-pushed the specialize-dict-key branch from 4dc6672 to 577a586 Compare May 1, 2024 13:17
@MicroProofs MicroProofs merged commit c5f1994 into main May 4, 2024
1 of 2 checks passed
@MicroProofs MicroProofs deleted the specialize-dict-key branch May 4, 2024 18:04
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
Status: 🚀 Released
Development

Successfully merging this pull request may close these issues.

2 participants