From ba7ebd7ce3a11687b525dfa7020ef2f3fff32bcd Mon Sep 17 00:00:00 2001 From: pandaman Date: Thu, 24 Jun 2021 18:06:05 +0900 Subject: [PATCH 1/3] Replace Array with Contiguous Array Replace those declared as Array with Contiguous Array type that is stored in contiguous blocks of memory. --- Sources/PagerTabStripViewController.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Sources/PagerTabStripViewController.swift b/Sources/PagerTabStripViewController.swift index 9aedb7d6..77a38993 100644 --- a/Sources/PagerTabStripViewController.swift +++ b/Sources/PagerTabStripViewController.swift @@ -44,7 +44,7 @@ public protocol PagerTabStripIsProgressiveDelegate: PagerTabStripDelegate { public protocol PagerTabStripDataSource: class { - func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] + func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> ContiguousArray } // MARK: PagerTabStripViewController @@ -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() 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 @@ -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 { assertionFailure("Sub-class must implement the PagerTabStripDataSource viewControllers(for:) method") return [] } @@ -385,7 +385,7 @@ open class PagerTabStripViewController: UIViewController, UIScrollViewDelegate { } - private var pagerTabStripChildViewControllersForScrolling: [UIViewController]? + private var pagerTabStripChildViewControllersForScrolling: ContiguousArray? private var lastPageNumber = 0 private var lastContentOffset: CGFloat = 0.0 private var pageBeforeRotate = 0 From f5a7529d16d11e24b3baca3fe3ec2496d1ad476a Mon Sep 17 00:00:00 2001 From: pandaman Date: Thu, 24 Jun 2021 18:28:49 +0900 Subject: [PATCH 2/3] Replace Array with ContiguousArray in Example project --- Example/Example/BarExampleViewController.swift | 4 ++-- Example/Example/ButtonBarExampleViewController.swift | 4 ++-- .../Instagram/InstagramExampleViewController.swift | 2 +- Example/Example/NavButtonBarExampleViewController.swift | 4 ++-- Example/Example/SegmentedExampleViewController.swift | 4 ++-- .../Example/Spotify/SpotifyExampleViewController.swift | 2 +- Example/Example/TwitterExampleViewController.swift | 4 ++-- .../Example/Youtube/YoutubeExampleViewController.swift | 2 +- .../YoutubeWithLabelExampleViewController.swift | 2 +- .../xcshareddata/IDEWorkspaceChecks.plist | 8 ++++++++ 10 files changed, 22 insertions(+), 14 deletions(-) create mode 100644 XLPagerTabStrip.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist diff --git a/Example/Example/BarExampleViewController.swift b/Example/Example/BarExampleViewController.swift index d7feea7b..81022a4d 100644 --- a/Example/Example/BarExampleViewController.swift +++ b/Example/Example/BarExampleViewController.swift @@ -43,7 +43,7 @@ class BarExampleViewController: BarPagerTabStripViewController { // MARK: - PagerTabStripDataSource - override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] { + override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> ContiguousArray { let child_1 = TableChildExampleViewController(style: .plain, itemInfo: "Table View") let child_2 = ChildExampleViewController(itemInfo: "View") @@ -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() { diff --git a/Example/Example/ButtonBarExampleViewController.swift b/Example/Example/ButtonBarExampleViewController.swift index e496d241..94c1975d 100644 --- a/Example/Example/ButtonBarExampleViewController.swift +++ b/Example/Example/ButtonBarExampleViewController.swift @@ -38,7 +38,7 @@ class ButtonBarExampleViewController: ButtonBarPagerTabStripViewController { // MARK: - PagerTabStripDataSource - override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] { + override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> ContiguousArray { 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") @@ -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() { diff --git a/Example/Example/Instagram/InstagramExampleViewController.swift b/Example/Example/Instagram/InstagramExampleViewController.swift index 392cae15..a7f59cca 100644 --- a/Example/Example/Instagram/InstagramExampleViewController.swift +++ b/Example/Example/Instagram/InstagramExampleViewController.swift @@ -53,7 +53,7 @@ class InstagramExampleViewController: ButtonBarPagerTabStripViewController { // MARK: - PagerTabStripDataSource - override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] { + override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> ContiguousArray { let child_1 = TableChildExampleViewController(style: .plain, itemInfo: "FOLLOWING") let child_2 = ChildExampleViewController(itemInfo: "YOU") return [child_1, child_2] diff --git a/Example/Example/NavButtonBarExampleViewController.swift b/Example/Example/NavButtonBarExampleViewController.swift index f9012222..36968a89 100644 --- a/Example/Example/NavButtonBarExampleViewController.swift +++ b/Example/Example/NavButtonBarExampleViewController.swift @@ -58,7 +58,7 @@ class NavButtonBarExampleViewController: ButtonBarPagerTabStripViewController { // MARK: - PagerTabStripDataSource - override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] { + override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> ContiguousArray { 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") @@ -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() { diff --git a/Example/Example/SegmentedExampleViewController.swift b/Example/Example/SegmentedExampleViewController.swift index 62552e4d..11b90c77 100644 --- a/Example/Example/SegmentedExampleViewController.swift +++ b/Example/Example/SegmentedExampleViewController.swift @@ -37,7 +37,7 @@ class SegmentedExampleViewController: SegmentedPagerTabStripViewController { // MARK: - PagerTabStripDataSource - override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] { + override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> ContiguousArray { 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") @@ -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) { diff --git a/Example/Example/Spotify/SpotifyExampleViewController.swift b/Example/Example/Spotify/SpotifyExampleViewController.swift index 4fa26a5d..7e7311f5 100644 --- a/Example/Example/Spotify/SpotifyExampleViewController.swift +++ b/Example/Example/Spotify/SpotifyExampleViewController.swift @@ -56,7 +56,7 @@ class SpotifyExampleViewController: ButtonBarPagerTabStripViewController { // MARK: - PagerTabStripDataSource - override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] { + override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> ContiguousArray { let child_1 = TableChildExampleViewController(style: .plain, itemInfo: IndicatorInfo(title: "FRIENDS")) child_1.blackTheme = true let child_2 = TableChildExampleViewController(style: .plain, itemInfo: IndicatorInfo(title: "FEATURED")) diff --git a/Example/Example/TwitterExampleViewController.swift b/Example/Example/TwitterExampleViewController.swift index 6cf7e04f..443499a5 100644 --- a/Example/Example/TwitterExampleViewController.swift +++ b/Example/Example/TwitterExampleViewController.swift @@ -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 { let child_1 = TableChildExampleViewController(style: .plain, itemInfo: "TableView") let child_2 = ChildExampleViewController(itemInfo: "View") @@ -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) { diff --git a/Example/Example/Youtube/YoutubeExampleViewController.swift b/Example/Example/Youtube/YoutubeExampleViewController.swift index f22e9ea9..340956d7 100644 --- a/Example/Example/Youtube/YoutubeExampleViewController.swift +++ b/Example/Example/Youtube/YoutubeExampleViewController.swift @@ -66,7 +66,7 @@ class YoutubeExampleViewController: BaseButtonBarPagerTabStripViewController [UIViewController] { + override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> ContiguousArray { 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"))) diff --git a/Example/Example/YoutubeWithLabel/YoutubeWithLabelExampleViewController.swift b/Example/Example/YoutubeWithLabel/YoutubeWithLabelExampleViewController.swift index c0893f4f..2b35a34f 100644 --- a/Example/Example/YoutubeWithLabel/YoutubeWithLabelExampleViewController.swift +++ b/Example/Example/YoutubeWithLabel/YoutubeWithLabelExampleViewController.swift @@ -68,7 +68,7 @@ class YoutubeWithLabelExampleViewController: BaseButtonBarPagerTabStripViewContr // MARK: - PagerTabStripDataSource - override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> [UIViewController] { + override func viewControllers(for pagerTabStripController: PagerTabStripViewController) -> ContiguousArray { 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"))) diff --git a/XLPagerTabStrip.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist b/XLPagerTabStrip.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist new file mode 100644 index 00000000..18d98100 --- /dev/null +++ b/XLPagerTabStrip.xcodeproj/project.xcworkspace/xcshareddata/IDEWorkspaceChecks.plist @@ -0,0 +1,8 @@ + + + + + IDEDidComputeMac32BitWarning + + + From adfa765c4ede57224d126e355cccbc0447fc4bb1 Mon Sep 17 00:00:00 2001 From: pandaman Date: Mon, 28 Jun 2021 10:35:20 +0900 Subject: [PATCH 3/3] Add PRTemplate --- .github/PULL_REQUEST_TEMPLATE.md | 34 ++++++++++++++++++++++++++++++++ 1 file changed, 34 insertions(+) diff --git a/.github/PULL_REQUEST_TEMPLATE.md b/.github/PULL_REQUEST_TEMPLATE.md index e69de29b..010f4821 100644 --- a/.github/PULL_REQUEST_TEMPLATE.md +++ b/.github/PULL_REQUEST_TEMPLATE.md @@ -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 \ No newline at end of file