TheAngryByrd / IcedTasks

F# Cold Tasks and Cancellable Tasks
https://www.jimmybyrd.me/IcedTasks/
MIT License
120 stars 5 forks source link

Allocations Optimizations #25

Closed TheAngryByrd closed 1 year ago

TheAngryByrd commented 1 year ago

Proposed Changes

Optimizes the Task/Awaiter path

Shown in the table below you can see the allocation was extremely high for binding to tasks directly.

|                                                      Method |    Categories |         Mean |        Error |      StdDev |       Median |  Ratio | RatioSD |       Gen0 |   Allocated | Alloc Ratio |
|                         AsyncBinds_cancellableTask_bindTask |    AsyncBinds |   3,732.8 μs |    34.07 μs |    31.87 μs |   3,731.6 μs |   1.03 |    0.01 |    23.4375 |    440170 B |        3.93 |
|              AsyncBinds_cancellableTask_bindCancellableTask |    AsyncBinds |   3,886.5 μs |    28.11 μs |    26.29 μs |   3,884.1 μs |   1.08 |    0.01 |    11.7188 |    200063 B |        1.79 |

After this change the allocations are the same as binding against CancellationToken -> Task<'T>

|                         AsyncBinds_cancellableTask_bindTask |    AsyncBinds |   3,748.7 μs |     21.73 μs |     20.33 μs |   3,753.9 μs |   1.05 |    0.01 |    11.7188 |    200038 B |        1.79 |
|              AsyncBinds_cancellableTask_bindCancellableTask |    AsyncBinds |   3,752.0 μs |     20.19 μs |     17.90 μs |   3,752.5 μs |   1.05 |    0.01 |    11.7188 |    200046 B |        1.79 |

Optimizes getCancellationToken

Similarly I was able to optimize allocations on getCancellationToken

|                               ManyWriteFile_cancellableTask | ManyWriteFile |     651.7 μs |    27.49 μs |    78.86 μs |     630.2 μs |   1.07 |    0.19 |     1.9531 |     32557 B |        3.96 |
|              ManyWriteFile_cancellableTask_withCancellation | ManyWriteFile |     669.3 μs |    30.86 μs |    88.55 μs |     654.5 μs |   1.10 |    0.21 |     1.9531 |     32742 B |        3.98 |
|             ManyWriteFile_cancellableTask_withCancellation2 | ManyWriteFile |     462.7 μs |    12.82 μs |    35.30 μs |     448.3 μs |   0.76 |    0.11 |          - |      8562 B |        1.04 |
|                               ManyWriteFile_cancellableTask | ManyWriteFile |     501.6 μs |    12.46 μs |    34.94 μs |     487.7 μs |   1.09 |    0.07 |          - |      8563 B |        1.04 |
|              ManyWriteFile_cancellableTask_withCancellation | ManyWriteFile |     509.7 μs |    16.83 μs |    48.28 μs |     488.4 μs |   1.12 |    0.11 |          - |      8690 B |        1.06 |
|             ManyWriteFile_cancellableTask_withCancellation2 | ManyWriteFile |     444.1 μs |    13.48 μs |    38.03 μs |     428.6 μs |   0.95 |    0.07 |     0.4883 |      8571 B |        1.04 |

Types of changes

What types of changes does your code introduce to IcedTasks? Put an x in the boxes that apply

Checklist

Put an x in the boxes that apply. You can also fill these out after creating the PR. If you're unsure about any of them, don't hesitate to ask. We're here to help! This is simply a reminder of what we are going to look for before merging your code.

Further comments

If this is a relatively large or complex change, kick off the discussion by explaining why you chose the solution you did and what alternatives you considered, etc...