create account

[SwapSteem] [Performance] Observable are not unsubscribed leading to higher CPU use by mightypanda

View this thread on: hive.blogpeakd.comecency.com
· @mightypanda · (edited)
$23.08
[SwapSteem] [Performance] Observable are not unsubscribed leading to higher CPU use
#### Project Information
**Repository**: https://github.com/nirvanaitsolutions/swapsteem
**Project Name**: SwapSteem

**Describe the bug**
Swapsteem code uses RxJS to subscribe to changes in data. RxJS in-turn use Observable which are Infinite Subscriptions. This means that the code will execute even without a subscribe being called unlike http.get where call will only be made when you subscribe to it. This causes a performance and memory hog as you exit and re-enter a controller because you are subscribing to same service multiple times. You can find more details in the stack overflow discussion here: https://stackoverflow.com/a/41177163/1141542

**To Reproduce**
This problem can be observed in dozens of places throughout the code. Below is a discussion on one such scenario and a fix for the same.

*You can clone the code using following command to check on your local machine. Git required*
```
git clone https://github.com/nirvanaitsolutions/swapsteem.git
```

*terms-and-conditions.component.ts*
```js
  this.auth.getUserData().subscribe((scAuthService) => {
        this.ngxService.stop();
        if (scAuthService) {
          this.auth.userData = scAuthService;
        }
        this.dialogRef.close();
      })
```

**Expected behavior**
Above code can be re-written as.
```js
  this.auth.getUserData().pipe(takeWhile(() => this.isAlive)).subscribe((scAuthService) => {
        this.ngxService.stop();
        if (scAuthService) {
          this.auth.userData = scAuthService;
        }
        this.dialogRef.close();
      })
```
When controller destroys set isAlive to false
```js
  ngOnDestroy() {
    this.isAlive = false;
  }
```

**GitHub Account** : https://github.com/mightypanda-x
**GitHub Issue** : https://github.com/nirvanaitsolutions/swapsteem/issues/147

πŸ‘  , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , , and 68 others
properties (23)
authormightypanda
permlinkswapsteem-performance-observable-are-not-unsubscribed-leading-to-higher-cpu-use
categoryutopian-io
json_metadata{"tags":["utopian-io","bug-hunting","busy"],"app":"busy/2.5.6","community":"busy","format":"markdown","users":[],"links":["https://github.com/nirvanaitsolutions/swapsteem","https://stackoverflow.com/a/41177163/1141542","https://github.com/mightypanda-x","https://github.com/nirvanaitsolutions/swapsteem/issues/147"],"image":[]}
created2019-03-07 04:19:06
last_update2019-03-07 04:37:00
depth0
children8
last_payout2019-03-14 04:19:06
cashout_time1969-12-31 23:59:59
total_payout_value17.207 HBD
curator_payout_value5.868 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,792
author_reputation21,847,608,676,835
root_title"[SwapSteem] [Performance] Observable are not unsubscribed leading to higher CPU use"
beneficiaries
0.
accountsteemplus-pay
weight100
1.
accountutopian.pay
weight500
max_accepted_payout100,000.000 HBD
percent_hbd10,000
post_id80,850,532
net_rshares31,974,986,349,108
author_curate_reward""
vote details (132)
@fego ·
$11.41
Hi @mightypanda, thanks for making this contribution.

This is unlike any bug we have received in times past. I see the PO has acknowledged the issue with a promise to put your thoughts into consideration in the next update by writing code with performance in mind.

This is a great find. I like that you properly explained your findings, the link you provided was also of great help. To a first time observer, the discussion in the thread helped to explain the issue, giving the reader a clearer perspective.

Thanks again for the contribution. Looking forward to more of these :)

