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

in utopian-io •  16 days ago

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

  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.

  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

  ngOnDestroy() {
    this.isAlive = false;
  }

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

Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order:  

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, 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.


Need help? Chat with us on Discord.

[utopian-moderator]

·

Thanks for the review.

·

Thank you for your review, @fego! Keep up the good work!

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

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 Peace, Abundance, and Liberty Network (PALnet) Discord Channel. It's a completely public and open space to all members of the Steemit community who voluntarily choose to be there.

If you would like to delegate to the Minnow Support Project you can do so by clicking on the following links: 50SP, 100SP, 250SP, 500SP, 1000SP, 5000SP.
Be sure to leave at least 50SP undelegated on your account.

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 to see the many ways in which SteemPlus can improve your Steem experience on Steemit and Busy.

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 or Steeditor).

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

Vote for Utopian Witness!

Congratulations @mightypanda! You have completed the following achievement on the Steem blockchain and have been rewarded with new badge(s) :

You made more than 7000 upvotes. Your next target is to reach 8000 upvotes.

Click here to view your Board
If you no longer want to receive notifications, reply to this comment with the word STOP

Vote for @Steemitboard as a witness and get one more award and increased upvotes!