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

GH-351 Use concurrent map for column width computation

This commit is contained in:
Tae Won Ha 2016-11-26 21:14:58 +01:00
parent 91255966f1
commit 65c342f63c
No known key found for this signature in database
GPG Key ID: E40743465B5B8B44
2 changed files with 16 additions and 7 deletions

View File

@ -60,6 +60,8 @@ class FileOutlineView: NSOutlineView, Flow, NSOutlineViewDataSource, NSOutlineVi
fileprivate let fileItemService: FileItemService
fileprivate let cellWidthCache = NSCache<NSString, NSNumber>()
required init?(coder: NSCoder) {
fatalError("init(coder:) has not been implemented")
}
@ -255,17 +257,23 @@ extension FileOutlineView {
}
fileprivate func adjustColumnWidth(for items: [FileBrowserItem], outlineViewLevel level: Int) {
let cellWidth = items.reduce(CGFloat(0)) { (curMaxWidth, item) in
let itemWidth = ImageAndTextTableCell.width(with: item.fileItem.url.lastPathComponent)
if itemWidth > curMaxWidth {
return itemWidth
let column = self.outlineTableColumn!
let cellWidth = items.concurrentChunkMap(20) {
let name = $0.fileItem.url.lastPathComponent
guard let cached = self.cellWidthCache.object(forKey: name as NSString) else {
let result = ImageAndTextTableCell.width(with: name)
self.cellWidthCache.setObject(NSNumber(value: Float(result)), forKey: name as NSString)
return result
}
return curMaxWidth
return CGFloat(cached.floatValue)
}
.max() ?? column.maxWidth
let width = cellWidth + (CGFloat(level + 2) * (self.indentationPerLevel + 2)) // + 2 just to have a buffer... -_-
let column = self.outlineTableColumn!
guard column.minWidth < width else {
return
}

View File

@ -32,7 +32,8 @@ extension Array {
func concurrentChunkMap<R>(
_ chunk: Int = 100,
queue: DispatchQueue = DispatchQueue.global(qos: DispatchQoS.QoSClass.userInitiated),
transform: (Element) -> R) -> [R] {
transform: (Element) -> R) -> [R]
{
let count = self.count
let chunkedCount = Int(ceil(Float(count) / Float(chunk)))