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

perf(Subject): Don't clone observers array unless we have to #6842

Merged
merged 1 commit into from
Feb 21, 2022
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
23 changes: 17 additions & 6 deletions src/internal/Subject.ts
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,9 @@ import { errorContext } from './util/errorContext';
*/
export class Subject<T> extends Observable<T> implements SubscriptionLike {
closed = false;

private currentObservers: Observer<T>[] | null = null;

/** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */
observers: Observer<T>[] = [];
/** @deprecated Internal implementation detail, do not use directly. Will be made internal in v8. */
Expand Down Expand Up @@ -58,8 +61,10 @@ export class Subject<T> extends Observable<T> implements SubscriptionLike {
errorContext(() => {
this._throwIfClosed();
if (!this.isStopped) {
const copy = this.observers.slice();
for (const observer of copy) {
if (!this.currentObservers) {
this.currentObservers = Array.from(this.observers);
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The general idea is we only clone when currentObservers is null (invalidated) and then we use currentObservers when possible.

}
for (const observer of this.currentObservers) {
observer.next(value);
}
}
Expand Down Expand Up @@ -95,7 +100,7 @@ export class Subject<T> extends Observable<T> implements SubscriptionLike {

unsubscribe() {
this.isStopped = this.closed = true;
this.observers = null!;
this.observers = this.currentObservers = null!;
}

get observed() {
Expand All @@ -118,9 +123,15 @@ export class Subject<T> extends Observable<T> implements SubscriptionLike {
/** @internal */
protected _innerSubscribe(subscriber: Subscriber<any>) {
const { hasError, isStopped, observers } = this;
return hasError || isStopped
? EMPTY_SUBSCRIPTION
: (observers.push(subscriber), new Subscription(() => arrRemove(observers, subscriber)));
if (hasError || isStopped) {
return EMPTY_SUBSCRIPTION;
}
this.currentObservers = null;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We invalidate currentObservers when we have a new subscriber

observers.push(subscriber);
return new Subscription(() => {
this.currentObservers = null;
Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We also invalidate currentObservers when anyone unsubscribes.

arrRemove(observers, subscriber);
});
}

/** @internal */
Expand Down