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

Improve BindableList performance + reduce allocations #6450

Merged
merged 2 commits into from
Dec 10, 2024

Conversation

smoogipoo
Copy link
Contributor

Extracted out from + excludes the controversial changes of #6405.

Before:

|    Method | NumBindings |        Mean | Error |     StdDev | Ratio | RatioSD |   Gen0 |   Gen1 | Allocated | Alloc Ratio |
|---------- |------------ |------------:|------:|-----------:|------:|--------:|-------:|-------:|----------:|------------:|
|    Create |           0 |    81.33 ns |    NA |   1.539 ns |  1.00 |    0.00 | 0.0930 |      - |     584 B |        1.00 |
|       Add |           0 |   135.18 ns |    NA |  21.728 ns |  1.66 |    0.24 | 0.1364 |      - |     856 B |        1.47 |
|    Remove |           0 |    96.40 ns |    NA |   1.789 ns |  1.19 |    0.04 | 0.1147 |      - |     720 B |        1.23 |
|     Clear |           0 |   106.17 ns |    NA |   0.695 ns |  1.31 |    0.02 | 0.1211 |      - |     760 B |        1.30 |
|  AddRange |           0 |   131.20 ns |    NA |   2.461 ns |  1.61 |    0.06 | 0.1390 |      - |     872 B |        1.49 |
|  SetIndex |           0 |   126.74 ns |    NA |   1.458 ns |  1.56 |    0.05 | 0.1440 |      - |     904 B |        1.55 |
| Enumerate |           0 |    83.12 ns |    NA |   0.102 ns |  1.02 |    0.02 | 0.0930 |      - |     584 B |        1.00 |
|           |             |             |       |            |       |         |        |        |           |             |
|    Create |           1 |   232.07 ns |    NA |  11.904 ns |  1.00 |    0.00 | 0.1299 |      - |     816 B |        1.00 |
|       Add |           1 |   337.81 ns |    NA |   9.747 ns |  1.46 |    0.03 | 0.1884 |      - |    1184 B |        1.45 |
|    Remove |           1 |   238.13 ns |    NA |   1.806 ns |  1.03 |    0.06 | 0.1450 |      - |     912 B |        1.12 |
|     Clear |           1 |   256.45 ns |    NA |   0.311 ns |  1.11 |    0.06 | 0.1578 |      - |     992 B |        1.22 |
|  AddRange |           1 |   358.29 ns |    NA |   0.575 ns |  1.55 |    0.08 | 0.1936 |      - |    1216 B |        1.49 |
|  SetIndex |           1 |   344.04 ns |    NA |   1.081 ns |  1.48 |    0.08 | 0.2036 |      - |    1280 B |        1.57 |
| Enumerate |           1 |   230.97 ns |    NA |   3.169 ns |  1.00 |    0.06 | 0.1299 |      - |     816 B |        1.00 |
|           |             |             |       |            |       |         |        |        |           |             |
|    Create |          10 | 1,454.40 ns |    NA |  18.072 ns |  1.00 |    0.00 | 0.6485 |      - |    4072 B |        1.00 |
|       Add |          10 | 2,080.50 ns |    NA |   1.270 ns |  1.43 |    0.02 | 0.9384 |      - |    5888 B |        1.45 |
|    Remove |          10 | 1,369.26 ns |    NA |   4.166 ns |  0.94 |    0.01 | 0.6065 |      - |    3808 B |        0.94 |
|     Clear |          10 | 1,447.20 ns |    NA |   2.785 ns |  1.00 |    0.01 | 0.6771 |      - |    4248 B |        1.04 |
|  AddRange |          10 | 2,223.23 ns |    NA |   6.040 ns |  1.53 |    0.01 | 0.9651 |      - |    6064 B |        1.49 |
|  SetIndex |          10 | 2,176.85 ns |    NA |  20.808 ns |  1.50 |    0.03 | 1.0223 |      - |    6416 B |        1.58 |
| Enumerate |          10 | 1,423.40 ns |    NA |   0.864 ns |  0.98 |    0.01 | 0.6485 |      - |    4072 B |        1.00 |
|           |             |             |       |            |       |         |        |        |           |             |
|    Create |          20 | 2,960.90 ns |    NA |  75.523 ns |  1.00 |    0.00 | 1.2703 | 0.0038 |    7976 B |        1.00 |
|       Add |          20 | 4,214.74 ns |    NA |  11.631 ns |  1.42 |    0.04 | 1.8387 |      - |   11544 B |        1.45 |
|    Remove |          20 | 3,661.51 ns |    NA |   4.385 ns |  1.24 |    0.03 | 1.1635 |      - |    7312 B |        0.92 |
|     Clear |          20 | 3,072.85 ns |    NA | 158.676 ns |  1.04 |    0.08 | 1.2970 | 0.0038 |    8152 B |        1.02 |
|  AddRange |          20 | 4,493.25 ns |    NA |   3.102 ns |  1.52 |    0.04 | 1.8921 | 0.0076 |   11880 B |        1.49 |
|  SetIndex |          20 | 4,488.73 ns |    NA |  48.956 ns |  1.52 |    0.02 | 1.9989 |      - |   12552 B |        1.57 |
| Enumerate |          20 | 3,041.22 ns |    NA | 160.420 ns |  1.03 |    0.03 | 1.2703 | 0.0038 |    7976 B |        1.00 |

