// // EntityDecodingBenchmark.swift // RSParserTests // // Created by Brent Simmons on 4/19/26. // import XCTest @testable import RSParser import RSParserObjC // One-shot comparison benchmark: old ObjC `rsparser_stringByDecodingHTMLEntities` // vs a Swift wrapper around `XMLEntities.decode`. Prints absolute times and the // speedup ratio. Not meant to be run as part of every test suite. final class EntityDecodingBenchmark: XCTestCase { /// A realistic mix: plain text, predefined XML entities, named HTML entities, /// numeric decimal, numeric hex, a worst-case ampersand-heavy input, and an /// entity-free input (exercises the no-op path). private let inputs: [String] = [ "Tom & Jerry — “Episode 5” (™ 2025)", "Café, résumé, naïve. © 2025 – all rights reserved.", "No entities here, just plain text content that's a typical paragraph from a feed article — should exercise the no-work path efficiently.", "“Hello, "world" & everything else” — said the fox.", String(repeating: "&", count: 200), "

Mixed markup & entities: <script> 😀 ❤ — works?

", "&invalidEntity; & � &xyz &; & &;" ] private let iterations = 5_000 func testCompareImplementations() { // Warm up: run both implementations a few times so the first iteration doesn't // pay for cold caches / lazy dict init (the ObjC path builds its entity dict // on first call via dispatch_once). for _ in 0..<100 { for s in inputs { _ = (s as NSString).rsparser_stringByDecodingHTMLEntities() _ = Self.swiftDecode(s) } } // Correctness sanity — outputs should be identical for our inputs. for s in inputs { let objc = (s as NSString).rsparser_stringByDecodingHTMLEntities() let swift = Self.swiftDecode(s) XCTAssertEqual(objc, swift, "divergence for input \(s.debugDescription)") } let objcTime = time(label: "ObjC ") { for _ in 0.. Void) -> Double { let start = DispatchTime.now() block() let end = DispatchTime.now() let ms = Double(end.uptimeNanoseconds - start.uptimeNanoseconds) / 1_000_000 let perCall = (ms * 1000) / Double(iterations * inputs.count) print(String(format: "%@: %7.2f ms total, %5.2f µs/call", label, ms, perCall)) return ms } /// Swift equivalent of `rsparser_stringByDecodingHTMLEntities`: /// scan a string, expanding every `&…;` entity via `XMLEntities.decode`. private static func swiftDecode(_ s: String) -> String { let bytes = Array(s.utf8) var out: [UInt8] = [] out.reserveCapacity(bytes.count) var i = 0 while i < bytes.count { let b = bytes[i] if b == .asciiAmpersand { let d = XMLEntities.decode(bytes: bytes, at: i, mode: .normal) out.append(contentsOf: d.bytes) i = d.nextIndex } else { out.append(b) i += 1 } } return String(decoding: out, as: UTF8.self) } }