Skip to content

Commit

Permalink
perf: optimize page map iterator to avoid items copying (#329)
Browse files Browse the repository at this point in the history
  • Loading branch information
abdelmagied94 authored Nov 18, 2024
1 parent 1a1e21e commit d3eff80
Showing 1 changed file with 5 additions and 4 deletions.
9 changes: 5 additions & 4 deletions Sources/InstantSearchCore/Pagination/PageMap.swift
Original file line number Diff line number Diff line change
Expand Up @@ -82,15 +82,16 @@ struct PageMap<Item> {
// MARK: SequenceType

extension PageMap: Sequence {
public func makeIterator() -> IndexingIterator<[Item?]> {
let elements = (self.startIndex..<self.endIndex).map { self[$0] }
return IndexingIterator(_elements: elements)
public func makeIterator() -> IndexingIterator<Self> {
return IndexingIterator(_elements: self)
}
}

// MARK: CollectionType

extension PageMap: BidirectionalCollection {
public typealias Element = Item?

public typealias Index = Int

public var startIndex: Index { return 0 }
Expand All @@ -114,7 +115,7 @@ extension PageMap: BidirectionalCollection {

/// Accesses and sets elements for a given flat index position.
/// Currently, setter can only be used to replace non-optional values.
public subscript(position: Index) -> Item? {
public subscript(position: Index) -> Element {
get {
let pageIndex = self.pageIndex(for: position)
let inPageIndex = position % pageSize
Expand Down

0 comments on commit d3eff80

Please sign in to comment.