Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

improve statusLine and StatusMessage by using slice instead of map #855

Merged
merged 4 commits into from
Aug 2, 2020
Merged
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
26 changes: 5 additions & 21 deletions status.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,6 @@ package fasthttp

import (
"fmt"
"sync/atomic"
)

const (
Expand Down Expand Up @@ -81,8 +80,7 @@ const (
)

var (
invalidStatusLines atomic.Value
statusLines = make([][]byte, statusMessageMax+1)
statusLines = make([][]byte, statusMessageMax+1)

statusMessages = []string{
StatusContinue: "Continue",
Expand Down Expand Up @@ -167,8 +165,6 @@ func StatusMessage(statusCode int) string {
}

func init() {
invalidStatusLines.Store(make(map[int][]byte))

// Fill all valid status lines
for i := 0; i < len(statusLines); i++ {
statusLines[i] = []byte(fmt.Sprintf("HTTP/1.1 %d %s\r\n", i, StatusMessage(i)))
Expand All @@ -183,22 +179,10 @@ func statusLine(statusCode int) []byte {
return statusLines[statusCode]
}

// invalidStatusLine return status line of which bigger than statusMessageMax(511)
// invalidStatusLine return status line of which
// smaller than 0 or
// bigger than statusMessageMax(511)
erikdubbelboer marked this conversation as resolved.
Show resolved Hide resolved
func invalidStatusLine(statusCode int) []byte {
m := invalidStatusLines.Load().(map[int][]byte)
h := m[statusCode]
if h != nil {
return h
}

statusText := StatusMessage(statusCode)

h = []byte(fmt.Sprintf("HTTP/1.1 %d %s\r\n", statusCode, statusText))
newM := make(map[int][]byte, len(m)+1)
for k, v := range m {
newM[k] = v
}
newM[statusCode] = h
invalidStatusLines.Store(newM)
return h
return []byte(fmt.Sprintf("HTTP/1.1 %d %s\r\n", statusCode, statusText))
}