ITADN

Provide a way to optimize allocation amounts in `Unique/RigidArray`

#555OpenMahdiBM 创建于 2025-12-25
enhancement
M
MahdiBMcommented
When asking for reserving capacity, Swift's `Array` takes it as "minimum capacity" that it should reserve. `Unique/RigidArray` though, take it very literally and only allocate those exact amounts: ```swift import BasicContainers var uniq = UniqueArray<UInt8>(capacity: 10) var arr = Array<UInt8>() arr.reserveCapacity(10) print(uniq.capacity) /// prints 10 print(arr.capacity) /// prints 16 ``` I think It's likely better to just copy paste [the algorithm that `Array` uses](https://github.com/swiftlang/swift/blob/652402e9e83d2e970de5a12c34e419f95d1cb7f3/stdlib/public/core/ContiguousArrayBuffer.swift#L349) for reserving capacities? Or there could be new functions / initializers for this purpose. I just recently moved [swift-idna](https://github.com/swift-dns/swift-idna) to use `Unique/RigidArray` instead of `Array`, and noticed this behavior. @lorentey WDYT? I could do the PRs if this sounds like a good idea to you.
1 条评论