Your contribution has been evaluated according to [Utopian policies and guidelines](https://join.utopian.io/guidelines), as well as a predefined set of questions pertaining to the category.

To view those questions and the relevant answers related to your post, [click here](https://review.utopian.io/result/5/3-1-1-1-1-3-).

---- 
Need help? Chat with us on [Discord](https://discord.gg/uTyJkNm).

[[utopian-moderator]](https://join.utopian.io/)
πŸ‘  , , , , , , , , , , , , , , , ,
properties (23)
authorfego
permlinkre-mightypanda-swapsteem-performance-observable-are-not-unsubscribed-leading-to-higher-cpu-use-20190308t045816605z
categoryutopian-io
json_metadata{"community":"busy","app":"busy/2.5.6","format":"markdown","tags":["utopian-io"],"users":["mightypanda"],"links":["/@mightypanda","https://join.utopian.io/guidelines","https://review.utopian.io/result/5/3-1-1-1-1-3-","https://discord.gg/uTyJkNm","https://join.utopian.io/"],"image":[]}
created2019-03-08 04:58:21
last_update2019-03-08 04:58:21
depth1
children2
last_payout2019-03-15 04:58:21
cashout_time1969-12-31 23:59:59
total_payout_value8.666 HBD
curator_payout_value2.739 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,029
author_reputation168,836,422,039,217
root_title"[SwapSteem] [Performance] Observable are not unsubscribed leading to higher CPU use"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id80,903,392
net_rshares15,380,860,858,215
author_curate_reward""
vote details (17)
@mightypanda ·
Thanks for the review.
properties (22)
authormightypanda
permlinkre-fego-re-mightypanda-swapsteem-performance-observable-are-not-unsubscribed-leading-to-higher-cpu-use-20190308t054210975z
categoryutopian-io
json_metadata{"community":"busy","app":"busy/2.5.6","format":"markdown","tags":["utopian-io"],"users":[],"links":[],"image":[]}
created2019-03-08 05:42:12
last_update2019-03-08 05:42:12
depth2
children0
last_payout2019-03-15 05:42:12
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length22
author_reputation21,847,608,676,835
root_title"[SwapSteem] [Performance] Observable are not unsubscribed leading to higher CPU use"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id80,904,527
net_rshares0
@utopian-io ·
Thank you for your review, @fego! Keep up the good work!
properties (22)
authorutopian-io
permlinkre-re-mightypanda-swapsteem-performance-observable-are-not-unsubscribed-leading-to-higher-cpu-use-20190308t045816605z-20190311t015535z
categoryutopian-io
json_metadata"{"app": "beem/0.20.17"}"
created2019-03-11 01:55:36
last_update2019-03-11 01:55:36
depth2
children0
last_payout2019-03-18 01:55:36
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length56
author_reputation152,955,367,999,756
root_title"[SwapSteem] [Performance] Observable are not unsubscribed leading to higher CPU use"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id81,043,085
net_rshares0
@minnowsupport ·
<p>Congratulations!  This post has been upvoted from the communal account, @minnowsupport, by mightyPanda from the Minnow Support Project. It's a witness project run by aggroed, ausbitbank, teamsteem, someguy123, neoxian, followbtcnews, and netuoso. The goal is to help Steemit grow by supporting Minnows.  Please find us at the <a href="https://discord.gg/HYj4yvw"> Peace, Abundance, and Liberty Network (PALnet) Discord Channel</a>.  It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.</p> <p>If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: <a href="https://v2.steemconnect.com/sign/delegateVestingShares?delegator=&amp;delegatee=minnowsupport&amp;vesting_shares=102530.639667%20VESTS">50SP</a>, <a href="https://v2.steemconnect.com/sign/delegateVestingShares?delegator=&amp;delegatee=minnowsupport&amp;vesting_shares=205303.639667%20VESTS">100SP</a>, <a href="https://v2.steemconnect.com/sign/delegateVestingShares?delegator=&amp;delegatee=minnowsupport&amp;vesting_shares=514303.639667%20VESTS">250SP</a>, <a href="https://v2.steemconnect.com/sign/delegateVestingShares?delegator=&amp;delegatee=minnowsupport&amp;vesting_shares=1025303.639667%20VESTS">500SP</a>, <a href="https://v2.steemconnect.com/sign/delegateVestingShares?delegator=&amp;delegatee=minnowsupport&amp;vesting_shares=2053030.639667%20VESTS">1000SP</a>, <a href="https://v2.steemconnect.com/sign/delegateVestingShares?delegator=&amp;delegatee=minnowsupport&amp;vesting_shares=10253030.639667%20VESTS">5000SP</a>. <br><strong>Be sure to leave at least 50SP undelegated on your account.</strong></p>
properties (22)
authorminnowsupport
permlinkre-swapsteem-performance-observable-are-not-unsubscribed-leading-to-higher-cpu-use-20190307t055732z
categoryutopian-io
json_metadata"{"app": "beem/0.20.17"}"
created2019-03-07 05:57:33
last_update2019-03-07 05:57:33
depth1
children0
last_payout2019-03-14 05:57:33
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length1,694
author_reputation148,902,805,319,183
root_title"[SwapSteem] [Performance] Observable are not unsubscribed leading to higher CPU use"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id80,855,815
net_rshares0
@steem-plus ·
SteemPlus upvote
Hi, @mightypanda!

You just got a **2.25%** upvote from SteemPlus!
To get higher upvotes, earn more SteemPlus Points (SPP). On your Steemit wallet, check your SPP balance and click on "How to earn SPP?" to find out all the ways to earn.
If you're not using SteemPlus yet, please check our last posts in [here](https://steemit.com/@steem-plus) to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.
properties (22)
authorsteem-plus
permlinkswapsteem-performance-observable-are-not-unsubscribed-leading-to-higher-cpu-use---vote-steemplus
categoryutopian-io
json_metadata{}
created2019-03-08 09:29:06
last_update2019-03-08 09:29:06
depth1
children0
last_payout2019-03-15 09:29:06
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length438
author_reputation247,952,188,232,400
root_title"[SwapSteem] [Performance] Observable are not unsubscribed leading to higher CPU use"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id80,911,053
net_rshares0
@steem-ua ·
$0.03
#### Hi @mightypanda!

Your post was upvoted by @steem-ua, new Steem dApp, using UserAuthority for algorithmic post curation!
Your post is eligible for our upvote, thanks to our collaboration with @utopian-io!
**Feel free to join our [@steem-ua Discord server](https://discord.gg/KpBNYGz)**
πŸ‘  
properties (23)
authorsteem-ua
permlinkre-swapsteem-performance-observable-are-not-unsubscribed-leading-to-higher-cpu-use-20190308t053444z
categoryutopian-io
json_metadata"{"app": "beem/0.20.18"}"
created2019-03-08 05:34:45
last_update2019-03-08 05:34:45
depth1
children0
last_payout2019-03-15 05:34:45
cashout_time1969-12-31 23:59:59
total_payout_value0.026 HBD
curator_payout_value0.008 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length290
author_reputation23,214,230,978,060
root_title"[SwapSteem] [Performance] Observable are not unsubscribed leading to higher CPU use"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id80,904,353
net_rshares48,254,540,536
author_curate_reward""
vote details (1)
@steemitboard ·
Congratulations @mightypanda! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

<table><tr><td>https://steemitimages.com/60x70/http://steemitboard.com/@mightypanda/votes.png?201903100406</td><td>You made more than 7000 upvotes. Your next target is to reach 8000 upvotes.</td></tr>
</table>

<sub>_[Click here to view your Board](https://steemitboard.com/@mightypanda)_</sub>
<sub>_If you no longer want to receive notifications, reply to this comment with the word_ `STOP`</sub>



###### [Vote for @Steemitboard as a witness](https://v2.steemconnect.com/sign/account-witness-vote?witness=steemitboard&approve=1) and get one more award and increased upvotes!
properties (22)
authorsteemitboard
permlinksteemitboard-notify-mightypanda-20190310t043229000z
categoryutopian-io
json_metadata{"image":["https://steemitboard.com/img/notify.png"]}
created2019-03-10 04:32:27
last_update2019-03-10 04:32:27
depth1
children0
last_payout2019-03-17 04:32:27
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length721
author_reputation38,975,615,169,260
root_title"[SwapSteem] [Performance] Observable are not unsubscribed leading to higher CPU use"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id80,994,459
net_rshares0
@utopian-io ·
Hey, @mightypanda!

**Thanks for contributing on Utopian**.
We’re already looking forward to your next contribution!

**Get higher incentives and support Utopian.io!**
 Simply set @utopian.pay as a 5% (or higher) payout beneficiary on your contribution post (via [SteemPlus](https://chrome.google.com/webstore/detail/steemplus/mjbkjgcplmaneajhcbegoffkedeankaj?hl=en) or [Steeditor](https://steeditor.app)).

**Want to chat? Join us on Discord https://discord.gg/h52nFrV.**

<a href='https://steemconnect.com/sign/account-witness-vote?witness=utopian-io&approve=1'>Vote for Utopian Witness!</a>
properties (22)
authorutopian-io
permlinkre-swapsteem-performance-observable-are-not-unsubscribed-leading-to-higher-cpu-use-20190309t005038z
categoryutopian-io
json_metadata"{"app": "beem/0.20.17"}"
created2019-03-09 00:50:39
last_update2019-03-09 00:50:39
depth1
children0
last_payout2019-03-16 00:50:39
cashout_time1969-12-31 23:59:59
total_payout_value0.000 HBD
curator_payout_value0.000 HBD
pending_payout_value0.000 HBD
promoted0.000 HBD
body_length593
author_reputation152,955,367,999,756
root_title"[SwapSteem] [Performance] Observable are not unsubscribed leading to higher CPU use"
beneficiaries[]
max_accepted_payout1,000,000.000 HBD
percent_hbd10,000
post_id80,942,652
net_rshares0