[Swift] Reduce swift_beginAccess cost to optimize performance
Feature Request
Reduce swift_beginAccess cost to optimize performance
Is your feature request related to a problem? Please describe
Baseline Benchmark Numbers (commit In https://github.com/chaokunyang/fory/commit/cd360e2f64233f622103628372a7b60e6275712e)
| Benchmark | ns/op | ops/sec |
|---|---|---|
| Struct Serialize | 58.60 | 17,064,577 |
| Struct Deserialize | 66.56 | 15,024,390 |
| Sample Serialize | 300.69 | 3,325,648 |
| Sample Deserialize | 942.51 | 1,061,000 |
| MediaContent Serialize | 686.30 | 1,457,097 |
| MediaContent Deserialize | 1669.27 | 599,065 |
| StructList Serialize | 364.29 | 2,745,061 |
| StructList Deserialize | 406.82 | 2,458,083 |
| SampleList Serialize | 1420.34 | 704,059 |
| SampleList Deserialize | 4986.70 | 200,533 |
| MediaContentList Serialize | 2994.44 | 333,952 |
| MediaContentList Deserialize | 8014.31 | 124,777 |
https://github.com/apache/fory/pull/3457 (HEAD) Performance
| Benchmark | ns/op | ops/sec | Regression |
|---|---|---|---|
| Struct Serialize | 106.13 | 9,422,352 | 1.81x slower |
| Struct Deserialize | 87.18 | 11,470,519 | 1.31x slower |
| Sample Serialize | 290.24 | 3,445,375 | OK (slightly faster) |
| Sample Deserialize | 995.39 | 1,004,627 | 1.06x slower |
| MediaContent Serialize | 688.33 | 1,452,791 | OK |
| MediaContent Deserialize | 1635.96 | 611,262 | OK (slightly faster) |
| StructList Serialize | 366.24 | 2,730,416 | OK |
| StructList Deserialize | 376.84 | 2,653,632 | OK (faster) |
| SampleList Serialize | 1445.54 | 691,784 | OK |
| SampleList Deserialize | 4992.00 | 200,320 | OK |
| MediaContentList Serialize | 2991.15 | 334,320 | OK |
| MediaContentList Deserialize | CRASH | CRASH | FATAL ERROR |
Key Issues
- Struct Serialize: 1.81x regression (58.60 -> 106.13 ns/op)
- Struct Deserialize: 1.31x regression (66.56 -> 87.18 ns/op)
The cost are mostly in swift Access checks
Describe the solution you'd like
Find an approach to reduce such costs
Describe alternatives you've considered
#3355 #3457
Additional context
No response
@chaokunyang I searching approach for reducing swift serialization costs. will share approach here shortly.
@chaokunyang I traced the Struct Serialize regression (58.60 ns to 106.13 ns) to swift_beginAccess overhead on var storage: [UInt8] in ByteBuffer.
For a small all-primitive struct, the current write path triggers 6 separate storage accesses: reserve(), writeInt32() for schema hash, then 4 more inside writeRegion() (count read, append, withUnsafeMutableBufferPointer, removeLast). At ~7-8 ns per access, that adds ~42-48 ns on top of the 58 ns baseline.
I propose two fixes.
Short-term: fold the schema hash write into the writeRegion block. This eliminates the standalone reserve() and writeInt32() calls, dropping from 6 storage accesses to 4 and recovering roughly 14-16 ns.
Long-term: replace var storage: [UInt8] with a raw-pointer-backed store. A private final class holds an UnsafeMutableRawPointer and ByteBuffer holds a let reference to it. Writes through the raw pointer bypass Swift's exclusivity tracking entirely. All existing method signatures stay the same. This fixes the regression across all struct types and all serialization paths.
I am willing to implement both. I can send a draft PR if you want to review the direction first.
@chaokunyang please check these approaches, i am waiting for your direction for start working on these.
Please implement MVP and show benchmark results directly here first @miantalha45
Ok i will work on it.