1
1
mirror of https://github.com/qvacua/vimr.git synced 2024-11-27 14:14:19 +03:00

Use a private function instead of a public extension.

It's way faster.
This commit is contained in:
Tae Won Ha 2022-09-13 21:08:27 +02:00
parent cb72c75cfa
commit 4b147028fe
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44

View File

@ -222,8 +222,7 @@ extension NvimView {
columnRange: ClosedRange<Int>
) -> [AttributesRun] {
rowRange.map { row in
self.ugrid.cells[row][columnRange]
.groupedRanges(with: { cell in cell.attrId })
groupedRanges(of: self.ugrid.cells[row][columnRange])
.compactMap { range in
let cells = self.ugrid.cells[row][range]
@ -273,3 +272,38 @@ private let infoTextAttrs = [
]
private let colorSpace = NSColorSpace.sRGB
/// When we use the following private function instead of the public extension function in
/// Commons.FoundationCommons.swift.groupedRanges(with:), then, according to Instruments
/// the percentage of the function is reduced from ~ 15% to 0%.
/// Keep the logic in sync with Commons.FoundationCommons.swift.groupedRanges(with:). Tests are
/// present in Commons lib.
private func groupedRanges(of cells: ArraySlice<UCell>) -> [ClosedRange<Int>] {
if cells.isEmpty { return [] }
if cells.count == 1 { return [cells.startIndex...cells.startIndex] }
var result = [ClosedRange<Int>]()
result.reserveCapacity(cells.count / 2)
let inclusiveEndIndex = cells.endIndex - 1
var lastStartIndex = cells.startIndex
var lastEndIndex = cells.startIndex
var lastMarker = cells.first!.attrId // cells is not empty!
for i in cells.startIndex..<cells.endIndex {
let currentMarker = cells[i].attrId
if lastMarker == currentMarker {
if i == inclusiveEndIndex { result.append(lastStartIndex...i) }
} else {
result.append(lastStartIndex...lastEndIndex)
lastMarker = currentMarker
lastStartIndex = i
if i == inclusiveEndIndex { result.append(i...i) }
}
lastEndIndex = i
}
return result
}