Commit 346e3bb
Reduce allocations within ReactiveObject and ReactiveRecord (#4195)
<!-- Please be sure to read the
[Contribute](https://github.com/reactiveui/reactiveui#contribute)
section of the README -->
**What kind of change does this PR introduce?**
<!-- Bug fix, feature, docs update, ... -->
Performance improvement.
**What is the current behavior?**
<!-- You can also link to an open issue here. -->
Currently `ReactiveObject` and `ReactiveRecord` use `Lazy<T>` to lazily
initialize `PropertyChanging`, `PropertyChanged`, `Changing`, `Changed`,
`ThrownExceptions`. This requires allocation of `Lazy<T>` itself and
allocation of closure. This produces unnecessary overhead of allocating
11 objects during construction instead of 1. This was main problem when
I was writing log reader, where each entry initially inherited from
`ReactiveObject` and I had to reimplement `INotifyProertyChanged`
manually.
**What is the new behavior?**
<!-- If this is a feature change -->
`Lazy<T>` was replaced with `Interlocked.CompareExchange`, events
initialization was performed with a simple `if` statement. It's a
simplified version of what
[`System.Threading.LazyInitializer`](https://github.com/dotnet/runtime/blob/main/src/libraries/System.Private.CoreLib/src/System/Threading/LazyInitializer.cs)
does.
**What might this PR break?**
Concurrent first access to `PropertyChanging` or `PropertyChanged`
events or first access to `Changing`, `Changed`, `ThrownExceptions`
properties.
**Please check if the PR fulfills these requirements**
- [ ] Tests for the changes have been added (for bug fixes / features)
- [ ] Docs have been added / updated (for bug fixes / features)
**Other information**:
---------
Co-authored-by: Glenn <[email protected]>1 parent d6b0ced commit 346e3bb
File tree
2 files changed
+37
-54
lines changed- src/ReactiveUI/ReactiveObject
2 files changed
+37
-54
lines changed| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
| 16 | + | |
| 17 | + | |
21 | 18 | | |
22 | 19 | | |
23 | 20 | | |
24 | 21 | | |
25 | 22 | | |
26 | 23 | | |
27 | | - | |
28 | | - | |
29 | | - | |
30 | | - | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | 24 | | |
45 | 25 | | |
46 | 26 | | |
47 | 27 | | |
48 | 28 | | |
49 | 29 | | |
50 | 30 | | |
51 | | - | |
| 31 | + | |
| 32 | + | |
| 33 | + | |
| 34 | + | |
| 35 | + | |
| 36 | + | |
52 | 37 | | |
53 | 38 | | |
54 | 39 | | |
| |||
59 | 44 | | |
60 | 45 | | |
61 | 46 | | |
62 | | - | |
| 47 | + | |
| 48 | + | |
| 49 | + | |
| 50 | + | |
| 51 | + | |
| 52 | + | |
63 | 53 | | |
64 | 54 | | |
65 | 55 | | |
| |||
78 | 68 | | |
79 | 69 | | |
80 | 70 | | |
81 | | - | |
| 71 | + | |
| 72 | + | |
82 | 73 | | |
83 | 74 | | |
84 | 75 | | |
| |||
87 | 78 | | |
88 | 79 | | |
89 | 80 | | |
90 | | - | |
| 81 | + | |
| 82 | + | |
91 | 83 | | |
92 | 84 | | |
93 | 85 | | |
| |||
96 | 88 | | |
97 | 89 | | |
98 | 90 | | |
99 | | - | |
| 91 | + | |
| 92 | + | |
100 | 93 | | |
101 | 94 | | |
102 | 95 | | |
| |||
| Original file line number | Diff line number | Diff line change | |
|---|---|---|---|
| |||
13 | 13 | | |
14 | 14 | | |
15 | 15 | | |
16 | | - | |
17 | | - | |
18 | | - | |
19 | | - | |
20 | | - | |
| 16 | + | |
| 17 | + | |
21 | 18 | | |
22 | 19 | | |
23 | 20 | | |
| |||
28 | 25 | | |
29 | 26 | | |
30 | 27 | | |
31 | | - | |
32 | | - | |
33 | | - | |
34 | | - | |
35 | | - | |
36 | | - | |
37 | | - | |
38 | | - | |
39 | | - | |
40 | | - | |
41 | | - | |
42 | | - | |
43 | | - | |
44 | | - | |
45 | | - | |
46 | | - | |
47 | | - | |
48 | 28 | | |
49 | 29 | | |
50 | 30 | | |
51 | 31 | | |
52 | 32 | | |
53 | 33 | | |
54 | 34 | | |
55 | | - | |
| 35 | + | |
| 36 | + | |
| 37 | + | |
| 38 | + | |
| 39 | + | |
| 40 | + | |
56 | 41 | | |
57 | 42 | | |
58 | 43 | | |
| |||
63 | 48 | | |
64 | 49 | | |
65 | 50 | | |
66 | | - | |
| 51 | + | |
| 52 | + | |
| 53 | + | |
| 54 | + | |
| 55 | + | |
| 56 | + | |
67 | 57 | | |
68 | 58 | | |
69 | 59 | | |
| |||
83 | 73 | | |
84 | 74 | | |
85 | 75 | | |
86 | | - | |
| 76 | + | |
87 | 77 | | |
88 | 78 | | |
89 | 79 | | |
| |||
93 | 83 | | |
94 | 84 | | |
95 | 85 | | |
96 | | - | |
| 86 | + | |
97 | 87 | | |
98 | 88 | | |
99 | 89 | | |
| |||
102 | 92 | | |
103 | 93 | | |
104 | 94 | | |
105 | | - | |
| 95 | + | |
106 | 96 | | |
107 | 97 | | |
108 | 98 | | |
| |||
0 commit comments