After:

|    Method | NumBindings |        Mean | Error |     StdDev | Ratio | RatioSD |   Gen0 | Allocated | Alloc Ratio |
|---------- |------------ |------------:|------:|-----------:|------:|--------:|-------:|----------:|------------:|
|    Create |           0 |    49.91 ns |    NA |   0.813 ns |  1.00 |    0.00 | 0.0561 |     352 B |        1.00 |
|       Add |           0 |    74.59 ns |    NA |   1.424 ns |  1.49 |    0.00 | 0.0842 |     528 B |        1.50 |
|    Remove |           0 |    75.69 ns |    NA |   0.761 ns |  1.52 |    0.04 | 0.0842 |     528 B |        1.50 |
|     Clear |           0 |    74.06 ns |    NA |   0.168 ns |  1.48 |    0.02 | 0.0842 |     528 B |        1.50 |
|  AddRange |           0 |    82.64 ns |    NA |   1.324 ns |  1.66 |    0.05 | 0.0842 |     528 B |        1.50 |
|  SetIndex |           0 |    74.48 ns |    NA |   0.211 ns |  1.49 |    0.02 | 0.0842 |     528 B |        1.50 |
| Enumerate |           0 |    50.59 ns |    NA |   0.092 ns |  1.01 |    0.02 | 0.0561 |     352 B |        1.00 |
|           |             |             |       |            |       |         |        |           |             |
|    Create |           1 |   161.05 ns |    NA |   1.265 ns |  1.00 |    0.00 | 0.0560 |     352 B |        1.00 |
|       Add |           1 |   241.94 ns |    NA |   1.801 ns |  1.50 |    0.00 | 0.0839 |     528 B |        1.50 |
|    Remove |           1 |   193.39 ns |    NA |   1.447 ns |  1.20 |    0.02 | 0.0842 |     528 B |        1.50 |
|     Clear |           1 |   190.15 ns |    NA |   1.779 ns |  1.18 |    0.00 | 0.0842 |     528 B |        1.50 |
|  AddRange |           1 |   262.38 ns |    NA |   0.329 ns |  1.63 |    0.01 | 0.0839 |     528 B |        1.50 |
|  SetIndex |           1 |   244.24 ns |    NA |   0.791 ns |  1.52 |    0.01 | 0.0839 |     528 B |        1.50 |
| Enumerate |           1 |   163.61 ns |    NA |   5.447 ns |  1.02 |    0.03 | 0.0560 |     352 B |        1.00 |
|           |             |             |       |            |       |         |        |           |             |
|    Create |          10 | 1,623.94 ns |    NA |  80.347 ns |  1.00 |    0.00 | 0.2422 |    1520 B |        1.00 |
|       Add |          10 | 1,588.68 ns |    NA |  36.029 ns |  0.98 |    0.07 | 0.3624 |    2280 B |        1.50 |
|    Remove |          10 | 1,713.21 ns |    NA | 524.839 ns |  1.05 |    0.27 | 0.2689 |    1696 B |        1.12 |
|     Clear |          10 | 1,309.85 ns |    NA |  90.409 ns |  0.81 |    0.10 | 0.2689 |    1696 B |        1.12 |
|  AddRange |          10 | 1,710.17 ns |    NA |   4.467 ns |  1.05 |    0.05 | 0.3624 |    2280 B |        1.50 |
|  SetIndex |          10 | 1,629.28 ns |    NA |  17.520 ns |  1.00 |    0.04 | 0.3624 |    2280 B |        1.50 |
| Enumerate |          10 | 1,269.36 ns |    NA |  58.352 ns |  0.78 |    0.07 | 0.2422 |    1520 B |        1.00 |
|           |             |             |       |            |       |         |        |           |             |
|    Create |          20 | 2,184.72 ns |    NA |   4.066 ns |  1.00 |    0.00 | 0.4921 |    3104 B |        1.00 |
|       Add |          20 | 3,328.71 ns |    NA |  15.992 ns |  1.52 |    0.00 | 0.7401 |    4656 B |        1.50 |
|    Remove |          20 | 2,346.84 ns |    NA |   9.699 ns |  1.07 |    0.01 | 0.5226 |    3280 B |        1.06 |
|     Clear |          20 | 2,206.18 ns |    NA |   5.316 ns |  1.01 |    0.00 | 0.5226 |    3280 B |        1.06 |
|  AddRange |          20 | 4,812.98 ns |    NA |  75.557 ns |  2.20 |    0.04 | 0.7401 |    4656 B |        1.50 |
|  SetIndex |          20 | 3,316.43 ns |    NA |  60.392 ns |  1.52 |    0.02 | 0.7401 |    4656 B |        1.50 |
| Enumerate |          20 | 2,168.55 ns |    NA |   4.329 ns |  0.99 |    0.00 | 0.4921 |    3104 B |        1.00 |

Remove usages of `Cast<T>` by using `ICollection<T>`. `List<T>`
internally optimises across `ICollection<T>`, so doing this removes an
enumeration.

Replaced `notifyCollectionChanged()` with local invocations, allowing
null-check to happen.

Only capture previous states (e.g. the cleared items during `Clear()`)
if there's a subscription to `CollectionChanged`.
@bdach bdach merged commit 86fc2c7 into ppy:master Dec 10, 2024
13 of 14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants