From 9d0d92bda30cfc094d8bf54b3cc0c219652e2f23 Mon Sep 17 00:00:00 2001 From: Mihaela Mihaljevic Date: Fri, 26 Jun 2026 00:11:49 +0200 Subject: [PATCH] perf(xpath): make translate() and the substring search functions linear (#308) translate(string, from, to) ran from.firstIndex(of: character) for every character of the source, so a large from/to map made it O(string * from). And contains/substring-before/substring-after matched the needle at every position of the haystack (Array.starts(with:) per index), O(n*m) and quadratic on a repetitive string, which is a denial-of-service vector on untrusted input. translate() now builds a from-character to replacement map (and a delete set) once, first occurrence winning, and translates each source character with an O(1) lookup. The substring search is Knuth-Morris-Pratt, O(n+m), which never rescans. The results are unchanged. A 4000-character large-alphabet translate drops ~0.13s to ~0.002s; a 16000- character repetitive contains drops ~0.054s to ~0.0002s. The full suite, the W3C XPath/XSLT and Apache Xalan corpora, and the WASM build stay green; TranslateFunctionTests and SubstringSearchTests pin the semantics. --- CHANGELOG.md | 4 +++ Sources/XPath/StringFunctions.swift | 53 ++++++++++++++++++++++++----- Tests/SubstringSearchTests.swift | 52 ++++++++++++++++++++++++++++ Tests/TranslateFunctionTests.swift | 52 ++++++++++++++++++++++++++++ 4 files changed, 152 insertions(+), 9 deletions(-) create mode 100644 Tests/SubstringSearchTests.swift create mode 100644 Tests/TranslateFunctionTests.swift diff --git a/CHANGELOG.md b/CHANGELOG.md index 762dfed..351dd42 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ## [Unreleased] +### Fixed + +- The `translate()` and `contains()`/`substring-before()`/`substring-after()` string functions are no longer quadratic. `translate()` scanned its `from` string for every source character (O(string * from)); it now builds the from-to map once and translates each character with an O(1) lookup. The substring functions matched the needle at every position of the haystack (O(n*m), quadratic and a denial-of-service vector on a repetitive untrusted string); they now use a linear-time Knuth-Morris-Pratt search. The results are unchanged (first-occurrence-wins translation, first-occurrence substring match). A large-alphabet `translate()` and a repetitive `contains()` each drop from hundreds of milliseconds to under a millisecond at 4k-16k characters. Guarded by `TranslateFunctionTests` and `SubstringSearchTests`. + ## [0.4.5] - 2026-06-25 ### Fixed diff --git a/Sources/XPath/StringFunctions.swift b/Sources/XPath/StringFunctions.swift index 36ee7c1..27e6fe0 100644 --- a/Sources/XPath/StringFunctions.swift +++ b/Sources/XPath/StringFunctions.swift @@ -49,10 +49,31 @@ extension PureXML.XPath { } /// The index of the first occurrence of `needle` in `haystack`, or nil. + /// Linear-time (Knuth-Morris-Pratt): matching `needle` at every position + /// of `haystack` is O(n*m) and quadratic on a repetitive string (a denial + /// of service vector on untrusted input); KMP never rescans, so it is + /// O(n+m). private static func search(_ needle: [Character], in haystack: [Character]) -> Int? { guard !needle.isEmpty else { return 0 } - for start in haystack.indices where haystack[start...].starts(with: needle) { - return start + guard needle.count <= haystack.count else { return nil } + // The failure function: failure[i] is the length of the longest + // proper prefix of needle[0...i] that is also a suffix of it. + var failure = [Int](repeating: 0, count: needle.count) + var prefix = 0 + for index in 1 ..< needle.count { + while prefix > 0, needle[index] != needle[prefix] { + prefix = failure[prefix - 1] + } + if needle[index] == needle[prefix] { prefix += 1 } + failure[index] = prefix + } + var matched = 0 + for index in haystack.indices { + while matched > 0, haystack[index] != needle[matched] { + matched = failure[matched - 1] + } + if haystack[index] == needle[matched] { matched += 1 } + if matched == needle.count { return index - needle.count + 1 } } return nil } @@ -80,14 +101,28 @@ extension PureXML.XPath { let source = argument(arguments, 0) let from = Array(argument(arguments, 1)) let replacements = Array(argument(arguments, 2)) - var result = "" - for character in source { - guard let index = from.firstIndex(of: character) else { - result.append(character) - continue - } + // Build the translation map once: a from-character maps to its + // replacement, or is marked for deletion when it has no positional + // replacement. The first occurrence in `from` wins (the XPath rule). + // Each source character is then translated by an O(1) lookup rather + // than a linear scan of `from`, which made this quadratic. + var replace: [Character: Character] = [:] + var delete: Set = [] + for (index, character) in from.enumerated() { + if replace[character] != nil || delete.contains(character) { continue } if index < replacements.count { - result.append(replacements[index]) + replace[character] = replacements[index] + } else { + delete.insert(character) + } + } + var result = "" + result.reserveCapacity(source.count) + for character in source { + if let replacement = replace[character] { + result.append(replacement) + } else if !delete.contains(character) { + result.append(character) } } return result diff --git a/Tests/SubstringSearchTests.swift b/Tests/SubstringSearchTests.swift new file mode 100644 index 0000000..7a045b0 --- /dev/null +++ b/Tests/SubstringSearchTests.swift @@ -0,0 +1,52 @@ +import Testing +@testable import PureXML + +/// `contains`, `substring-before`, and `substring-after` locate the needle +/// through a linear-time (KMP) search rather than matching at every position +/// (which is quadratic on a repetitive string). These pin the search semantics: +/// found at the start/middle/end, not found, overlapping repeats, and the empty +/// needle, plus a repetitive case that the naive search would have made slow. +@Suite("substring search functions") +struct SubstringSearchTests { + private func string(_ expression: String) throws -> String { + try PureXML.XPath.Query(expression).value(at: PureXML.parseTree("")).string + } + + private func boolean(_ expression: String) throws -> Bool { + try PureXML.XPath.Query(expression).value(at: PureXML.parseTree("")).boolean + } + + @Test("contains finds the needle anywhere, or reports its absence") + func test_contains() throws { + #expect(try boolean("contains('hello world', 'world')")) + #expect(try boolean("contains('hello world', 'hello')")) + #expect(try boolean("contains('hello world', 'o w')")) + #expect(try !boolean("contains('hello', 'xyz')")) + #expect(try boolean("contains('hello', '')")) + // Overlapping repeats: the match starts at the first valid position. + #expect(try boolean("contains('aaaa', 'aaa')")) + #expect(try !boolean("contains('aaa', 'aaaa')")) + } + + @Test("substring-before and substring-after split at the first occurrence") + func test_split() throws { + #expect(try string("substring-before('1999/04/01', '/')") == "1999") + #expect(try string("substring-after('1999/04/01', '/')") == "04/01") + #expect(try string("substring-before('a-b-c', '-')") == "a") + #expect(try string("substring-after('a-b-c', '-')") == "b-c") + // Needle absent: both return the empty string (XPath 1.0). + #expect(try string("substring-before('abc', 'x')") == "") + #expect(try string("substring-after('abc', 'x')") == "") + } + + @Test("the search is correct on a repetitive string the naive scan made slow") + func test_repetitive() throws { + let haystack = String(repeating: "a", count: 500) + "b" + // 'a...ab' (the whole string) is present once, at the start. + #expect(try boolean("contains('\(haystack)', '\(haystack)')")) + // 'a...a' + 'c' is absent; the search must report false without rescanning. + let absent = String(repeating: "a", count: 400) + "c" + #expect(try !boolean("contains('\(haystack)', '\(absent)')")) + #expect(try string("substring-after('\(haystack)', 'b')") == "") + } +} diff --git a/Tests/TranslateFunctionTests.swift b/Tests/TranslateFunctionTests.swift new file mode 100644 index 0000000..0b920b7 --- /dev/null +++ b/Tests/TranslateFunctionTests.swift @@ -0,0 +1,52 @@ +import Testing +@testable import PureXML + +/// `translate(string, from, to)` builds its from-to map once for an O(1) lookup +/// per source character instead of scanning `from` per character. These pin the +/// XPath 1.0 semantics it must preserve: positional replacement, deletion when a +/// from-character has no positional replacement, first-occurrence-wins for a +/// repeated from-character, and pass-through for characters not in `from`. +@Suite("translate function") +struct TranslateFunctionTests { + private func translate(_ source: String, _ from: String, _ target: String) throws -> String { + let escaped = { (text: String) in text.replacingOccurrences(of: "'", with: "'") } + let query = "translate('\(escaped(source))', '\(escaped(from))', '\(escaped(target))')" + return try PureXML.XPath.Query(query).value(at: PureXML.parseTree("")).string + } + + private func scalars(_ base: Int, _ count: Int) -> String { + String((0 ..< count).compactMap { UnicodeScalar(base + $0).map(Character.init) }) + } + + @Test("positional replacement and pass-through") + func test_replace() throws { + #expect(try translate("bar", "abc", "ABC") == "BAr") + #expect(try translate("abcdef", "abc", "xyz") == "xyzdef") + #expect(try translate("hello", "", "") == "hello") + } + + @Test("a from-character with no positional replacement is deleted") + func test_delete() throws { + // The classic spec example: '-' has no replacement, so it is removed. + #expect(try translate("--aaa--", "abc-", "ABC") == "AAA") + #expect(try translate("abc", "abc", "x") == "x") + #expect(try translate("a1b2c3", "123", "") == "abc") + } + + @Test("the first occurrence of a repeated from-character wins") + func test_firstOccurrenceWins() throws { + // Second 'a' in `from` is ignored: 'a' maps to the index-0 replacement. + #expect(try translate("abc", "aba", "XYZ") == "XYc") + // First occurrence is a deletion (no positional replacement); the later + // occurrence that would replace is ignored. + #expect(try translate("ab", "aa", "X") == "Xb") + } + + @Test("translating a long string with a large map is correct") + func test_largeMap() throws { + // The quadratic-prone case made fast: many distinct characters mapped. + let source = scalars(0x100, 300) + let target = scalars(0x101, 300) + #expect(try translate(source, source, target) == target) + } +}