From 977b6c2c20d09d16b6e76605117f2ec3229df3e3 Mon Sep 17 00:00:00 2001 From: Nabil Chatbi Date: Fri, 16 Dec 2016 20:15:00 +0100 Subject: [PATCH 1/3] performance fix --- Example/SwiftSoup/ViewController.swift | 70 ++++++++++ Sources/TextNode.swift | 2 +- ...9F80FB9B-4E37-45A8-BFE8-9AF36737A6F3.plist | 22 ++++ .../Info.plist | 31 +++++ Tests/textTests/SwiftSoupTests.swift | 123 +++++++++++++++++- 5 files changed, 246 insertions(+), 2 deletions(-) create mode 100644 SwiftSoup.xcodeproj/xcshareddata/xcbaselines/8CE4181E1DAA54A900240B42.xcbaseline/9F80FB9B-4E37-45A8-BFE8-9AF36737A6F3.plist diff --git a/Example/SwiftSoup/ViewController.swift b/Example/SwiftSoup/ViewController.swift index 19354d8..d76c539 100644 --- a/Example/SwiftSoup/ViewController.swift +++ b/Example/SwiftSoup/ViewController.swift @@ -14,6 +14,8 @@ class ViewController: UIViewController { override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view, typically from a nib. + //testPerformanceDiv() + testSite() } override func didReceiveMemoryWarning() { @@ -29,6 +31,74 @@ class ViewController: UIViewController { return doc } + func testPerformanceDiv() { + let h: String = "\n" + + "\n" + + " \n" + + " \n" + + " "\"><>&\"\n" + + " "\"><>&\"\n" + + " "\"><>&\"\n" + + " "\"><>&\"\n" + + " "\"><>&\"\n" + + " "\"><>&\"\n" + + " "\"><>&\"\n" + + " "\"><>&\"\n" + + " "\"><>&\"\n" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + " bar\n" + + " \n" + + "" + let doc: Document = try! SwiftSoup.parse(h); + do { + for _ in 0...100000{ + _ = try doc.select("div"); + } + } + catch { + } + } + + func testSite() + { + let myURLString = "http://apple.com" + guard let myURL = URL(string: myURLString) else { + print("Error: \(myURLString) doesn't seem to be a valid URL") + return + } + let html = try! String(contentsOf: myURL, encoding: .utf8) + let doc: Document = try! SwiftSoup.parse(html) + + do { + for _ in 0...100{ + _ = try doc.text() + } + } + catch { + print("Error") + } + } + } diff --git a/Sources/TextNode.swift b/Sources/TextNode.swift index 1489fe2..fbab800 100644 --- a/Sources/TextNode.swift +++ b/Sources/TextNode.swift @@ -146,7 +146,7 @@ open class TextNode : Node { } static open func lastCharIsWhitespace(_ sb: StringBuilder)->Bool { - return sb.length != 0 && sb.toString().charAt(sb.length - 1) == " "; + return sb.toString().characters.last == " "; } // attribute fiddling. create on first access. diff --git a/SwiftSoup.xcodeproj/xcshareddata/xcbaselines/8CE4181E1DAA54A900240B42.xcbaseline/9F80FB9B-4E37-45A8-BFE8-9AF36737A6F3.plist b/SwiftSoup.xcodeproj/xcshareddata/xcbaselines/8CE4181E1DAA54A900240B42.xcbaseline/9F80FB9B-4E37-45A8-BFE8-9AF36737A6F3.plist new file mode 100644 index 0000000..8572677 --- /dev/null +++ b/SwiftSoup.xcodeproj/xcshareddata/xcbaselines/8CE4181E1DAA54A900240B42.xcbaseline/9F80FB9B-4E37-45A8-BFE8-9AF36737A6F3.plist @@ -0,0 +1,22 @@ + + + + + classNames + + SwiftSoupTests + + testPerformanceDiv() + + com.apple.XCTPerformanceMetric_WallClockTime + + baselineAverage + 36.859 + baselineIntegrationDisplayName + Local Baseline + + + + + + diff --git a/SwiftSoup.xcodeproj/xcshareddata/xcbaselines/8CE4181E1DAA54A900240B42.xcbaseline/Info.plist b/SwiftSoup.xcodeproj/xcshareddata/xcbaselines/8CE4181E1DAA54A900240B42.xcbaseline/Info.plist index 2e37db7..7745749 100644 --- a/SwiftSoup.xcodeproj/xcshareddata/xcbaselines/8CE4181E1DAA54A900240B42.xcbaseline/Info.plist +++ b/SwiftSoup.xcodeproj/xcshareddata/xcbaselines/8CE4181E1DAA54A900240B42.xcbaseline/Info.plist @@ -4,6 +4,37 @@ runDestinationsByUUID + 9F80FB9B-4E37-45A8-BFE8-9AF36737A6F3 + + localComputer + + busSpeedInMHz + 100 + cpuCount + 1 + cpuKind + Intel Core i7 + cpuSpeedInMHz + 3100 + logicalCPUCoresPerPackage + 4 + modelCode + MacBookPro12,1 + physicalCPUCoresPerPackage + 2 + platformIdentifier + com.apple.platform.macosx + + targetArchitecture + x86_64 + targetDevice + + modelCode + iPhone7,2 + platformIdentifier + com.apple.platform.iphonesimulator + + F9553B46-8F24-4C2B-8A1E-8CC5535D12E1 localComputer diff --git a/Tests/textTests/SwiftSoupTests.swift b/Tests/textTests/SwiftSoupTests.swift index 5ee7da1..a7e8556 100644 --- a/Tests/textTests/SwiftSoupTests.swift +++ b/Tests/textTests/SwiftSoupTests.swift @@ -29,6 +29,127 @@ class SwiftSoupTests: XCTestCase { super.tearDown() } + private func createHtmlDocument(_ charset: String)->Document { + let doc: Document = Document.createShell(""); + try! doc.head()?.appendElement("meta").attr("charset", charset); + try! doc.head()?.appendElement("meta").attr("name", "charset").attr("content", charset); + return doc; + } + //average: 58.562, + //passed (586.015 seconds) +// func testPerformanceExample() { +// let h: String = "\n" + +// "\n" + +// " \n" + +// " \n" + +// " "\"><>&\"\n" + +// " "\"><>&\"\n" + +// " "\"><>&\"\n" + +// " "\"><>&\"\n" + +// " "\"><>&\"\n" + +// " "\"><>&\"\n" + +// " "\"><>&\"\n" + +// " "\"><>&\"\n" + +// " "\"><>&\"\n" + +// "

Hello

Another element

" + +// "

Hello

Another element

" + +// "

Hello

Another element

" + +// "

Hello

Another element

" + +// "

Hello

Another element

" + +// "

Hello

Another element

" + +// "

Hello

Another element

" + +// "

Hello

Another element

" + +// "

Hello

Another element

" + +// "

Hello

Another element

" + +// "

Hello

Another element

" + +// "

Hello

Another element

" + +// "

Hello

Another element

" + +// "

Hello

Another element

" + +// "

Hello

Another element

" + +// "

Hello

Another element

" + +// "

Hello

Another element

" + +// "

Hello

Another element

" + +// "

Hello

Another element

" + +// "

Hello

Another element

" + +// "

Hello

Another element

" + +// " bar\n" + +// " \n" + +// "" +// self.measure { +// do { +// for _ in 0...1000{ +// let doc: Document = try! SwiftSoup.parse(h); +// doc.updateMetaCharsetElement(true); +// try doc.charset(String.Encoding.isoLatin2); +// +// _ = try doc.toString() +// +// let selectedElement: Element = try doc.select("meta[charset]").first()!; +// _ = doc.charset().displayName() +// _ = try selectedElement.attr("charset") +// _ = doc.outputSettings().charset() +// _ = try doc.select("div") +// _ = try doc.cssSelector() +// _ = doc.firstElementSibling() +// _ = try doc.getElementsByAttributeValueContaining("key", "mm") +// _ = selectedElement.children() +// _ = try selectedElement.after(" c ") +// _ = try selectedElement.select("dd") +// } +// } +// catch { +// } +// } +// } + //passed (390.343 seconds). + func testPerformanceDiv() { + let h: String = "\n" + + "\n" + + " \n" + + " \n" + + " "\"><>&\"\n" + + " "\"><>&\"\n" + + " "\"><>&\"\n" + + " "\"><>&\"\n" + + " "\"><>&\"\n" + + " "\"><>&\"\n" + + " "\"><>&\"\n" + + " "\"><>&\"\n" + + " "\"><>&\"\n" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + "

Hello

Another element

" + + " bar\n" + + " \n" + + "" + let doc: Document = try! SwiftSoup.parse(h); + self.measure { + do { + for _ in 0...100000{ + _ = try doc.select("div"); + } + } + catch { + } + } + } - } From 1c69c19606386191da4065d0eab740ad830f6e5b Mon Sep 17 00:00:00 2001 From: Nabil Chatbi Date: Fri, 16 Dec 2016 21:39:10 +0100 Subject: [PATCH 2/3] removed performance test --- Tests/textTests/SwiftSoupTests.swift | 51 +--------------------------- 1 file changed, 1 insertion(+), 50 deletions(-) diff --git a/Tests/textTests/SwiftSoupTests.swift b/Tests/textTests/SwiftSoupTests.swift index a7e8556..2e64e21 100644 --- a/Tests/textTests/SwiftSoupTests.swift +++ b/Tests/textTests/SwiftSoupTests.swift @@ -102,54 +102,5 @@ class SwiftSoupTests: XCTestCase { // } // } //passed (390.343 seconds). - func testPerformanceDiv() { - let h: String = "\n" + - "\n" + - " \n" + - " \n" + - " "\"><>&\"\n" + - " "\"><>&\"\n" + - " "\"><>&\"\n" + - " "\"><>&\"\n" + - " "\"><>&\"\n" + - " "\"><>&\"\n" + - " "\"><>&\"\n" + - " "\"><>&\"\n" + - " "\"><>&\"\n" + - "

Hello

Another element

" + - "

Hello

Another element

" + - "

Hello

Another element

" + - "

Hello

Another element

" + - "

Hello

Another element

" + - "

Hello

Another element

" + - "

Hello

Another element

" + - "

Hello

Another element

" + - "

Hello

Another element

" + - "

Hello

Another element

" + - "

Hello

Another element

" + - "

Hello

Another element

" + - "

Hello

Another element

" + - "

Hello

Another element

" + - "

Hello

Another element

" + - "

Hello

Another element

" + - "

Hello

Another element

" + - "

Hello

Another element

" + - "

Hello

Another element

" + - "

Hello

Another element

" + - "

Hello

Another element

" + - " bar\n" + - " \n" + - "" - let doc: Document = try! SwiftSoup.parse(h); - self.measure { - do { - for _ in 0...100000{ - _ = try doc.select("div"); - } - } - catch { - } - } - } - + } From 15296f3ab3ecd5469636d6214efa15a98a576831 Mon Sep 17 00:00:00 2001 From: Nabil Chatbi Date: Fri, 16 Dec 2016 21:41:18 +0100 Subject: [PATCH 3/3] bump version --- SwiftSoup.podspec | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/SwiftSoup.podspec b/SwiftSoup.podspec index e51a4d0..b32b3f1 100644 --- a/SwiftSoup.podspec +++ b/SwiftSoup.podspec @@ -8,7 +8,7 @@ Pod::Spec.new do |s| s.name = 'SwiftSoup' - s.version = '1.0.1' + s.version = '1.0.2' s.summary = 'Swift HTML Parser.' # This description is used to generate tags and improve search results.