From 01b4560a4d6d3f3e126a78e9322939ee213fe39d Mon Sep 17 00:00:00 2001 From: "google-labs-jules[bot]" <161369871+google-labs-jules[bot]@users.noreply.github.com> Date: Wed, 7 Jan 2026 13:12:51 +0000 Subject: [PATCH] =?UTF-8?q?=E2=9A=A1=20Bolt:=20Optimize=20SVG=20number=20p?= =?UTF-8?q?arsing?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Reduces memory allocation during SVG path parsing by creating Substrings directly from the original input string instead of allocating new Strings from UTF8View slices. This leverages `Double.init(_ text: S)`. --- Sources/SVGKit/SVGPathParser.swift | 9 +++++++-- 1 file changed, 7 insertions(+), 2 deletions(-) diff --git a/Sources/SVGKit/SVGPathParser.swift b/Sources/SVGKit/SVGPathParser.swift index bb00eb2e..e8b5cc53 100644 --- a/Sources/SVGKit/SVGPathParser.swift +++ b/Sources/SVGKit/SVGPathParser.swift @@ -166,8 +166,9 @@ public enum SVGParseError: Error, LocalizedError { // MARK: - Internal Scanner private final class PathScanner: @unchecked Sendable { + private let originalInput: String private let input: String.UTF8View - private var index: String.UTF8View.Index + private var index: String.Index // Lookup table for whitespace characters // 0x09: tab, 0x0A: line feed, 0x0C: form feed, 0x0D: carriage return, 0x20: space @@ -183,6 +184,7 @@ private final class PathScanner: @unchecked Sendable { } init(_ input: String) { + originalInput = input self.input = input.utf8 index = self.input.startIndex } @@ -260,7 +262,10 @@ private final class PathScanner: @unchecked Sendable { } let slice = input[startIndex ..< index] - guard !slice.isEmpty, let string = String(slice), let value = Double(string) else { + // OPTIMIZATION: Use Substring from original string to avoid String allocation from UTF8View slice. + // Swift's Double.init(_ text: S) where S : StringProtocol handles Substring efficiently. + let substring = originalInput[startIndex ..< index] + guard !slice.isEmpty, let value = Double(substring) else { if slice.isEmpty { return nil } // Using Array(slice) to create [UInt8] for String decoding let bytes = Array(slice)