You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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]>
0 commit comments