Skip to content

Commit

Permalink
Speed up AllPagesViewController render time
Browse files Browse the repository at this point in the history
Profiling revealed that rendering AllPagesViewController spent
a substantial amount of time in FileManager's attributesOfItem. Using
a Yap view instead results in a significant speed-up.
  • Loading branch information
landakram committed Nov 26, 2019
1 parent 39dba84 commit 736467f
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 16 deletions.
14 changes: 14 additions & 0 deletions Kiwi/Wiki/Indexer.swift
Expand Up @@ -73,6 +73,20 @@ class Indexer {
})
}

func list() -> [String] {
var results: [String] = []

self.backingStore.newConnection().read({ (transaction) in
if let tx = transaction.ext("orderdedByModifiedTimeDesc") as? YapDatabaseViewTransaction {
tx.enumerateKeys(inGroup: "pages") { (collection, key, index, stop) in
results.append(key)
}
}
})

return results
}

func get(permalink: String) -> Page? {
var page: Page?

Expand Down
13 changes: 1 addition & 12 deletions Kiwi/Wiki/Wiki.swift
Expand Up @@ -92,18 +92,7 @@ class Wiki {
}

func files() -> [String] {
let files = self.filesystem.list(path: Wiki.WIKI_PATH)
return files.filter({ (path: Path) -> Bool in
return !path.isDirectory
}).sorted(by: { (path1, path2) -> Bool in
if path1.modificationDate == nil || path2.modificationDate == nil {
return true
} else {
return path1.modificationDate! > path2.modificationDate!
}
}).map({ (path: Path) -> String in
return path.fileName
})
return self.indexer.list()
}

static func isPage(_ permalink: String) -> Bool {
Expand Down
20 changes: 20 additions & 0 deletions Kiwi/Wiki/YapDatabase.swift
Expand Up @@ -36,8 +36,28 @@ class Yap {
)

_sharedInstance?.register(fullTextSearch, withName: "fts")
_sharedInstance?.register(orderedByModifiedTimeView(), withName: "orderdedByModifiedTimeDesc")
}

return _sharedInstance!
}

class func orderedByModifiedTimeView() -> YapDatabaseView {
let grouping = YapDatabaseViewGrouping.withKeyBlock { (transaction, collection, key) -> String? in
return collection
}
let sorting = YapDatabaseViewSorting.withObjectBlock { (transaction, group, collection1, key1, object1, collection2, key2, object2) -> ComparisonResult in
let page1 = (object1 as! EncodablePage).page
let page2 = (object2 as! EncodablePage).page
if page1.modifiedTime > page2.modifiedTime {
return .orderedAscending
} else if page2.modifiedTime < page2.modifiedTime {
return .orderedDescending
} else {
return .orderedSame
}
}
let versionTag = "2019-11-25"
return YapDatabaseAutoView(grouping: grouping, sorting: sorting, versionTag: versionTag)
}
}
41 changes: 37 additions & 4 deletions KiwiTests/IndexerSpec.swift
Expand Up @@ -56,18 +56,39 @@ class FakeFilesystem: EventedFilesystem {
}

class IndexerSpec: QuickSpec {

override func spec() {
describe("the Indexer") {
var indexer: Indexer!
var page: Page!
var filesystem: FakeFilesystem!
var page1: Page!
var page2: Page!

func toDate(str: String) -> Date {
let dateFormatter = DateFormatter()
dateFormatter.dateFormat = "yyyy'-'MM'-'dd"
return dateFormatter.date(from: str)!
}

beforeEach {
filesystem = FakeFilesystem()
indexer = Indexer(filesystem: filesystem)
indexer.start()
page = Page(rawContent: "test", permalink: "test_page", name: "Test", modifiedTime: Date(), createdTime: Date(), isDirty: false)
page1 = Page(
rawContent: "test",
permalink: "test_page",
name: "Test",
modifiedTime: toDate(str: "2019-11-01"),
createdTime: toDate(str: "2019-11-01"),
isDirty: false
)
page2 = Page(
rawContent: "test2",
permalink: "test_page2",
name: "Test2",
modifiedTime: toDate(str: "2019-11-02"),
createdTime: toDate(str: "2019-11-02"),
isDirty: false
)
}

context("when a page is written to the filesystem") {
Expand All @@ -82,7 +103,7 @@ class IndexerSpec: QuickSpec {

context("when a page is indexed") {
beforeEach {
indexer.index(page: page)
indexer.index(page: page1)
}

context("when a file is removed from the filesystem") {
Expand All @@ -95,6 +116,18 @@ class IndexerSpec: QuickSpec {
}
}
}

describe(".list") {
beforeEach {
indexer.index(page: page1)
indexer.index(page: page2)
}

it("returns all permalinks in descending order") {
let permalinks = indexer.list()
expect(permalinks).to(equal([page2.permalink, page1.permalink]))
}
}
}
}
}

0 comments on commit 736467f

Please sign in to comment.