Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Optimize for more efficient and predictable performance by changing Array to CountiguousArray #825

Open
wants to merge 8 commits into
base: master
Choose a base branch
from
34 changes: 34 additions & 0 deletions .github/PULL_REQUEST_TEMPLATE.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
I changed to use ContiguousArray< UIViewController > type to store UIViewControllers in a contiguous block of memory.

When using a large number of UIViewControllers as tabs, I think that using ContiguousArray will yield more predictable performance compared to Array.



The changed files are:

- Sources/PagerTabStripViewController.swift

for Example:

- BarExampleViewController.swift
- ButtonBarExampleViewController.swift
- InstagramExampleViewController.swift
- NavButtonBarExampleViewController.swift
- SegmentedExampleViewController.swift
- SpotifyExampleViewController.swift
- TwitterExampleViewController.swift
- YoutubeExampleViewController.swift
- YoutubeWithLabelExampleViewController.swift



References

- https://github.com/apple/swift/blob/main/docs/Arrays.rst

- https://developer.apple.com/documentation/swift/contiguousarray

- https://github.com/apple/swift/blob/main/docs/OptimizationTips.rst#advice-use-contiguousarray-with-reference-types-when-nsarray-bridging-is-unnecessary

- http://jordansmith.io/on-performant-arrays-in-swift/
- https://medium.com/@nitingeorge_39047/swift-array-vs-contiguousarray-a6153098a5
4 changes: 2 additions & 2 deletions Example/Example/BarExampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ class BarExampleViewController: BarPagerTabStripViewController {

// MARK: - PagerTabStripDataSource

override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> ContiguousArray<UIViewController> {

let child_1 = TableChildExampleViewController(style: .plain, itemInfo: "Table View")
let child_2 = ChildExampleViewController(itemInfo: "View")
Expand All @@ -63,7 +63,7 @@ class BarExampleViewController: BarPagerTabStripViewController {
}
}
let nItems = 1 + (arc4random() % 4)
return Array(childViewControllers.prefix(Int(nItems)))
return ContiguousArray(childViewControllers.prefix(Int(nItems)))
}

