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

4.x: Improve the performance of ToObservable() #705

Merged
merged 4 commits into from
Jun 29, 2018

Conversation

akarnokd
Copy link
Collaborator

@akarnokd akarnokd commented Jun 28, 2018

This PR improves the performance and allocation behavior of the ToObservable(IEnumerable) operator by inlining the recursive scheduling management and splits the operator into separate, dedicated long-running and recursive variants.

The recursive variant is still declared to not link up the disposable management so the volatile bool _disposed field is enough to signal cancellation. In addition, the previous Action<Action<>>-based recursive scheduling was mostly responsible for the overhead as it was still tracking each recursively scheduled task despite they would have never gotten disposed.

Comparison:

BenchmarkDotNet=v0.10.14, OS=Windows 10.0.17134
Intel Core i7-4790 CPU 3.60GHz (Haswell), 1 CPU, 8 logical and 4 physical cores
Frequency=3513586 Hz, Resolution=284.6095 ns, Timer=TSC
  [Host]     : .NET Framework 4.6.2 (CLR 4.0.30319.42000), 64bit RyuJIT-v4.7.3110.0
  DefaultJob : .NET Framework 4.6.2 (CLR 4.0.30319.42000), 64bit RyuJIT-v4.7.3110.0

// ============================================
// Before

 Method |       N |           Mean |         Error |        StdDev |      Gen 0 |   Allocated |
------- |-------- |---------------:|--------------:|--------------:|-----------:|------------:|
  Exact |       1 |       1.136 us |     0.0122 us |     0.0108 us |     0.2346 |       992 B |
  Exact |      10 |       4.897 us |     0.0435 us |     0.0407 us |     0.5493 |      2312 B |
  Exact |     100 |      43.431 us |     0.5890 us |     0.5221 us |     3.6621 |     15552 B |
  Exact |    1000 |     432.782 us |     3.5242 us |     2.9429 us |    34.6680 |    146291 B |
  Exact |   10000 |   4,298.397 us |    72.5810 us |    67.8924 us |   343.7500 |   1450440 B |
  Exact |  100000 |  41,972.884 us |   240.8476 us |   225.2889 us |  3437.5000 |  14500174 B |
  Exact | 1000000 | 426,720.117 us | 7,334.0969 us | 6,860.3181 us | 34562.5000 | 144987362 B |

// ============================================
// After

 Method |       N |             Mean |          Error |         StdDev |      Gen 0 |  Allocated |
------- |-------- |-----------------:|---------------:|---------------:|-----------:|-----------:|
  Exact |       1 |         738.3 ns |       5.741 ns |       5.370 ns |     0.1497 |      632 B |
  Exact |      10 |       2,464.1 ns |      34.347 ns |      32.128 ns |     0.2861 |     1208 B |
  Exact |     100 |      26,969.5 ns |     167.985 ns |     157.133 ns |     1.6479 |     6968 B |
  Exact |    1000 |     189,754.5 ns |   2,804.522 ns |   2,486.136 ns |    15.3809 |    64569 B |
  Exact |   10000 |   1,882,753.7 ns |   6,135.018 ns |   5,738.699 ns |   152.3438 |   640589 B |
  Exact |  100000 |  26,518,449.9 ns | 180,846.221 ns | 169,163.651 ns |  1500.0000 |  6400644 B |
  Exact | 1000000 | 262,672,144.8 ns | 937,389.291 ns | 830,971.109 ns | 15250.0000 | 64001342 B |

@danielcweber danielcweber merged commit 59fec42 into dotnet:master Jun 29, 2018
@akarnokd akarnokd deleted the ToObservableImprovements branch June 29, 2018 17:28
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants