Bk/performance optimizations 2#159
Merged
Merged
Conversation
bryankeller
commented
Jun 9, 2026
Comment on lines
-513
to
-519
| private func mutateSectionModels( | ||
| withUnsafeMutableBufferPointer body: (inout UnsafeMutableBufferPointer<SectionModel>) -> Void) | ||
| { | ||
| // Accessing these arrays using unsafe, untyped (raw) pointers | ||
| // avoids expensive copy-on-writes and Swift retain / release calls. | ||
| sectionModels.withUnsafeMutableBufferPointer(body) | ||
| } |
Contributor
Author
There was a problem hiding this comment.
no reason for this helper
bryankeller
commented
Jun 9, 2026
Comment on lines
+867
to
+897
| if MagazineLayout._enableExperimentalOptimizations { | ||
| sectionModels.withUnsafeMutableBufferPointer { sectionModels in | ||
| for (itemModel, insertIndexPath) in (itemModelInsertIndexPathPairs.sorted { $0.insertIndexPath < $1.insertIndexPath }) { | ||
| let sectionIndex = insertIndexPath.section | ||
| let itemIndex = insertIndexPath.item | ||
| let section = sectionModels[sectionIndex] | ||
| if itemIndex < section.numberOfItems, itemModel.id == section.idForItemModel(atIndex: itemIndex) { | ||
| // If the `itemModel` to insert already exists at the destination index, then there's no need to insert it again. This | ||
| // happens if item move updates are generated in addition to section move updates, which appears to be the case when using | ||
| // `UICollectionViewDiffableDataSource`. Other diffing approaches, like Paul Heckel's, do not produce item moves when | ||
| // their containing sections move. | ||
| continue | ||
| } else { | ||
| sectionModels[insertIndexPath.section].insert(itemModel, atIndex: itemIndex) | ||
| } | ||
| } | ||
| } | ||
| } else { | ||
| for (itemModel, insertIndexPath) in (itemModelInsertIndexPathPairs.sorted { $0.insertIndexPath < $1.insertIndexPath }) { | ||
| let sectionIndex = insertIndexPath.section | ||
| let itemIndex = insertIndexPath.item | ||
| let section = sectionModels[sectionIndex] | ||
| if itemIndex < section.numberOfItems, itemModel.id == section.idForItemModel(atIndex: itemIndex) { | ||
| // If the `itemModel` to insert already exists at the destination index, then there's no need to insert it again. This | ||
| // happens if item move updates are generated in addition to section move updates, which appears to be the case when using | ||
| // `UICollectionViewDiffableDataSource`. Other diffing approaches, like Paul Heckel's, do not produce item moves when | ||
| // their containing sections move. | ||
| continue | ||
| } else { | ||
| sectionModels[insertIndexPath.section].insert(itemModel, atIndex: itemIndex) | ||
| } |
Contributor
Author
There was a problem hiding this comment.
nothing new here, just copy and pasted into two branches so we can safely test
bryankeller
commented
Jun 9, 2026
| } | ||
|
|
||
| let numberOfItems = modelState.numberOfItems(inSectionAtIndex: sectionIndex) | ||
| for itemIndex in 0..<numberOfItems { |
Contributor
Author
There was a problem hiding this comment.
this diff is kind of annoying - this else branch is just the existing code but indented.
bryankeller
commented
Jun 9, 2026
Comment on lines
+123
to
+166
| modelState.forEachSectionModel { sectionIndex, sectionModel in | ||
| reusableIndexPath.section = sectionIndex | ||
| } | ||
|
|
||
| let sectionMetrics = metricsForSection(atIndex: sectionIndex) | ||
| modelState.updateMetrics(to: sectionMetrics, forSectionAtIndex: sectionIndex) | ||
| modelState.updateMetrics( | ||
| to: metricsForSection(atIndex: sectionIndex), | ||
| forSectionAtIndex: sectionIndex, | ||
| sectionModel: §ionModel) | ||
|
|
||
| if let headerModel = headerModelForHeader(inSectionAtIndex: sectionIndex) { | ||
| modelState.setHeader(headerModel, forSectionAtIndex: sectionIndex) | ||
| } else { | ||
| modelState.removeHeader(forSectionAtIndex: sectionIndex) | ||
| } | ||
| if let headerModel = headerModelForHeader(inSectionAtIndex: sectionIndex) { | ||
| modelState.setHeader( | ||
| headerModel, | ||
| forSectionAtIndex: sectionIndex, | ||
| sectionModel: §ionModel) | ||
| } else { | ||
| modelState.removeHeader(forSectionAtIndex: sectionIndex, sectionModel: §ionModel) | ||
| } | ||
|
|
||
| if let footerModel = footerModelForFooter(inSectionAtIndex: sectionIndex) { | ||
| modelState.setFooter(footerModel, forSectionAtIndex: sectionIndex) | ||
| } else { | ||
| modelState.removeFooter(forSectionAtIndex: sectionIndex) | ||
| } | ||
| if let footerModel = footerModelForFooter(inSectionAtIndex: sectionIndex) { | ||
| modelState.setFooter( | ||
| footerModel, | ||
| forSectionAtIndex: sectionIndex, | ||
| sectionModel: §ionModel) | ||
| } else { | ||
| modelState.removeFooter(forSectionAtIndex: sectionIndex, sectionModel: §ionModel) | ||
| } | ||
|
|
||
| if let backgroundModel = backgroundModelForBackground(inSectionAtIndex: sectionIndex) { | ||
| modelState.setBackground(backgroundModel, forSectionAtIndex: sectionIndex) | ||
| } else { | ||
| modelState.removeBackground(forSectionAtIndex: sectionIndex) | ||
| } | ||
| if let backgroundModel = backgroundModelForBackground(inSectionAtIndex: sectionIndex) { | ||
| modelState.setBackground( | ||
| backgroundModel, | ||
| forSectionAtIndex: sectionIndex, | ||
| sectionModel: §ionModel) | ||
| } else { | ||
| modelState.removeBackground( | ||
| forSectionAtIndex: sectionIndex, | ||
| sectionModel: §ionModel) | ||
| } | ||
|
|
||
| let numberOfItems = modelState.numberOfItems(inSectionAtIndex: sectionIndex) | ||
| for itemIndex in 0..<numberOfItems { | ||
| if Self._enableExperimentalOptimizations { | ||
| modelState.updateItemSizeModes( | ||
| forSectionAtIndex: sectionIndex, | ||
| sectionModel: §ionModel) | ||
| { itemIndex in | ||
| reusableIndexPath.item = itemIndex | ||
| modelState.updateItemSizeMode(to: sizeModeForItem(at: reusableIndexPath), forItemAt: reusableIndexPath) | ||
| return sizeModeForItem(at: reusableIndexPath) | ||
| } |
Contributor
Author
There was a problem hiding this comment.
this is the same code as the else branch, but using the batch-mutators - shaves ~10ms off of this work for 100k items.
brynbodayle
approved these changes
Jun 9, 2026
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Details
Reduce some array overhead by using
UnsafeMutableBufferPointerwhen batch-updating thesectionModelsanditemModelsarrays.How Has This Been Tested
Tested in demo app and Airbnb app. Additionally, all unit tests pass.
Types of changes