Skip to content

Commit

Permalink
further reduce heap allocations
Browse files Browse the repository at this point in the history
  • Loading branch information
soypat committed Dec 31, 2023
1 parent 6a26e9d commit d7a45d2
Show file tree
Hide file tree
Showing 4 changed files with 38 additions and 24 deletions.
30 changes: 18 additions & 12 deletions httpx/header.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,23 +8,27 @@ import (
"strings"
)

type ResponseHeader struct {
hdr header
}

type RequestHeader struct {
header
}

type header struct {
statusCode int
contentLength int
host []byte
contentLengthBytes []byte
contentType []byte
userAgent []byte
method []byte
proto []byte
requestURI []byte
rawHeaders []byte
mulHeader []byte
cookies []argsKV
statusCode int
contentLength int
host []byte
contentLengthBytes []byte
contentType []byte
userAgent []byte
method []byte
proto []byte
requestURI []byte
rawHeaders []byte
mulHeader []byte

disableNormalizing bool
disableSpecialHeader bool
noDefaultContentType bool
Expand All @@ -34,7 +38,9 @@ type header struct {
// Reusable buffer for building strings.
bufKV argsKV
h []argsKV
cookies []argsKV
trailer []argsKV
scanner headerScanner
}

func (h *header) SetContentRange(startPos, endPos, contentLength int) {
Expand Down
4 changes: 2 additions & 2 deletions httpx/header_parse.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,8 +130,8 @@ func readRawHeaders(dst, buf []byte) ([]byte, int, error) {

func (h *header) parseHeaders(buf []byte) (int, error) {
h.contentLength = -2

var s headerScanner
h.scanner = headerScanner{}
s := &h.scanner
s.b = buf
s.disableNormalizing = h.disableNormalizing
var err error
Expand Down
10 changes: 5 additions & 5 deletions httpx/header_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ func TestRequestHeaderEmptyValueFromString(t *testing.T) {
"Host: foobar\r\n" +
"EmptyValue2: \r\n" +
"\r\n"
var h header
var h RequestHeader
br := bufio.NewReader(bytes.NewBufferString(s))
if err := h.Read(br); err != nil {
t.Fatalf("unexpected error: %v", err)
Expand All @@ -42,7 +42,7 @@ func TestRequestRawHeaders(t *testing.T) {
t.Run("normalized", func(t *testing.T) {
s := "GET / HTTP/1.1\r\n" + kvs
exp := kvs
var h header
var h RequestHeader
br := bufio.NewReader(bytes.NewBufferString(s))
if err := h.Read(br); err != nil {
t.Fatalf("unexpected error: %v", err)
Expand Down Expand Up @@ -88,7 +88,7 @@ func TestRequestRawHeaders(t *testing.T) {
t.Run("http10", func(t *testing.T) {
s := "GET / HTTP/1.0\r\n" + kvs
exp := kvs
var h header
var h RequestHeader
br := bufio.NewReader(bytes.NewBufferString(s))
if err := h.Read(br); err != nil {
t.Fatalf("unexpected error: %v", err)
Expand All @@ -107,7 +107,7 @@ func TestRequestRawHeaders(t *testing.T) {
t.Run("no-kvs", func(t *testing.T) {
s := "GET / HTTP/1.1\r\n\r\n"
exp := ""
var h header
var h RequestHeader
h.DisableNormalizing()
br := bufio.NewReader(bytes.NewBufferString(s))
if err := h.Read(br); err != nil {
Expand All @@ -134,7 +134,7 @@ func TestRequestDisableSpecialHeaders(t *testing.T) {
"Non-Special: val\r\n" +
"\r\n"

var h header
var h RequestHeader
h.DisableSpecialHeader()

s := "GET / HTTP/1.0\r\n" + kvs
Expand Down
18 changes: 13 additions & 5 deletions stacks/dhcp_client.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,8 @@ type DHCPClient struct {
svip [4]byte
requestedIP [4]byte
optionbuf [4]dhcp.Option
msgbug [1]byte
// This field is for avoiding heap allocations.
msgbug [2]byte
}

// State transition table:
Expand Down Expand Up @@ -214,13 +215,18 @@ func (d *DHCPClient) recv(pkt *UDPPacket) (err error) {
}

// Parse DHCP options looking for message type field.
var msgType dhcp.MessageType
debugEnabled := d.stack.isLogEnabled(slog.LevelDebug)
// var mt dhcp.MessageType
mt := &d.msgbug[0]
db := &d.msgbug[1]
*db = 0
if d.stack.isLogEnabled(slog.LevelDebug) {
*db = 1
}

Check warning on line 224 in stacks/dhcp_client.go

View check run for this annotation

Codecov / codecov/patch

stacks/dhcp_client.go#L223-L224

Added lines #L223 - L224 were not covered by tests
err = dhcp.ForEachOption(incpayload, func(opt dhcp.Option) error {
switch opt.Num {
case dhcp.OptMessageType:
if len(opt.Data) == 1 {
msgType = dhcp.MessageType(opt.Data[0])
*mt = opt.Data[0]
}
// The DHCP server information does not have to be in the header, but can
// be in the options. Copy into the decoded header for simplicity.
Expand All @@ -229,11 +235,13 @@ func (d *DHCPClient) recv(pkt *UDPPacket) (err error) {
copy(rcvHdr.SIAddr[:], opt.Data)
}
}
if debugEnabled && !internal.HeapAllocDebugging {
if *db != 0 && !internal.HeapAllocDebugging {
d.stack.debug("DHCP:rx", slog.String("opt", opt.Num.String()), slog.String("data", stringNumList(opt.Data)))
}
return nil
})

msgType := dhcp.MessageType(*mt)
if d.stack.isLogEnabled(slog.LevelInfo) {
d.stack.info("DHCP:rx", slog.String("msg", msgType.String()))
}
Expand Down

0 comments on commit d7a45d2

Please sign in to comment.