fix(ngRepeat): trigger move animation
What kind of change does this PR introduce? (Bug fix, feature, docs update, ...) bug fix
What is the current behavior? (You can also link to an open issue here) #15068
Hi @pondermatic, thanks for this PR. This is a much requested feature, and I'd be happy if we can get this in. Since ngRepeat is a performance critical part of the code, we'll have to review it thoroughly, though (and run some benchmarks). I've looked at the changes, and I have the feeling that some of them aren't really related to the PR. could you maybe comment on these sections that are essential to the PR, or even create a commit that only includes the changes need to fix this?
The diff is pretty ugly because I changed several variable names. I'll work on a version that preserves as many names as possible. I would appreciate performance testing. I timed the unit tests in Windows Chrome between the previous version and this version of ngRepeat and didn't see a noticeable difference. But it's critical that this directive run as fast as possible.
I've made some tests with the orderBy benchmark (after some fixes), rendering 500 ngRepeated spans from scratch, and found that the new version is about 15% slower than the current master. I'm not sure how much truth is in these numbers, but the difference was consistent (still in the millisecond area, though) This is a simple ngRepeat that renders 5000 spans, without any animations.
The unit tests won't say much about performance as they usually have repeats with small numbers.
I've made a version that minimizes the changes, but the diff is still hard to follow. It'll probably be better to explain the key changes.
I'm going to do some profiling and see if I can speed things up.
I've been running the orderby-bp benchmark, updated by @Narretz today 07849779ba365f371a8caa3b58e23f677cfdc5ad.
- number of samples to collect and average: 20 (default)
- number of ngRepeats: 5000 (default)
- Loop 25x
- Windows Chrome v53
- One incognito window with five tabs, each tab with a different test
- added up mean test time from setup and $apply in all five tests
- each pass opened in new incognito window
c54f7a93e09d165d8e6742da7c8b7df2391f0c91 before pondermatic pass 1 = 1058.66 ms pass 2 = 1067.40 ms
263be6bbdfe67a877c50430fcb4acd9045ff8448 after pondermatic pass 1 = 1110.78 ms pass 2 = 1127.65 ms
My version is 5.29% slower. :(
When I removed the lines that delete the collection item's key and value from nextBlockMap, pass 1 was faster but pass 2 was much slower. Either way, the last collection is retained in memory which is not good.
Then I put the key/value in its own array so that deleting is unnecessary. I also tried an array of just the keys, which should use less memory than 263be6bbdfe67a877c50430fcb4acd9045ff8448, especially if the values are large objects. The mean of those benchmark passes was 1111.31.
I plan on committing revised code with this memory tweak and another tweak.
Commit 7a981722316779a8c14c5a1669d974db54b847ef ran the orderby-bp benchmark in 1037.09 ms for pass 1 and 1079.71 ms for pass 2. ngRepeat is now as fast as or a little bit faster than it was before.
Made some more perf tests and this looks to be basically as fast as before, nice! @matsko do you think you have time to give this a quick look?
Hi @pondermatic, I'm very close to merging this half a year later ... while doing some tests, I found a small bug and fixed it here: https://github.com/Narretz/angular.js/commit/7dbf428765d3093cd0419c8dd5b4e7bd52cbb290
Would you like to review and include this into your PR?
Hi @Narretz. Thank you for catching that bug! Your fix and unit test work perfectly.
Here's a plunker with ngRepeat and the fixed version to play around with: http://plnkr.co/edit/fu2SDoNfxCrUZPA0v0zm?p=preview
Generally looks like a good refactoring. Thanks @pondermatic
I can't believe I pushed that change without running tests. :)
I decided to do some more tests, and it looks like we created a performance problem with the new behavior when ngAnimate is used. See this updated plnkr: http://plnkr.co/edit/fu2SDoNfxCrUZPA0v0zm?p=preview
The second repeat renders 10000 elements (I know that you generally should avoid these huge lists)
- Now remove the second item in the array. Only Chrome handles this gracefully. Firefox and Edge have a noticable delay. become unresponsive and might even display "page not responding" warnings
- This behavior occurs even when you remove the animation styles, because ngAnimate will still check each item for animatable styles. Only when you disable animations or remove ngAnimate, perf returns to normal
- The bottleneck is not the ngRepeat computation or the dom operation
- ngAnimate caches the transitions / animations, so the problem mainly lies with the application / rendering of the styles to the elements (essentially the browser engine).
- with 1000 items, the performance is okay
@matsko had done some interesting changes with regard to caching animation styles here: https://github.com/angular/angular.js/pull/14166 With ngAnimate enabled and no actual animation styles, the whole process is aborted earlier with this PR, but there is no noticable performance gain.
So the question is, are we okay with the perf dropoff with large repeats? There are many ways to disable animations on perf critical parts of the page, but this might still bite people whose apps will suddenly perform worse.
I suggest we should at least look into making ngAnimate faster when no actual css animations are present.
Consistenly slow is checking when animations are allowed, because we have to traverse the node parent again again and again: https://github.com/angular/angular.js/blob/f403925ee034be3c95d3a0f78fa11dcfc905ae13/src/ngAnimate/animateQueue.js#L621 What's worse, we basically do the same later on to determine the animation order: https://github.com/angular/angular.js/blob/f403925ee034be3c95d3a0f78fa11dcfc905ae13/src/ngAnimate/animation.js#L51 maybe this could be combined, or cached?