From 753e37ad4b21fd3834a4ccc35fb0846c00549cbf Mon Sep 17 00:00:00 2001 From: MahdiBM Date: Thu, 1 Oct 2026 16:04:55 +0330 Subject: [PATCH 1/3] Make `FramePadding.countLeadingZeroBytes` faster --- Sources/SwiftNetwork/QUIC/QUICFrame.swift | 17 +++++++++++++++++ 1 file changed, 17 insertions(+) diff --git a/Sources/SwiftNetwork/QUIC/QUICFrame.swift b/Sources/SwiftNetwork/QUIC/QUICFrame.swift index a9557cf5..2e23c10b 100644 --- a/Sources/SwiftNetwork/QUIC/QUICFrame.swift +++ b/Sources/SwiftNetwork/QUIC/QUICFrame.swift @@ -773,8 +773,24 @@ struct FramePadding: ~Copyable, QUICFrameProtocol { private static func countLeadingZeroBytes(_ bytes: RawSpan) -> Int { let count = bytes.byteCount let wordSize = MemoryLayout.size + let vectorSize = 32 var offset = 0 + /// 1. Speed through the zeros via vectorization. + while offset &+ vectorSize <= count { + var orResult: UInt8 = 0 + /// This loop is auto-vectorized. + for idx in 0.. Date: Thu, 1 Oct 2026 23:48:55 +0330 Subject: [PATCH 2/3] Address review comments --- Sources/SwiftNetwork/QUIC/QUICFrame.swift | 10 +++++----- 1 file changed, 5 insertions(+), 5 deletions(-) diff --git a/Sources/SwiftNetwork/QUIC/QUICFrame.swift b/Sources/SwiftNetwork/QUIC/QUICFrame.swift index 2e23c10b..54447bbd 100644 --- a/Sources/SwiftNetwork/QUIC/QUICFrame.swift +++ b/Sources/SwiftNetwork/QUIC/QUICFrame.swift @@ -776,12 +776,12 @@ struct FramePadding: ~Copyable, QUICFrameProtocol { let vectorSize = 32 var offset = 0 - /// 1. Speed through the zeros via vectorization. + // 1. Speed through the zeros via vectorization. while offset &+ vectorSize <= count { var orResult: UInt8 = 0 - /// This loop is auto-vectorized. + // This loop is auto-vectorized. for idx in 0.. Date: Fri, 2 Oct 2026 00:25:11 +0330 Subject: [PATCH 3/3] Increase byte-count in tests to something more than 32 (86/87) --- Tests/QUICTests/QUICFrameTests.swift | 8 ++++---- 1 file changed, 4 insertions(+), 4 deletions(-) diff --git a/Tests/QUICTests/QUICFrameTests.swift b/Tests/QUICTests/QUICFrameTests.swift index 2af519a8..0e7bd028 100644 --- a/Tests/QUICTests/QUICFrameTests.swift +++ b/Tests/QUICTests/QUICFrameTests.swift @@ -106,8 +106,8 @@ class QUICFrameTests: XCTestCase { func testPaddingInitStopsAtFirstNonZeroByte() throws { // Offset 0 is the frame type byte, so start at 1 - for sentinelOffset in 1..<23 { - var bytes = [UInt8](repeating: 0x00, count: 23) + for sentinelOffset in 1..<87 { + var bytes = [UInt8](repeating: 0x00, count: 87) bytes[sentinelOffset] = 0xff var frame = Frame(copyBuffer: bytes) @@ -118,10 +118,10 @@ class QUICFrameTests: XCTestCase { } func testPaddingInitAllZeroes() throws { - let bytes = [UInt8](repeating: 0x00, count: 23) + let bytes = [UInt8](repeating: 0x00, count: 87) var frame = Frame(copyBuffer: bytes) let paddingFrame = try FramePadding(frame: &frame, packetNumberSpace: .applicationData) - XCTAssertEqual(paddingFrame.extraPadding, 22) + XCTAssertEqual(paddingFrame.extraPadding, 86) frame.finalize(success: true) }