Skip to content

Commit

Permalink
Use 16 headers as starting point
Browse files Browse the repository at this point in the history
  • Loading branch information
normanmaurer committed Apr 10, 2018
1 parent 827abfe commit 82dbe29
Showing 1 changed file with 11 additions and 8 deletions.
19 changes: 11 additions & 8 deletions Sources/NIOHTTP1/HTTPDecoder.swift
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ import CNIOHTTPParser
private struct HTTPParserState {
var dataAwaitingState: DataAwaitingState = .messageBegin
var currentNameIndex: HTTPHeaderIndex?
var currentHeaders: [HTTPHeader] = []
var currentHeaders: [HTTPHeader]
var currentURI: URI?
var currentStatus: String?
var slice: (readerIndex: Int, length: Int)?
Expand All @@ -36,9 +36,15 @@ private struct HTTPParserState {
case body
}

init() {
// We start with space for 16 headers.
self.currentHeaders = []
self.currentHeaders.reserveCapacity(16)
}

mutating func reset() {
self.currentNameIndex = nil
self.currentHeaders = []
self.currentHeaders.removeAll(keepingCapacity: true)
self.currentURI = nil
self.currentStatus = nil
self.slice = nil
Expand Down Expand Up @@ -196,17 +202,15 @@ public class HTTPDecoder<HTTPMessageT>: ByteToMessageDecoder, AnyHTTPDecoder {
let method = HTTPMethod.from(httpParserMethod: http_method(rawValue: parser.pointee.method))
let version = HTTPVersion(major: parser.pointee.http_major, minor: parser.pointee.http_minor)
let request = HTTPRequestHead(version: version, method: method, rawURI: state.currentURI!, headers: HTTPHeaders(buffer: cumulationBuffer!, headers: state.currentHeaders))
state.currentNameIndex = nil
state.currentHeaders = []
self.state.currentHeaders.removeAll(keepingCapacity: true)
return request
}

private func newResponseHead(_ parser: UnsafeMutablePointer<http_parser>!) -> HTTPResponseHead {
let status = HTTPResponseStatus(statusCode: Int(parser.pointee.status_code), reasonPhrase: state.currentStatus!)
let version = HTTPVersion(major: parser.pointee.http_major, minor: parser.pointee.http_minor)
let response = HTTPResponseHead(version: version, status: status, headers: HTTPHeaders(buffer: cumulationBuffer!, headers: state.currentHeaders))
state.currentNameIndex = nil
state.currentHeaders = []
self.state.currentHeaders.removeAll(keepingCapacity: true)
return response
}

Expand Down Expand Up @@ -339,8 +343,7 @@ public class HTTPDecoder<HTTPMessageT>: ByteToMessageDecoder, AnyHTTPDecoder {
handler.state.dataAwaitingState = .messageBegin

let trailers = handler.state.currentHeaders.isEmpty ? nil : HTTPHeaders(buffer: handler.state.cumulationBuffer!, headers: handler.state.currentHeaders)
handler.state.currentNameIndex = nil
handler.state.currentHeaders = []
handler.state.currentHeaders.removeAll(keepingCapacity: true)
switch handler {
case let handler as HTTPRequestDecoder:
handler.pendingInOut.append(handler.wrapInboundOut(HTTPServerRequestPart.end(trailers)))
Expand Down

0 comments on commit 82dbe29

Please sign in to comment.