Project: titanium
Issue TIMOB-23447: Android: Optimize removeAllChildren()
The current implementation of [removeAllChildren()|https://github.com/appcelerator/titanium_mobile/blob/415bd6c66dcc55b1a59a59574f3babd3c3a84ede/android/titanium/src/java/org/appcelerator/titanium/proxy/TiViewProxy.java#L719] creates a copy of all views and removes each view in a while loop. The people at Shockoe showed in a [blog post|http://shockoe.com/development/profiling-titanium/] that it takes up to 24 seconds to remove 1000 views with {code:java} theWindow.removeAllChildren(); {code} Changing the TiViewProxy method to use the native [ViewGroup.removeAllViews()|https://developer.android.com/reference/android/view/ViewGroup.html#removeAllViews%28%29] would bring down delay below 100ms! *Test app:* https://github.com/bpulley/titanium-profiling can be used to test the behavior. Just run the "Remove views en masse" test. {code:java} var win = Ti.UI.createWindow({ backgroundColor: "white" }); var view = Ti.UI.createView({ top: 0, left: 0, right: 0, height: 100, layout: "horizontal" }); var scrollview = Ti.UI.createScrollView({ top: 100, left: 0, right: 0, height: 100, layout: "horizontal", type: "horizontal" }); for (var i = 0; i < 100; i++) { var v = Ti.UI.createView({ width: 10, height: 10, backgroundColor: "red", left: 10 }); view.add(v); v = null; var v2 = Ti.UI.createView({ width: 10, height: 10, backgroundColor: "red", left: 10 }); scrollview.add(v2); v2 = null; } // Listview var listView = Ti.UI.createListView({ top: 200, height: 100 }); var sections = []; var fruitSection = Ti.UI.createListSection({ headerTitle: 'Fruits' }); var fruitDataSet = [{ properties: { title: 'Apple' } }, { properties: { title: 'Banana' } }, ]; fruitSection.setItems(fruitDataSet); sections.push(fruitSection); var vegSection = Ti.UI.createListSection({ headerTitle: 'Vegetables' }); var vegDataSet = [{ properties: { title: 'Carrots' } }, { properties: { title: 'Potatoes' } }, ]; vegSection.setItems(vegDataSet); sections.push(vegSection); listView.sections = sections; var fishSection = Ti.UI.createListSection({ headerTitle: 'Fish' }); var fishDataSet = [{ properties: { title: 'Cod' } }, { properties: { title: 'Haddock' } }, ]; fishSection.setItems(fishDataSet); function onOpen(e) { var startTime = new Date(); console.log("View length: " + view.getChildren().length); view.removeAllChildren(); console.log("View after remove length: " + view.getChildren().length); console.log("ScrollView length: " + scrollview.getChildren().length); scrollview.removeAllChildren(); console.log("ScrollView after remove length: " + scrollview.getChildren().length); console.log("ListView length: " + listView.getSections().length); listView.removeAllChildren(); console.log("ListView after remove length: " + listView.getSections().length); var endTime = new Date();	var delta = endTime - startTime; alert('time ' + delta + 'ms'); } win.add(listView); win.add(view); win.add(scrollview); win.addEventListener("open", onOpen); win.open(); {code} Ti SDK 5.2.2 = 4160ms (and listview.removeAllViews() is not working) Patched Version = 333ms (listview will be empty)
Story points: 5