mirror of
https://github.com/apple/swift-http-structured-headers
synced 2026-11-24 04:37:50 +00:00
Remove unnecessary genericism and generic constraints.
Motivation: Early on in the design of Structured Headers I made a conscious effort to try to avoid transforming types in the low-level API. This design was built on the assumption that strings, tokens, and some other data types could be built entirely as slices of the backing data. Unfortunately, this isn't true: because of the need to escape chars in Strings, it became necessary to start using more complex types in Item. Eventually, we ended up in a place where the only field in Item that actually stored backing data was `.undecodedByteSequence`. This field forced almost the entire set of types in the StructuredHeaders module to be generic, just for the sake of carrying this one slice type around. This prevented powerful optimisations, bloated code size, and forced additional unnecessary heap allocations. It also forced us to constrain the types used in the parser to those where the subsequence type was Hashable, which excluded some really important types (like `String.UTF8View`). Fundamentally, this genericism was hurting more than it helped at this point, so I bit the bullet and replaced the slice type with `String` for `undecodedByteSequence`. This led to a huge cascading removal of generic type parameters for a wide range of types, as well as the removal of the "must have hashable subsequence" constraints on the parser data type. This lifts an enormous amount of complexity out of the module at the extremely minor cost of forcing an allocation whenever we have a lot of undecoded binary data: not a huge cost, in my view, and well worth what we got back in return. Modifications: - Remove generic type parameter in BaseItem. - Remove generic types everywhere else. - Remove hashable constraint. Result: Better, faster, simpler code.
This commit is contained in:
1 parent
246d886cf7
commit
b2ea94c173
16 files changed
+131
-133
No files matched your search
@@ -46,7 +46,7 @@ extension StructuredFieldDecoder {
|
||||
/// - data: The bytes of the structured header field.
|
||||
/// - throws: If the header field could not be parsed, or could not be decoded.
|
||||
/// - returns: An object of type `StructuredField`.
|
||||
public func decode<StructuredField: StructuredHeaderField, BaseData: RandomAccessCollection>(_ type: StructuredField.Type = StructuredField.self, from data: BaseData) throws -> StructuredField where BaseData.Element == UInt8, BaseData.SubSequence: Hashable {
|
||||
public func decode<StructuredField: StructuredHeaderField, BaseData: RandomAccessCollection>(_ type: StructuredField.Type = StructuredField.self, from data: BaseData) throws -> StructuredField where BaseData.Element == UInt8 {
|
||||
switch StructuredField.structuredFieldType {
|
||||
case .item:
|
||||
return try self.decodeItemField(from: data)
|
||||
@@ -64,7 +64,7 @@ extension StructuredFieldDecoder {
|
||||
/// - data: The bytes of the structured header field.
|
||||
/// - throws: If the header field could not be parsed, or could not be decoded.
|
||||
/// - returns: An object of type `StructuredField`.
|
||||
private func decodeDictionaryField<StructuredField: Decodable, BaseData: RandomAccessCollection>(_ type: StructuredField.Type = StructuredField.self, from data: BaseData) throws -> StructuredField where BaseData.Element == UInt8, BaseData.SubSequence: Hashable {
|
||||
private func decodeDictionaryField<StructuredField: Decodable, BaseData: RandomAccessCollection>(_ type: StructuredField.Type = StructuredField.self, from data: BaseData) throws -> StructuredField where BaseData.Element == UInt8 {
|
||||
let parser = StructuredFieldParser(data)
|
||||
let decoder = _StructuredFieldDecoder(parser, keyDecodingStrategy: self.keyDecodingStrategy)
|
||||
try decoder.parseDictionaryField()
|
||||
@@ -78,7 +78,7 @@ extension StructuredFieldDecoder {
|
||||
/// - data: The bytes of the structured header field.
|
||||
/// - throws: If the header field could not be parsed, or could not be decoded.
|
||||
/// - returns: An object of type `StructuredField`.
|
||||
private func decodeListField<StructuredField: Decodable, BaseData: RandomAccessCollection>(_ type: StructuredField.Type = StructuredField.self, from data: BaseData) throws -> StructuredField where BaseData.Element == UInt8, BaseData.SubSequence: Hashable {
|
||||
private func decodeListField<StructuredField: Decodable, BaseData: RandomAccessCollection>(_ type: StructuredField.Type = StructuredField.self, from data: BaseData) throws -> StructuredField where BaseData.Element == UInt8 {
|
||||
let parser = StructuredFieldParser(data)
|
||||
let decoder = _StructuredFieldDecoder(parser, keyDecodingStrategy: self.keyDecodingStrategy)
|
||||
try decoder.parseListField()
|
||||
@@ -92,7 +92,7 @@ extension StructuredFieldDecoder {
|
||||
/// - data: The bytes of the structured header field.
|
||||
/// - throws: If the header field could not be parsed, or could not be decoded.
|
||||
/// - returns: An object of type `StructuredField`.
|
||||
private func decodeItemField<StructuredField: Decodable, BaseData: RandomAccessCollection>(_ type: StructuredField.Type = StructuredField.self, from data: BaseData) throws -> StructuredField where BaseData.Element == UInt8, BaseData.SubSequence: Hashable {
|
||||
private func decodeItemField<StructuredField: Decodable, BaseData: RandomAccessCollection>(_ type: StructuredField.Type = StructuredField.self, from data: BaseData) throws -> StructuredField where BaseData.Element == UInt8 {
|
||||
let parser = StructuredFieldParser(data)
|
||||
let decoder = _StructuredFieldDecoder(parser, keyDecodingStrategy: self.keyDecodingStrategy)
|
||||
try decoder.parseItemField()
|
||||
@@ -111,7 +111,7 @@ extension StructuredFieldDecoder {
|
||||
}
|
||||
}
|
||||
|
||||
class _StructuredFieldDecoder<BaseData: RandomAccessCollection> where BaseData.Element == UInt8, BaseData.SubSequence: Hashable {
|
||||
class _StructuredFieldDecoder<BaseData: RandomAccessCollection> where BaseData.Element == UInt8 {
|
||||
private var parser: StructuredFieldParser<BaseData>
|
||||
|
||||
// For now we use a stack here because the CoW operations on Array would suck. Ideally I'd just have us decode
|
||||
@@ -220,13 +220,13 @@ extension _StructuredFieldDecoder: Decoder {
|
||||
extension _StructuredFieldDecoder {
|
||||
/// The basic elements that make up a Structured Header
|
||||
fileprivate enum Element {
|
||||
case dictionary(OrderedMap<String, ItemOrInnerList<BaseData.SubSequence>>)
|
||||
case list([ItemOrInnerList<BaseData.SubSequence>])
|
||||
case item(Item<BaseData.SubSequence>)
|
||||
case innerList(InnerList<BaseData.SubSequence>)
|
||||
case bareItem(BareItem<BaseData.SubSequence>)
|
||||
case bareInnerList(BareInnerList<BaseData.SubSequence>)
|
||||
case parameters(OrderedMap<String, BareItem<BaseData.SubSequence>>)
|
||||
case dictionary(OrderedMap<String, ItemOrInnerList>)
|
||||
case list([ItemOrInnerList])
|
||||
case item(Item)
|
||||
case innerList(InnerList)
|
||||
case bareItem(BareItem)
|
||||
case bareInnerList(BareInnerList)
|
||||
case parameters(OrderedMap<String, BareItem>)
|
||||
|
||||
func innerElement(for key: _StructuredHeaderCodingKey) throws -> Element {
|
||||
switch self {
|
||||
|
||||
Reference in new issue
Block a user