Skip to content

Commit

Permalink
Merge pull request #15 from bboreham/stack-attrs
Browse files Browse the repository at this point in the history
Allocate space for Attr slice on the stack, to save garbage-collection
  • Loading branch information
Harmen authored Oct 20, 2018
2 parents 1ea2662 + 217e76d commit 840bf5b
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 9 deletions.
21 changes: 14 additions & 7 deletions client.go
Original file line number Diff line number Diff line change
Expand Up @@ -303,7 +303,8 @@ func (c Conn) ConnTCP(local map[string]struct{}) *ConnTCP {
func parsePayload(b []byte) (*Conn, error) {
// Most of this comes from libnetfilter_conntrack/src/conntrack/parse_mnl.c
conn := &Conn{}
attrs, err := parseAttrs(b)
var attrSpace [16]Attr
attrs, err := parseAttrs(b, attrSpace[0:0])
if err != nil {
return conn, err
}
Expand Down Expand Up @@ -333,7 +334,8 @@ func parsePayload(b []byte) (*Conn, error) {
}

func parseTuple(b []byte, tuple *Tuple) error {
attrs, err := parseAttrs(b)
var attrSpace [16]Attr
attrs, err := parseAttrs(b, attrSpace[0:0])
if err != nil {
return fmt.Errorf("invalid tuple attr: %s", err)
}
Expand All @@ -356,7 +358,8 @@ func parseTuple(b []byte, tuple *Tuple) error {
}

func parseCounters(b []byte) (uint64, uint64, error) {
attrs, err := parseAttrs(b)
var attrSpace [16]Attr
attrs, err := parseAttrs(b, attrSpace[0:0])
if err != nil {
return 0, 0, fmt.Errorf("invalid tuple attr: %s", err)
}
Expand All @@ -374,7 +377,8 @@ func parseCounters(b []byte) (uint64, uint64, error) {
}

func parseIP(b []byte, tuple *Tuple) error {
attrs, err := parseAttrs(b)
var attrSpace [16]Attr
attrs, err := parseAttrs(b, attrSpace[0:0])
if err != nil {
return fmt.Errorf("invalid tuple attr: %s", err)
}
Expand All @@ -396,7 +400,8 @@ func parseIP(b []byte, tuple *Tuple) error {
}

func parseProto(b []byte, tuple *Tuple) error {
attrs, err := parseAttrs(b)
var attrSpace [16]Attr
attrs, err := parseAttrs(b, attrSpace[0:0])
if err != nil {
return fmt.Errorf("invalid tuple attr: %s", err)
}
Expand Down Expand Up @@ -427,7 +432,8 @@ func parseProto(b []byte, tuple *Tuple) error {
}

func parseProtoinfo(b []byte, conn *Conn) error {
attrs, err := parseAttrs(b)
var attrSpace [16]Attr
attrs, err := parseAttrs(b, attrSpace[0:0])
if err != nil {
return fmt.Errorf("invalid tuple attr: %s", err)
}
Expand All @@ -445,7 +451,8 @@ func parseProtoinfo(b []byte, conn *Conn) error {
}

func parseProtoinfoTCP(b []byte, conn *Conn) error {
attrs, err := parseAttrs(b)
var attrSpace [16]Attr
attrs, err := parseAttrs(b, attrSpace[0:0])
if err != nil {
return fmt.Errorf("invalid tuple attr: %s", err)
}
Expand Down
3 changes: 1 addition & 2 deletions netlink_attr.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,8 +16,7 @@ type Attr struct {
IsNetByteorder bool
}

func parseAttrs(b []byte) ([]Attr, error) {
var attrs []Attr
func parseAttrs(b []byte, attrs []Attr) ([]Attr, error) {
for len(b) >= attrHdrLength {
var attr Attr
attr, b = parseAttr(b)
Expand Down

0 comments on commit 840bf5b

Please sign in to comment.