angular-service-utilities icon indicating copy to clipboard operation
angular-service-utilities copied to clipboard

High CPU usage

Open akoenig opened this issue 11 years ago • 6 comments

We are using angular-service-utilities in a bigger application and it fills the gap, when it comes to 2way data binding between services and the controller scope which is our use-case. Thanks a lot for the good work! Now we are pretty close to a 1.0 version of our application and doing the last profiling analyses before releasing the app.

Unfortunately we identified a pretty huge performance problem which brings the CPU load to nearly 100% most of the time. After further investigation we found out that the runtime environment spends the time on executing the "digestOnceOnNextTick" in general and the "debounce" function in particular:

https://github.com/mbertolacci/angular-service-utilities/blob/master/angular-service-utilities.js#L189

We think that the "0" interval is pretty tight but necessary to achieve the "digest triggering" in real-time. Anyway, do you have any advice for a workaround?

/ André

akoenig avatar Aug 19 '13 18:08 akoenig

Really happy to hear you're getting some value from the library. :)

I suspect it's a bug equivalent to a $digest cycle, but with the digest calls separated by the setTimeout.

I'd love to take a closer look at it later, but in the meantime, can I double check that you're using the latest version of the library? I introduced an update a few months ago that deals with one of those corner cases. However, I might have missed a few more.

Failing that, do you have any sample code triggering the problem I could take a look at?

mbertolacci avatar Aug 19 '13 23:08 mbertolacci

Thanks for your quick reply :)

I think we identified the problem. Take a look at this scenario: We have something like a ParentView which encapsulates the more complex views. In the parent view which is literally responsible for displaying user information and so on. So while bootstrapping the app, we check in the modules "run" function if there is a session and store the respective user object in the $rootScope (this very object is the only information that we store in the $rootScope). For taking advantage of the 2way data binding between the service and the particular scope we've bound the encapsulated user object within the service to the $rootScope:

UserService.$attachProperty('session', $rootScope, 'user);

If I comment this line out, there is no high CPU usage anymore. At the moment I don't see an alternative to "binding this object to the $rootScope" :(

akoenig avatar Aug 20 '13 10:08 akoenig

Ah! That makes sense. Every serviceScope is a child of $rootScope, so when the service scope triggers a digest on the attached scope (in this case rootScope) to notify it of changes, it also triggers another digest on the serviceScope.

I've updated the library to deal with this case. However, I can't be 100% sure it fixes the issue you're seeing - can you grab the latest and take a look?

mbertolacci avatar Aug 23 '13 00:08 mbertolacci

Thanks for this patch!

I tried the latest version, but unfortunately the beast is still alive.

akoenig avatar Aug 23 '13 09:08 akoenig

Okay, I have another theory about the edge case causing it. I'll have to follow up on Monday though.

mbertolacci avatar Aug 23 '13 09:08 mbertolacci

:+1:

Okay, that's fine. Looking forward to see your theory in practice :)

akoenig avatar Aug 23 '13 09:08 akoenig