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

refactor: Reduce memory consumption and simplify inner and outer subscription #5610

Merged
merged 4 commits into from
Jul 30, 2020

Conversation

benlesh
Copy link
Member

@benlesh benlesh commented Jul 30, 2020

  • Reduces memory pressure by no longer capturing outer values where they are not needed. For example, every inner subscription in mergeMap was retaining the outer value, if the outer value was a large array, that could add up
  • Makes subscribeToResult less ridiculous, I am not sure what I was thinking there 🤦
  • Leaves a few operators with more complex inner subscriptions, to be refactored later.
  • Removes some redundant code that removed inner subscriptions manually, this was necessary in the early days of RxJS 5, but has not been necessary in a long time

Explainer

So the old OuterSubscription is now called ComplexOuterSubscription, and InnerSubscription is ComplexInnerSubscription.

Basically, back when we had all sorts of additional "resultSelectors" (pre v6), those result selectors relied on data that we captured with the InnerSubscription. After removing resultSelectors, that was no longer needed in the majority of cases, BUT it was still there to keep this "one inner subscription process to rule them all" sort of thing we had going.

Unfortunately, those trapped outer values can be costly. Imagine this scenario:

import { of, timer } from 'rxjs';
import { mergeMap } from 'rxjs';

const data = new Array(1000000).fill('test');

of(data).pipe(
  mergeMap(d => timer(1000000))
)
.subscribe(console.log);

In the above scenario, the subscription to of(data), as a source begins and ends synchronously. BUT!!! even though we don't even use that huge array in the inner observable we return in the mergeMap, that array is retained in the InnerSubscriber's outerValue property, which is created by the merge map... and held, in this case, for 1,000 seconds! Not great.

This PR solves that by no longer retaining the outerValue unless necessary.

There's a long tail of one-offs that we'll need to address. But they are commented on the ComplexOuterSubscriber implementation and its methods.

@benlesh benlesh requested a review from cartant July 30, 2020 02:04
- Reduces memory pressure by no longer capturing outer values where they are not needed. For example, every inner subscription in mergeMap was retaining the outer value, if the outer value was a large array, that could add up
- Makes subscribeToResult less ridiculous, I am not sure what I was thinking there
- Leaves a few operators with more complex inner subscriptions, to be refactored later.
- Removes some redundant code that removed inner subscriptions manually, this was necessary in the early days of RxJS 5, but has not been necessary in a long time
@benlesh benlesh force-pushed the refactor/reduce-memory-pressure branch from d115113 to 242e87c Compare July 30, 2020 02:19
@benlesh benlesh changed the title refactor: simplify inner and outer subscription refactor: Reduce memory consumption and simplify inner and outer subscription Jul 30, 2020
Copy link
Collaborator

@cartant cartant left a comment

Choose a reason for hiding this comment

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

Some comments, opinions and nits.

src/internal/innies-and-outies.ts Outdated Show resolved Hide resolved
src/internal/operators/exhaustMap.ts Show resolved Hide resolved
src/internal/innies-and-outies.ts Outdated Show resolved Hide resolved
src/internal/innies-and-outies.ts Outdated Show resolved Hide resolved
Copy link
Member

@niklas-wortmann niklas-wortmann left a comment

Choose a reason for hiding this comment

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

LGTM

@benlesh benlesh merged commit bff1827 into ReactiveX:master Jul 30, 2020
benlesh added a commit to benlesh/rxjs that referenced this pull request Jul 30, 2020
this is the duplicate of ReactiveX#5610, it is refactoring to ensure outer values are not retained when they do not have to be. It needs to be done in a separate PR because the branches diverge just enough to require it. This PR also has some mild, internal type fixes.
benlesh added a commit that referenced this pull request Jul 31, 2020
this is the duplicate of #5610, it is refactoring to ensure outer values are not retained when they do not have to be. It needs to be done in a separate PR because the branches diverge just enough to require it. This PR also has some mild, internal type fixes.
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.

3 participants