override func reloadPagerTabStripView() {
Expand Down
4 changes: 2 additions & 2 deletions Example/Example/ButtonBarExampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ class ButtonBarExampleViewController: ButtonBarPagerTabStripViewController {

// MARK: - PagerTabStripDataSource

override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> ContiguousArray<UIViewController> {
let child_1 = TableChildExampleViewController(style: .plain, itemInfo: "Table View")
let child_2 = ChildExampleViewController(itemInfo: "View")
let child_3 = TableChildExampleViewController(style: .grouped, itemInfo: "Table View 2")
Expand All @@ -62,7 +62,7 @@ class ButtonBarExampleViewController: ButtonBarPagerTabStripViewController {
}
}
let nItems = 1 + (arc4random() % 8)
return Array(childViewControllers.prefix(Int(nItems)))
return ContiguousArray(childViewControllers.prefix(Int(nItems)))
}

override func reloadPagerTabStripView() {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ class InstagramExampleViewController: ButtonBarPagerTabStripViewController {

// MARK: - PagerTabStripDataSource

override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> ContiguousArray<UIViewController> {
let child_1 = TableChildExampleViewController(style: .plain, itemInfo: "FOLLOWING")
let child_2 = ChildExampleViewController(itemInfo: "YOU")
return [child_1, child_2]
Expand Down
4 changes: 2 additions & 2 deletions Example/Example/NavButtonBarExampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ class NavButtonBarExampleViewController: ButtonBarPagerTabStripViewController {

// MARK: - PagerTabStripDataSource

override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> ContiguousArray<UIViewController> {
let child_1 = TableChildExampleViewController(style: .plain, itemInfo: "Table View")
let child_2 = ChildExampleViewController(itemInfo: "View")
let child_3 = TableChildExampleViewController(style: .grouped, itemInfo: "Table View 2")
Expand All @@ -82,7 +82,7 @@ class NavButtonBarExampleViewController: ButtonBarPagerTabStripViewController {
}
}
let nItems = 1 + (arc4random() % 8)
return Array(childViewControllers.prefix(Int(nItems)))
return ContiguousArray(childViewControllers.prefix(Int(nItems)))
}

override func reloadPagerTabStripView() {
Expand Down
4 changes: 2 additions & 2 deletions Example/Example/SegmentedExampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ class SegmentedExampleViewController: SegmentedPagerTabStripViewController {

// MARK: - PagerTabStripDataSource

override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> ContiguousArray<UIViewController> {
let child_1 = TableChildExampleViewController(style: .plain, itemInfo: "Table View")
let child_2 = ChildExampleViewController(itemInfo: "View")
let child_3 = TableChildExampleViewController(style: .grouped, itemInfo: "Table View 2")
Expand All @@ -58,7 +58,7 @@ class SegmentedExampleViewController: SegmentedPagerTabStripViewController {
}
}
let nItems = 1 + (arc4random() % 4)
return Array(childViewControllers.prefix(Int(nItems)))
return ContiguousArray(childViewControllers.prefix(Int(nItems)))
}

@IBAction func reloadTapped(_ sender: UIBarButtonItem) {
Expand Down
2 changes: 1 addition & 1 deletion Example/Example/Spotify/SpotifyExampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ class SpotifyExampleViewController: ButtonBarPagerTabStripViewController {

// MARK: - PagerTabStripDataSource

override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> ContiguousArray<UIViewController> {
let child_1 = TableChildExampleViewController(style: .plain, itemInfo: IndicatorInfo(title: "FRIENDS"))
child_1.blackTheme = true
let child_2 = TableChildExampleViewController(style: .plain, itemInfo: IndicatorInfo(title: "FEATURED"))
Expand Down
4 changes: 2 additions & 2 deletions Example/Example/TwitterExampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ import XLPagerTabStrip
class TwitterExampleViewController: TwitterPagerTabStripViewController {
var isReload = false

override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> ContiguousArray<UIViewController> {

let child_1 = TableChildExampleViewController(style: .plain, itemInfo: "TableView")
let child_2 = ChildExampleViewController(itemInfo: "View")
Expand All @@ -53,7 +53,7 @@ class TwitterExampleViewController: TwitterPagerTabStripViewController {
}
}
let nItems = 1 + (arc4random() % 8)
return Array(childViewControllers.prefix(Int(nItems)))
return ContiguousArray(childViewControllers.prefix(Int(nItems)))
}

@IBAction func reloadTapped(_ sender: AnyObject) {
Expand Down
2 changes: 1 addition & 1 deletion Example/Example/Youtube/YoutubeExampleViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ class YoutubeExampleViewController: BaseButtonBarPagerTabStripViewController<You

// MARK: - PagerTabStripDataSource

override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> ContiguousArray<UIViewController> {
let child_1 = TableChildExampleViewController(style: .plain, itemInfo: IndicatorInfo(title: " HOME", image: UIImage(named: "home")))
let child_2 = TableChildExampleViewController(style: .plain, itemInfo: IndicatorInfo(title: " TRENDING", image: UIImage(named: "trending")))
let child_3 = ChildExampleViewController(itemInfo: IndicatorInfo(title: " ACCOUNT", image: UIImage(named: "profile")))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ class YoutubeWithLabelExampleViewController: BaseButtonBarPagerTabStripViewContr

// MARK: - PagerTabStripDataSource

override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> ContiguousArray<UIViewController> {
let child_1 = TableChildExampleViewController(style: .plain, itemInfo: IndicatorInfo(title: " HOME", image: UIImage(named: "home")))
let child_2 = TableChildExampleViewController(style: .plain, itemInfo: IndicatorInfo(title: " TRENDING", image: UIImage(named: "trending")))
let child_3 = ChildExampleViewController(itemInfo: IndicatorInfo(title: " ACCOUNT", image: UIImage(named: "profile")))
Expand Down
8 changes: 4 additions & 4 deletions Sources/PagerTabStripViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ public protocol PagerTabStripIsProgressiveDelegate: PagerTabStripDelegate {

public protocol PagerTabStripDataSource: class {

func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController]
func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> ContiguousArray<UIViewController>
}

// MARK: PagerTabStripViewController
Expand All @@ -58,7 +58,7 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate {

open var pagerBehaviour = PagerTabStripBehaviour.progressive(skipIntermediateViewControllers: true, elasticIndicatorLimit: true)

open private(set) var viewControllers = [UIViewController]()
open private(set) var viewControllers = ContiguousArray<UIViewController>()
open private(set) var currentIndex = 0
open private(set) var preCurrentIndex = 0 // used *only* to store the index to which move when the pager becomes visible

Expand Down Expand Up @@ -177,7 +177,7 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate {

// MARK: - PagerTabStripDataSource

open func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] {
open func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> ContiguousArray<UIViewController> {
assertionFailure("Sub-class must implement the PagerTabStripDataSource viewControllers(for:) method")
return []
}
Expand Down Expand Up @@ -385,7 +385,7 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate {

}

private var pagerTabStripChildViewControllersForScrolling: [UIViewController]?
private var pagerTabStripChildViewControllersForScrolling: ContiguousArray<UIViewController>?
private var lastPageNumber = 0
private var lastContentOffset: CGFloat = 0.0
private var pageBeforeRotate = 0
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<dict>
<key>IDEDidComputeMac32BitWarning</key>
<true/>
</dict>
</plist>