feat: optimize `RestMethodInfo`, reduce dictionary allocations and li… by TimothyMakkison · Pull Request #1742 · reactiveui/refit
- Changed
ParameterInfoMaptoParameterInfoArray.- Not sure why refit was using a dictionary as an array?
- Changed
Headers,HeaderParameterMap,PropertyParameterMap,QueryParameterMap,AttachmentNameMapto only allocate a dictionary when needed.- Sharing a mutable
Emptydictionary is a little risky but I dont see why we'd ever want to mutate the collection.
- Sharing a mutable
- Addded
EmptyDictionary<TKey, TValue>.get()to create static empty dictionaries. I would prefer to useImmutableDictionary.Emptyand changeDictionarytoIReadOnlyDictionarybut they aren't available in . Net Framework.- This is a little messy but It saves 72 bytes per dictionary instance.
bodyParamEnumerableandauthorizeParamsEnumerablenow use a tuple instead of an anonymous class.- Used an integer
HeaderCollectionParameterIndexinstead of the hashsetHeaderCollectionParameterMap- Another strange one, refit explicitly prevents adding more than 1 item to the hashset, it seemed a little redundant 😄.
We could probably get away with using arrays instead of Dictionary<int, TSomething> they would use less memory in most cases and would have faster lookups for smaller numbers.
Saves 4Kb of memory, I might be able to do more after profiling.
Original
| Method | Job | InvocationCount | UnrollFactor | Mean | Error | StdDev | Gen0 | Gen1 | Allocated |
|---|---|---|---|---|---|---|---|---|---|
| CreateService | DefaultJob | Default | 16 | 71.07 us | 1.320 us | 1.412 us | 5.1270 | 0.1221 | 47.17 KB |
| ConstantRouteAsync | DefaultJob | Default | 16 | 56.56 us | 0.908 us | 0.758 us | 5.8594 | - | 55.27 KB |
| ComplexRequestAsync | DefaultJob | Default | 16 | 59.39 us | 1.143 us | 1.122 us | 6.1035 | 0.1221 | 57.09 KB |
| FirstCallConstantRouteAsync | Job-NYYUCJ | 1 | 1 | 32.87 us | 0.640 us | 0.897 us | - | - | 9.08 KB |
| FirstCallComplexRequestAsync | Job-NYYUCJ | 1 | 1 | 58.53 us | 1.669 us | 4.843 us | - | - | 11.9 KB |
Changes
| Method | Job | InvocationCount | UnrollFactor | Mean | Error | StdDev | Median | Gen0 | Gen1 | Allocated |
|---|---|---|---|---|---|---|---|---|---|---|
| CreateService | DefaultJob | Default | 16 | 46.24 us | 0.828 us | 0.692 us | 46.30 us | 4.6387 | 0.1221 | 43 KB |
| ConstantRouteAsync | DefaultJob | Default | 16 | 51.42 us | 0.282 us | 0.236 us | 51.41 us | 5.5542 | 0.1221 | 51.1 KB |
| ComplexRequestAsync | DefaultJob | Default | 16 | 55.17 us | 0.730 us | 0.683 us | 55.13 us | 5.7373 | 0.1221 | 52.94 KB |
| FirstCallConstantRouteAsync | Job-OIQPYS | 1 | 1 | 36.07 us | 1.025 us | 2.873 us | 35.20 us | - | - | 9.08 KB |
| FirstCallComplexRequestAsync | Job-OIQPYS | 1 | 1 | 50.36 us | 1.357 us | 3.738 us | 49.30 us | - | - | 11.9 KB |