Skip to content

Commit

Permalink
Minor performance improvements
Browse files Browse the repository at this point in the history
  • Loading branch information
Eugene Kazaev committed Jan 19, 2022
1 parent 51bacfa commit 95aefee
Show file tree
Hide file tree
Showing 188 changed files with 4,507 additions and 3,897 deletions.
2 changes: 1 addition & 1 deletion ChatLayout.podspec
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
Pod::Spec.new do |s|
s.name = 'ChatLayout'
s.version = '1.1.15'
s.version = '1.1.16'
s.summary = 'Chat UI Library. It uses custom UICollectionViewLayout to provide you full control over the presentation.'
s.swift_version = '5.2'

Expand Down
2 changes: 1 addition & 1 deletion ChatLayout/Classes/Core/ChatItemAlignment.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ChatItemAlignment.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
2 changes: 1 addition & 1 deletion ChatLayout/Classes/Core/ChatLayout.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ChatLayout.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
2 changes: 1 addition & 1 deletion ChatLayout/Classes/Core/ChatLayoutAttributes.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ChatLayoutAttributes.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
2 changes: 1 addition & 1 deletion ChatLayout/Classes/Core/ChatLayoutDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ChatLayoutDelegate.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ChatLayoutInvalidationContext.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
2 changes: 1 addition & 1 deletion ChatLayout/Classes/Core/ChatLayoutPositionSnapshot.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ChatLayoutPositionSnapshot.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
2 changes: 1 addition & 1 deletion ChatLayout/Classes/Core/ChatLayoutSettings.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ChatLayoutSettings.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
2 changes: 1 addition & 1 deletion ChatLayout/Classes/Core/Extensions/CGRect+Extension.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// CGRect+Extension.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// IndexPath+Extension.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
2 changes: 1 addition & 1 deletion ChatLayout/Classes/Core/Model/ChangeItem.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ChangeItem.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
2 changes: 1 addition & 1 deletion ChatLayout/Classes/Core/Model/ItemKind.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ItemKind.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
2 changes: 1 addition & 1 deletion ChatLayout/Classes/Core/Model/ItemModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ItemModel.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
2 changes: 1 addition & 1 deletion ChatLayout/Classes/Core/Model/ItemPath.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ItemPath.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
2 changes: 1 addition & 1 deletion ChatLayout/Classes/Core/Model/ItemSize.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ItemSize.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
2 changes: 1 addition & 1 deletion ChatLayout/Classes/Core/Model/LayoutModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// LayoutModel.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
2 changes: 1 addition & 1 deletion ChatLayout/Classes/Core/Model/ModelState.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ModelState.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
2 changes: 1 addition & 1 deletion ChatLayout/Classes/Core/Model/SectionModel.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// SectionModel.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
58 changes: 50 additions & 8 deletions ChatLayout/Classes/Core/Model/StateController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// StateController.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down Expand Up @@ -99,17 +99,33 @@ final class StateController {
func layoutAttributesForElements(in rect: CGRect,
state: ModelState,
ignoreCache: Bool = false) -> [ChatLayoutAttributes] {
let predicate: (ChatLayoutAttributes) -> ComparisonResult = { attributes in
if attributes.frame.intersects(rect) {
return .orderedSame
}
if attributes.frame.minY > rect.maxY {
return .orderedDescending
}
return .orderedAscending
}

if !ignoreCache,
let cachedAttributesState = cachedAttributesState,
cachedAttributesState.rect.contains(rect) {
return cachedAttributesState.attributes.filter { $0.frame.intersects(rect) }
return cachedAttributesState.attributes.binarySearchRange(predicate: predicate)
} else {
let totalRect = rect.inset(by: UIEdgeInsets(top: -rect.height / 2, left: -rect.width / 2, bottom: -rect.height / 2, right: -rect.width / 2))
let totalRect: CGRect
switch state {
case .beforeUpdate:
totalRect = rect.inset(by: UIEdgeInsets(top: -rect.height / 2, left: -rect.width / 2, bottom: -rect.height / 2, right: -rect.width / 2))
case .afterUpdate:
totalRect = rect
}
let attributes = allAttributes(at: state, visibleRect: totalRect)
if !ignoreCache {
cachedAttributesState = (rect: totalRect, attributes: attributes)
}
let visibleAttributes = attributes.filter { $0.frame.intersects(rect) }
let visibleAttributes = rect != totalRect ? attributes.binarySearchRange(predicate: predicate) : attributes
return visibleAttributes
}
}
Expand Down Expand Up @@ -650,10 +666,10 @@ final class StateController {

// Find if any of the items of the section is visible
if [ComparisonResult.orderedSame, .orderedDescending].contains(predicate(itemIndex: section.items.count - 1)),
let firstMatchIndex = Array(0...section.items.count - 1).binarySearch(predicate: predicate) {
let firstMatchingIndex = Array(0...section.items.count - 1).binarySearch(predicate: predicate) {
// Find first item that is visible
startingIndex = firstMatchIndex
for itemIndex in (0..<firstMatchIndex).reversed() {
startingIndex = firstMatchingIndex
for itemIndex in (0..<firstMatchingIndex).reversed() {
let itemPath = ItemPath(item: itemIndex, section: sectionIndex)
guard let itemFrame = itemFrame(for: itemPath, kind: .cell, at: state, isFinal: true) else {
continue
Expand Down Expand Up @@ -725,6 +741,7 @@ final class StateController {
return self.itemAttributes(for: path, kind: kind, predefinedFrame: frame, at: state)
}
} else {
// Debug purposes only.
var attributes = [ChatLayoutAttributes]()
attributes.reserveCapacity(layout.sections.count * 1000)
layout.sections.enumerated().forEach { sectionIndex, section in
Expand Down Expand Up @@ -835,7 +852,7 @@ final class StateController {

}

private extension RandomAccessCollection where Index == Int {
extension RandomAccessCollection where Index == Int {

func binarySearch(predicate: (Element) -> ComparisonResult) -> Index? {
var lowerBound = startIndex
Expand All @@ -854,4 +871,29 @@ private extension RandomAccessCollection where Index == Int {
return nil
}

func binarySearchRange(predicate: (Element) -> ComparisonResult) -> [Element] {
guard let firstMatchingIndex = binarySearch(predicate: predicate) else {
return []
}

var startingIndex = firstMatchingIndex
for index in (0..<firstMatchingIndex).reversed() {
let attributes = self[index]
guard predicate(attributes) == .orderedSame else {
break
}
startingIndex = index
}

var lastIndex = firstMatchingIndex
for index in (firstMatchingIndex + 1)..<count {
let attributes = self[index]
guard predicate(attributes) == .orderedSame else {
break
}
lastIndex = index
}
return Array(self[startingIndex...lastIndex])
}

}
2 changes: 1 addition & 1 deletion ChatLayout/Classes/Extras/CellLayoutContainerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// CellLayoutContainerView.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ContainerCollectionReusableView.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ContainerCollectionViewCell.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ContainerCollectionViewCellDelegate.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
2 changes: 1 addition & 1 deletion ChatLayout/Classes/Extras/EdgeAligningView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// EdgeAligningView.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
2 changes: 1 addition & 1 deletion ChatLayout/Classes/Extras/ImageMaskedView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ImageMaskedView.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
2 changes: 1 addition & 1 deletion ChatLayout/Classes/Extras/MessageContainerView.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// MessageContainerView.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// RoundedCornersContainerView.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
2 changes: 1 addition & 1 deletion ChatLayout/Classes/Extras/StaticViewFactory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// StaticViewFactory.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
2 changes: 1 addition & 1 deletion Example/ChatLayout/AppDelegate.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// AppDelegate.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ChatViewControllerBuilder.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
2 changes: 1 addition & 1 deletion Example/ChatLayout/Chat/Constants.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// Constants.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
2 changes: 1 addition & 1 deletion Example/ChatLayout/Chat/Controller/ChatController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ChatController.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
// ChatControllerDelegate.swift
// https://github.com/ekazaev/ChatLayout
//
// Created by Eugene Kazaev in 2020-2021.
// Created by Eugene Kazaev in 2020-2022.
// Distributed under the MIT license.
//

Expand Down
Loading

0 comments on commit 95aefee

Please sign in to comment.